This C Program sorts the string(ignore spaces) and repeated characters should be present only once.
Here is source code of the C Program to sort the string(ignore spaces) and repeated characters should be present only once. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C Program to Sort the String(ignore spaces) and Repeated
* Characters should be present only Once
*/
#include <stdio.h>
#include <string.h>
void main()
{
int i, j = 0, k = 0;
char str[100], str1[10][20], temp, min;
printf("enter the string:");
scanf("%[^\n]s", str);
/* ignores spaces */
for (i = 0; str[i]!= '\0';i++)
{
if (str[i] == ' ')
{
for (j = i;str[j] != '\0'; j++)
{
str[j] = str[j + 1];
}
}
}
/* removes repeated characters */
for (i = 0;str[i]!= '\0';i++)
{
for (j = i + 1;str[j] != '\0';j++)
{
if (str[i] == str[j])
{
for (k = j; str[k] != '\0'; k++)
str[k] = str[k+1];
j--;
}
}
}
/* sorts the string */
for (i = 0; str[i] != '\0'; i++)
{
for (j = 0; str[j] != '\0';j++)
{
if (str[j] > str[i])
{
temp = str[i];
str[i] = str[j];
str[j] = temp;
}
}
}
printf("%s", str);
}
$ cc string15.c $ a.out enter the string:abcde| bcd! abcdefg?? !?abcdefg|
Sanfoundry Global Education & Learning Series – 1000 C Programs.
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:
- Practice BCA MCQs
- Buy Computer Science Books
- Practice Computer Science MCQs
- Watch Advanced C Programming Videos
- Apply for Computer Science Internship