This C Program Sort Word in String.
Here is source code of the C Program to Sort Word in String. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C Program to Sort Word in String
*/
#include <stdio.h>
#include <string.h>
void main()
{
int count = 0, c = 0, i, j = 0, k, l, space = 0;
char str[100], p[50][100], str1[20], ptr1[50][100], cmp[50];
printf("Enter the string\n");
scanf(" %[^\n]s", str);
for (i = 0;i < strlen(str);i++)
{
if ((str[i] == ' ')||(str[i] == ', ')||(str[i] == '.'))
{
space++;
}
}
for (i = 0, j = 0, k = 0;j < strlen(str);j++)
{
if ((str[j] == ' ')||(str[j] == 44)||(str[j] == 46))
{
p[i][k] = '\0';
i++;
k = 0;
}
else
p[i][k++] = str[j];
}
for (i = 0;i < space;i++) //loop for sorting
{
for (j = i + 1;j <= space;j++)
{
if ((strcmp(p[i], p[j]) > 0))
{
strcpy(cmp, p[i]);
strcpy(p[i], p[j]);
strcpy(p[j], cmp);
}
}
}
printf("After sorting string is \n");
for (i = 0;i <= space;i++)
{
printf("%s ", p[i]);
}
}
$ cc string18.c $ a.out Enter the string welcome to sanfoundry's c programming class, welcome to c class again After sorting string is again c c class class programming sanfoundry's to to welcome welcome
Sanfoundry Global Education & Learning Series – 1000 C Programs.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement
Here’s the list of Best Books in C Programming, Data-Structures and Algorithms
If you wish to look at programming examples on all topics, go to C Programming Examples.
Next Steps:
- Get Free Certificate of Merit in C Programming
- Participate in C Programming Certification Contest
- Become a Top Ranker in C Programming
- Take C Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Related Posts:
- Watch Advanced C Programming Videos
- Practice Computer Science MCQs
- Practice BCA MCQs
- Buy C Books
- Buy Computer Science Books