Hi everyone I'm starting to learn C programming and I have a bad time in making a program that sort the words in ascending order using strtok. Like this:
INPUT: I am good.
OUTPUT: am
good
i
Please help me, thanks! :)
Printable View
Hi everyone I'm starting to learn C programming and I have a bad time in making a program that sort the words in ascending order using strtok. Like this:
INPUT: I am good.
OUTPUT: am
good
i
Please help me, thanks! :)
Post up what you have tried. That fulfills the requirements of the forum, AND it helps us to help you, as well. The doctor must see the patient. ;)
I do not have a code yet..
Hmm.. Then take your time to write some code and then post back if needed ;)
Well the first step is using strtok so you can just do
INPUT: I am good.
OUTPUT:
I
am
good
Then we'll talk about sorting, if you can't figure it out in the meantime.
http://cboard.cprogramming.com/c-pro...t-process.html
Code:#include<stdio.h>
#include<conio>.h>
void main(){
int i,j,n;
char str[20][20],temp[20];
puts("Enter the no. of string to be sorted");
scanf("%d",&n);
for(i=0;i<=n;i++)
gets(str[i]);
for(i=0;i<=n;i++)
for(j=i+1;j<=n;j++){
if(strcmp(str[i],str[j])>0){
strcpy(temp,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],temp);
}
}
printf("The sorted string\n");
for(i=0;i<=n;i++)
puts(str[i]);
getch();
}
I didn't use strtok here, because I do not know how to use it in sorting.
You don't use it in sorting. What you probably do is use it prior to the sorting, to break up the sentence into individual words that can be sorted.
It is possible that I will store the individual words into a new variable?
I expect that you would have an array to store the individual words, or pointers thereof, then you would sort this array.Quote:
Originally Posted by programmerc
Tokenization example with strtok on the forum
http://cboard.cprogramming.com/c-pro...ml#post1140965
You can modify this to fit your problem specification, just sort the words after they are collected.