| |

VerySource

 Forgot password?
 Register
Search
View: 950|Reply: 3

119! ~ Urgent! ~ Design a program cuts

[Copy link]

2

Threads

2

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-23 11:00:01
| Show all posts |Read mode
Design a program cuts that reads data from standard input and obtains data in the range defined by the first parameter n and the second parameter m. Both n and m are integers, that is, the nth is extracted from the input string. All characters between and including the m character. E.g:
$ cuts 11 14
this is a test of cuts program (type)
test (show results)
5
Reply

Use magic Report

0

Threads

13

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-2-8 11:00:01
| Show all posts
#include <stdio.h>
#include <stdlib.h>

#define MAX_STR 100

int main (int argc, char * argv []) {
  int start, end, len;
  char str [MAX_STR];
  char output [MAX_STR];

  if (argc <3) {
    printf ("Not enough parameters!\n");
    printf ("Usage:% s start end\n", argv [0]);
    exit (0);
  }

  start = atoi (argv [1]);
  --start;
  end = atoi (argv [2]);
  --end;
  if (start> end) {
    printf ("start> end!\n");
    exit (0);
  }

  if (start <0) start = 0;
  fgets (str, MAX_STR, stdin);
  len = strlen (str);
  if (end> len-1) end = len-1;
  strncpy (output, str + start, end-start + 1);
  output [end-start + 1] = '\0';
  printf ("% s", output);
}
Reply

Use magic Report

0

Threads

13

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-2-8 12:00:01
| Show all posts
C ++ version:

#include <iostream>

int main (int argc, char * argv []) {
  int start, end, len;
  std :: string str;

  if (argc <3) {
    std :: cout << "Not enough parameters!\n";
    exit (0);
  }

  start = atoi (argv [1]);
  end = atoi (argv [2]);
  --start;
  --end;
  if (start> end) {
    std :: cout << "start> end!\n";
    exit (0);
  }

  if (start <0) start = 0;
  std :: cin >> str;
  std :: cout << str.substr (start, end-start + 1) << std :: endl;
}
Reply

Use magic Report

0

Threads

13

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-2-8 13:45:01
| Show all posts
You can directly use the cut command in Linux:
cut -c N-M
N and M are start and end respectively
Ctrl-D to exit
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list