C Program to Sort the String by Removing Whitespaces and Duplicates

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.

  1. /*
  2.  * C Program to sort string ignoring whitespaces and repeating characters only once
  3.  */
  4. #include <stdio.h>
  5. #include <string.h>
  6.  
  7. #define SIZE 50
  8.  
  9. void main()
  10. {
  11.     char string[SIZE], string1[SIZE], string2[SIZE];
  12.     int i, j = 0, a = 0, temp, len = 0, len1 = 0, k = 0;
  13.  
  14.     printf("\nEnter a string:");
  15.     scanf("%[^\n]s", string1);
  16.  
  17.     /* Code to remove whitespaces */
  18.     for (i = 0;string1[i] != '\0';i++)
  19.     {    
  20.         if (string1[i] == ' ')
  21.         {
  22.             continue;
  23.         }
  24.         string[j++] = string1[i];
  25.     }
  26.  
  27.     /* Code to sort the string */
  28.     for (i = 0;string[i] != '\0';i++)
  29.     {
  30.         for (j = i + 1;string[j] != '\0';j++)
  31.         {
  32.             if (string[i] > string[j])
  33.             {
  34.                 temp = string[i];
  35.                 string[i] = string[j];
  36.                 string[j] = temp;
  37.             }
  38.         }
  39.     }
  40.     string[i] = '\0';
  41.     len = strlen(string);
  42.  
  43.     /* Code to remove redundant characters */
  44.     for (i = 0;string[i] != '\0';i++)
  45.     {
  46.         if (string[i] == string[i + 1] && string[i + 1] != '\0')
  47.         {
  48.             k++;
  49.             continue;
  50.         }
  51.         string2[a++] = string[i];
  52.         string[a] = '\0';
  53.     }
  54.     len1 = len - k;
  55.     printf("The sorted string is:");
  56.     for (temp = 0;temp < len1;temp++)
  57.     {
  58.         printf("%c", string2[temp]);
  59.     }
  60. }

$ 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
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.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.