سؤال

The code is pretty simple. I don't understand why my program crashes when I try to print out pointok2. Maybe I don't see the problem because I am tired or something. A little help would be appreciated.

Tokmain will always be of the format "word word".

scanf("%s",&tokmain);
for(i=0;i<50;i++) {
    if (tokmain[i]=='\n') {
        tokmain[i]='\0';
        lettercount=i;
        break;
    }
}
pointok1=strtok(tokmain,delim);
pointok2=strtok(NULL,delim);

puts(pointok1);
puts(pointok2);`
هل كانت مفيدة؟

المحلول

scanf() truncates at the space. So, tokmain will only have "word" when you enter "word word".

Try

scanf("%49[^\n]s",&tokmain); /*Notice the format string*/

for(i=0;i<50;i++) {
    if (tokmain[i]=='\n') {
        tokmain[i]='\0';
        lettercount=i;
        break;
    }
}
pointok1=strtok(tokmain,delim);
pointok2=strtok(NULL,delim);

puts(pointok1);
puts(pointok2);`
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top