Arranging characters in alphabetical order
I have written a code that asks the user for a word and then it arranges
that word in alphabetical order and stores it in another string.
#include<stdio.h>
main()
{
char in[100],out[100],ch;
int i,len,j=0;
//ask user for a word
printf("Enter a word: ");
scanf("%s",in);
//arrange that word in alphabetical order
for(ch = 'a'; ch <= 'z'; ++ch)
for(i = 0; i < strlen(in); ++i)
if(in[i] == ch)
{
out[j] = ch;
++j;
}
//print the word
printf("%s",out);
fflush(stdin);
getchar();
}
The problem is when word is stored in the other string, there are some
extra letters or symbols after that word. Can someone please tell me what
could be possibly wrong with my code.
No comments:
Post a Comment