C Program to Sort the String and Remove Repeated Characters

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.

  1. /* 
  2.  * C Program to Sort the String(ignore spaces) and Repeated  
  3.  * Characters should be present only Once  
  4.  */
  5. #include <stdio.h>
  6. #include <string.h>
  7.  
  8. void main()
  9. {
  10.     int i, j = 0, k = 0;
  11.     char str[100], str1[10][20], temp, min;
  12.  
  13.     printf("enter the string:");
  14.     scanf("%[^\n]s", str);
  15.  
  16. /* ignores spaces */
  17.     for (i = 0; str[i]!= '\0';i++)
  18.     {
  19.         if (str[i] == ' ')
  20.         {
  21.             for (j = i;str[j] != '\0'; j++)
  22.             {
  23.                 str[j] = str[j + 1];
  24.             }
  25.         }
  26.     }
  27.  
  28. /* removes repeated characters */
  29.     for (i = 0;str[i]!= '\0';i++)
  30.     {
  31.         for (j = i + 1;str[j] != '\0';j++)
  32.         {
  33.             if (str[i] == str[j])
  34.             {
  35.                 for (k = j; str[k] != '\0'; k++)
  36.                 str[k] = str[k+1];
  37.                 j--;
  38.             }
  39.         }
  40.     }
  41.  
  42. /* sorts the string */
  43.     for (i = 0; str[i] != '\0'; i++) 
  44.     {
  45.         for (j = 0; str[j] != '\0';j++)
  46.         {
  47.             if (str[j] > str[i])
  48.             {
  49.                 temp = str[i];
  50.                 str[i] = str[j];
  51.                 str[j] = temp;
  52.             }
  53.         }
  54.     }
  55.     printf("%s", str);
  56. }

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

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.