Saturday 11 November 2017

C Program to Sort the String and Repeated Characters should be present only Once


Code:

#include
#include

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);
}


Output:

enter the string:abcde| bcd! abcdefg??
!?abcdefg|


More C Programs














100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...