I have written a program that i want to input for example “11:12” to and then get “11” and “12” back. This is my code:
#include<stdio.h>
#include <string.h>
#include <conio.h>
int main(void)
{
char s[] = "";
char seperator[] = ":";
char *ch;
while (1)
{
scanf("%s", &s); //scanf("%s", &s);
if (s == -1) break;
char *result = NULL;
result = strtok(s, seperator);
while( result != NULL ) {
printf( "result is \"%s\"\n", result );
result = strtok( NULL, seperator );
}
fflush(stdout);
}
return 0;
}
but when i input “11:12”, this is my output:
result is "11"
result is "12→ ("
How can i get rid of the last “→ (“?
You are not allocating any memory for your “s” array. Try it out like this:
Plus, you are doing a double indirection on scanf with parameter &s, it is not necessary since “s” will be decayed to a pointer as a parameter to the function.
Try it like this: