Help with Alphabetizing strings - Programmers Heaven

Howdy, Stranger!

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

Categories

Help with Alphabetizing strings

jloginjlogin Posts: 1Member
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

  • evindaevinda Posts: 12Member
    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]













  • evindaevinda Posts: 12Member
    I hope I could help!!!!!!!!!!!!!!!!!!!












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