Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- // substring.c
- // compile with: gcc -o substring substring.c
- // run with: ./substring "input string" start_index end_index
- int main(int argc, char **argv) {
- // argv[1] is the inputted string // first argument
- // atoi(); is a function that converts strings to integers; refer to `man 3 atoi'
- int start = atoi(argv[2]); // second argument; start position
- int end = atoi(argv[3]); // third argument; end position
- // allocate space for our inputted string.
- char *string = malloc(sizeof(char) * (strlen(argv[1]) + 1));
- // copy input string to working string
- strcpy(string,argv[1]);
- // allocate space for our partial string
- char *str = malloc(sizeof(char) * end);
- // the magic
- strncpy(str,&(string[start]),end);
- // and the goods.
- printf("input string = \"%s\"; start: %d; end: %d; returned string = \"%s\";\n",argv[1],start,end,str);
- return EXIT_SUCCESS;
- }
Add Comment
Please, Sign In to add comment