In: Computer Science
Write a code in C that will take four words from the user and show those in ascending order.
If you have any queries please comment in the comments section I will surely help you out and if you found this solution to be helpful kindly upvote.
Solution :
Code :
#include<stdio.h>
#include<string.h>
// main function
int main()
{
// declare variables
int i,j;
char words[4][100],temp[4];
// input words
for(i=0;i<4;i++)
{
scanf("%s",&words[i]);
}
// iterate from 0th intex to 3rd index
for(i=0;i<4;i++)
{
// now iterate from (i+1)th index to 3rd index
for(j=i+1;j<4;j++)
{
// if the ith word is greater than the jth word then swap
if(strcmp(words[i],words[j])>0)
{
strcpy(temp,words[i]);
strcpy(words[i],words[j]);
strcpy(words[j],temp);
}
}
}
// print the words
for(i=0;i<4;i++)
{
printf("%s\n",words[i]);
}
return 0;
}
Output :