I have this code in C, where I will be inputting a string of numbers separated by spaces, and splitting it with strsep
. If I input a string like "1 2"
, and set strcmp
to look for a number before the last element, the code works, but if I set strcmp
to look for the last element, the code fails. Can you suggest any fixes?
char *string = malloc(1028), *found;
if (fgets(string, 1028, stdin) != NULL) {
while ((found = strsep(&string, " ")) != NULL) {
if (strcmp(found, "2") == 0) {
printf("%s\n", found);
}
}
}