char - C strtok, enter by enter -


i want read data: sample text opp see this: sample (enter) text (enter) opp (enter)

however, code not work well.

#include <stdio.h> #include <stdlib.h> int main(){     char separator[] = " ";     char *schowek;     char *wejscie;      gets(&wejscie);     schowek = strtok(&wejscie,separator);      while( schowek != null )     {         printf( "%s\n", schowek );         schowek = strtok( null, separator );     }      return 0; } 

ok, have code.

#include <stdio.h> #include <stdlib.h>  int main() {      char * slowo[] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety", "hundred", "thousand", "million"};     int liczba[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,30,40,50,60,70,80,90,100,1000,1000000};      char n[]="";     int i=0;      char s1[]="zero";       char separator[] = " ";      char wejscie[1024];     if (fgets(wejscie, 1024, stdin))     {         char* schowek = strtok(wejscie,separator); /* removed '&'. */          if(strcmp(wejscie,n)==0)         {             exit;         }          while (schowek)         {             printf("%s\n", schowek);             schowek = strtok(null, separator);         }     }     return 0; } 

is alright him? want convert string number.

sample input

six negative 7 hundred twenty 9 1 million 1 hundred one

sample output

6 -729 1000101

how can that?

you did not allocate buffer hold read data.

 char *wejscie; 

allocates pointer hoild address of buffer, not actual buffer.

 gets(&wejscie); 

reads pointer, not big enough buffer.

this way (allocates buffer on stack):

 char buffer[1024];  fgets(buffer, sizeof(buffer), stdin); 

if wish use pointer , dynamic memory way it:

 char * wejscie = malloc(1024);  fgets(wejscie, 1024, stdin); 

Comments