0% found this document useful (0 votes)
3 views

Unit 3 String Excercise 5

Uploaded by

tv5874
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Unit 3 String Excercise 5

Uploaded by

tv5874
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

School of Computing Science and Engineering

Course Code : R1UC101B Name: Programming for Problem Solving-C

UNIT III
String: Exercise:-5

Program Name: B.Tech. (CSE)


Ex-1 : C Program to Sort Elements in Lexicographical Order
(Dictionary Order)
In this example, you will learn to sort 5 strings entered by the user in the
lexicographical order (dictionary order).
#include <stdio.h>
#include <string.h>

int main() {
char str[5][50], temp[50];
printf("Enter 5 words: ");

// Getting strings input


for (int i = 0; i < 5; ++i) {
fgets(str[i], sizeof(str[i]), stdin);
}
Continue…

// storing strings in the lexicographical order


for (int i = 0; i < 5; ++i) {
for (int j = i + 1; j < 5; ++j) {

// swapping strings if they are not in the lexicographical order


if (strcmp(str[i], str[j]) > 0) {
strcpy(temp, str[i]);
strcpy(str[i], str[j]);
strcpy(str[j], temp);
}
}
}
Continue…

printf("\nIn the lexicographical order: \n");


for (int i = 0; i < 5; ++i) {
fputs(str[i], stdout);
}
return 0;
}
Output
Enter 5 words: R programming
JavaScript
Java
C programming
C++ programming
Output

In the lexicographical order:


C programming
C++ programming
Java
JavaScript
R programming
References

 http://kirste.userpage.fu-berlin.de/chemnet/use/info/libc/libc_7.html
 Let Us C by Yashavant Kanetkar : Authentic Guide to C PROGRAMMING Language 17th
Edition, BPB Publications
 C in Depth by by S.K.Srivastava and Deepali Srivastava, BPB Publications

You might also like