Answer to Question #316348 in C for Lee

Question #316348

a C program that declare an array of five characters using a WHILE loop and sort the elements in row one in alphabetical order using a nested FOR loop and IF statement and print the sorted characters using a WHILE loop

1
Expert's answer
2022-03-23T02:58:15-0400
#include <stdio.h>

int main()
{
    int i, j;
    char charray[5];
    char temp;
    i = 0;
    printf("Enter five chars:\n");
    while (i < 5)
    {
        charray[i]=getchar();
        getchar();
        i++;
    }
    for(i = 0; i < 4; i++)
        for(j = i+1; j < 5; j++)
        {
            if (charray[i] > charray[j])
            {
                temp = charray[i];
                charray[i] = charray[j];
                charray[j] = temp;
            }
        }
    
    printf("\n\nSorted chars:\n");
    i = 0;
    while (i < 5)
    {
        printf("%c ", charray[i]);
        i++;
    }

    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS