This code is a C program that sorts a given number of strings in lexicographical order. It begins by asking the user to enter the number of strings. Then, it prompts the user to input each string one by one. After obtaining all the strings, it uses nested loops and the `strcmp` function to compare and sort the strings in lexicographical order. Finally, it prints the sorted strings to the console. The code utilizes the standard libraries `stdio.h`, `stdlib.h`, and `string.h` for input/output operations, memory allocation, and string manipulation, respectively.
Source code of this program :
Lexicographic.C
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(){
int number;
char string[10][100],s[100];
printf("Enter number of strings : ");
scanf("%d", &number);
// Here we will get strings from the user
for (int i = 0; i < number; i++)
{
printf("Enter string %d : ", i + 1);
scanf("%s", string[i]);
}
// now to sort strings in lexicographical order
for (int i = 0; i < number; i++)
{
for (int j = i+1; j < number; j++)
{
if (strcmp(string[i], string[j])>0)
{
strcpy(s, string[i]);
strcpy(string[i], string[j]);
strcpy(string[j] , s);
}
}
}
printf("\n");
// now to print the result
printf("Strings in lexicographical order : \n");
for (int i = 0; i < number; i++)
{
printf("-> %s",string[i]);
printf("\n");
}
return 0;
}
Follow for more code and there source codes
THANK YOU
Comments
Post a Comment