This C Program sort string ignoring whitespaces and repeating characters only once.
Here is source code of the C Program to sort string ignoring whitespaces and repeating characters 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 string ignoring whitespaces and repeating characters only once
*/
#include <stdio.h>
#include <string.h>
#define SIZE 50
void main()
{
char string[SIZE], string1[SIZE], string2[SIZE];
int i, j = 0, a = 0, temp, len = 0, len1 = 0, k = 0;
printf("\nEnter a string:");
scanf("%[^\n]s", string1);
/* Code to remove whitespaces */
for (i = 0;string1[i] != '\0';i++)
{
if (string1[i] == ' ')
{
continue;
}
string[j++] = string1[i];
}
/* Code to sort the string */
for (i = 0;string[i] != '\0';i++)
{
for (j = i + 1;string[j] != '\0';j++)
{
if (string[i] > string[j])
{
temp = string[i];
string[i] = string[j];
string[j] = temp;
}
}
}
string[i] = '\0';
len = strlen(string);
/* Code to remove redundant characters */
for (i = 0;string[i] != '\0';i++)
{
if (string[i] == string[i + 1] && string[i + 1] != '\0')
{
k++;
continue;
}
string2[a++] = string[i];
string[a] = '\0';
}
len1 = len - k;
printf("The sorted string is:");
for (temp = 0;temp < len1;temp++)
{
printf("%c", string2[temp]);
}
}
$ cc string99.c
$ a.out
Enter a string:abcdel bcdl abcdefg
The sorted string is:abcdefgl
Sanfoundry Global Education & Learning Series – 1000 C Programs.
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.
Related Posts:
- Check C Books
- Apply for Computer Science Internship
- Practice Computer Science MCQs
- Apply for C Internship
- Practice BCA MCQs