Computer Programming C Laboratory: String C Programs

To sort names (strings) in ascending order using C Program

String C Programming

To sort names (strings) in ascending order using C Program.

#include <stdio.h>

#include <string.h>

#include <conio.h>

void main()

{

int num,i,j, result, index;

char name[25][25]; /*declaring array of strings */

char temp[25];

printf("Number of names to be sorted in ascending order\n");

scanf("%d", &num);

printf("Enter %d names to be sorted\n", num);

for(i=0;i<num;i++)

         scanf("%s", name[i]);  /*input the names*/

for(i=0;i<num;i++)

{

         index=i;

         for(j=i+1;j<num;j++)

         {

                  result = strcmp(name[index], name[j]);

                  if(result > 0)

                           index=j;

         }

}

strcpy(temp, name[index]);

strcpy(name[index], name[i]);

strcpy(name[i], temp);

}

printf("\nNames Sorted in Ascending Order\n");

for(i=0;i<num;i++)

         printf("\t%s", name[i]);

}

OUTPUT

Number of names to be sorted in ascending order 4

Enter 4 Names to be sorted

Basker

Aravind

Damu

Chinni

Names sorted in Ascending order

Aravind

Baskar

Chinni

Damu

EXPLANATION: The main logic to sort numbers in ascending order remains same over here, however the only difference is that we have to use string functions to compare two strings and also for swapping two strings we have to use string functions.


Computer Programming C Laboratory: String C Programs : Tag: C Programming : - To sort names (strings) in ascending order using C Program


Computer Programming C Laboratory: String C Programs



Under Subject


Computer Programming C Laboratory

CS25C01 1st Semester EEE, ECE, CSE, CSE(CY), AIDS, IT practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation



Related Subjects


English Essentials I

EN25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


தமிழர் மரபு - Heritage of Tamils

UC25H01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Calculus

MA25C01 Maths 1 M1 - 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Physics I

PH25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Chemistry I

CY25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Makerspace

ME25C04 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Computer Programming C

CS25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Computer Programming Python

CS25C02 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Fundamentals of Electrical and Electronics Engineering

EE25C03 1st Semester EEE Depart | 2025 Regulation | 1st Semester 2025 Regulation


Introduction to Mechanical Engineering

ME25C03 1st Semester Mechanical Dept | 2025 Regulation | 1st Semester 2025 Regulation


Introduction to Civil Engineering

CE25C01 1st Semester Civil, Agri Departments | 2025 Regulation | 1st Semester 2025 Regulation


Essentials of Computing

CS25C03 1st Semester - AIDS, CSE, CSE(CY), IT Department | 2025 Regulation | 1st Semester 2025 Regulation


Applied Physics I Laboratory

PH25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Applied Chemistry I Laboratory

CY25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Computer Programming C Laboratory

CS25C01 1st Semester EEE, ECE, CSE, CSE(CY), AIDS, IT practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Computer Programming Python Laboratory

CS25C02 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Engineering Drawing

ME25C01 EEE, Mech, Agri, EEE Depts | 2025 Regulation | 2nd Semester 2025 Regulation


Basic Electronics and Electrical Engineering

EE25C04 1st Semester ECE Dept | 2025 Regulation | 1st Semester 2025 Regulation


Essentials of Computing - Laboratory

CS25C03 1st Semester AIDS, CSE, CSE(CY), IT Depts | practical Laboratory Manual | 2025 Regulation | 1st Semester 2025 Regulation