Answers for "store names and arrange them in ascending order in c"

C
0

store names and arrange them in ascending order in c

#include <stdio.h>
#include <string.h>

void main()
{
	char name[10][20], temp_name[10][20], temp[20];
    int i, j, n;

    printf("Only the first letter of the name should be in uppercase\n\n");
    printf("enter number of names you want to input: ");
    scanf("%d", &n);

    for (i = 0; i < n; i++)
    {
        printf("Enter name %d: ", i +1);
        scanf("%s", &name[i]);
        strcpy(temp_name[i], name[i]);
    }

    for (i = 0; i < n - 1 ; i++)
    {
        for (j = i + 1; j < n; j++)
        {
            if (strcmp(name[i], name[j]) > 0)
            {
                strcpy(temp, name[i]);
                strcpy(name[i], name[j]);
                strcpy(name[j], temp);
            }
        }
    }


    printf("\n\nSorted names in ascending alphabetical order\n\n");

	for (i = 0; i < n; i++)
    {
            printf("%s\n", name[i]);
    }
}
Posted by: Guest on June-23-2021

Code answers related to "store names and arrange them in ascending order in c"

Code answers related to "C"

Browse Popular Code Answers by Language