#include <stdio.h> #include <stdlib.h> #include <locale.h> #include <malloc.h> void sort(char *words[],int size) { int n,m,code; char *temp; for(n=1; n<size; n++) for(m=0; m<size-n; m++) { code=strcmp(words[m],words[m+1]); if(code>0) { temp=words[m]; words[m]=words[m+1]; words[m+1]=temp; } } printf("\nsort:\n"); for (n = 0; n < size; n++) printf("%s ",words[n] ); } int main() { char *words[5]; char word[5][50]; int i; for (i=0;i<5;i++){ printf("slovo:"); gets(word[i]); words[i]=word[i]; } int size =sizeof(words)/sizeof(char*); sort(words,size); } 

How can I enter a string using strtok, and not word for word as above? (Sort alphabetically, but I would like the string to be entered right away)

  • uh ... scanf("%s") ? - pavel
  • @pavel ummm, no, this is not the point, the problem is that the line needs to be broken into words using strtok, I don’t understand how to do it - Mar
  • Catch This is not a strtok, but something similar and, as for me, so convenient. - avp

1 answer 1

 #include<stdio.h> int main() { char buff[200]; fgets(buff,200,stdin); char* token=strtok(buff," "); while(token) { printf("%s\n", token); token=strtok(NULL," "); } }