Help with Alphabetizing strings

I need help...

Input : Hello My World
Output : eHllo My World

desired output : eHllo My dlorW

Somehow my loop does not continue to the next series of words and stops after the first one...

Code:

puts ("Enter a String: ");
gets (c,"");

for(i=0;c[i] !=' ';i++)
{
for(j=0;c[j]!=' ';j++)
{
if(toupper(c[i])<toupper(c[j]))
{
t=c[i];
c[i]=c[j];
c[j]=t;


}
}

}

puts("This is your string alphabetize:");
puts(c);

Comments

  • Your program gives not the right result,because the loop stops after i=4 and j=4.
    Using the following code,you will get the desired output:
    [code]
    #include

    int main()
    {
    char c[50],t;
    int i,j,m,k;
    puts ("Enter a String: ");
    gets (c);
    for(i=0;c[i] !=' ';i++)
    {
    for(j=0;c[j]!=' ';j++){
    if(toupper(c[i])<toupper(c[j])){
    t=c[i];
    c[i]=c[j];
    c[j]=t;
    }
    printf("i=%d,j=%d
    ",i,j);
    }
    }
    for(i=9;c[i] !='';i++)
    {
    for(j=9;c[j]!=' ';j++){
    if(toupper(c[i])<toupper(c[j])){
    t=c[i];
    c[i]=c[j];
    c[j]=t;
    }
    }
    }
    puts("This is your string alphabetize:");
    puts(c);
    return 0;
    }
    [/code]













  • I hope I could help!!!!!!!!!!!!!!!!!!!












  • If you don't understand something,tell me and I will explain it to you!
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion