Skip to main content

Featured

C Pattern Programs: Square Number and Alphabet Patterns Explained

πŸ”· Square Star Pattern πŸ“‹ Copy Code #include <stdio.h> int main() { int num; printf("Enter the number:\n"); scanf("%d", &num); for(int i = 1; i <= num; i++) { for(int j = 1; j <= num; j++) { printf("* ");//keep"* " } printf("\n"); } return 0; } πŸ”· Reverse Square Alphabet Pattern (Column-wise) πŸ“‹ Copy Code #include <stdio.h> int main() { int num; printf("Enter the number:\n"); scanf("%d", &num); for(int i = num; i >= 1; i--) { for(int j = num; j >= 1; j--) { printf("%c ", j + 64);//%c for Character and 64 will be ASIIC VALUE } printf("\n"); } return 0; } πŸ”· Reverse Square Alphabet Pattern (Row-wise) πŸ“‹ Copy Code #include <stdio.h> int main() { int num; ...

C Program to Remove Duplicates, Ignore Spaces, and Sort Characters in Descending Order

Remove Duplicates and Sort Characters in Descending Order - C Program

πŸš€ C Program to Remove Duplicates, Ignore Spaces, and Sort Characters in Descending Order

πŸ“„ Source Code:


#include<stdio.h>
#include<string.h>

int present(char a[], char ch, int len)
{
    for (int i = 0; i < len; i++)
    {
        if (a[i] == ch)
        {
            return 1;
        }
    }
    return 0;
}

void des(char a[], int len)
{
    for (int i = 0; i < len - 1; i++)
    {
        for (int j = i + 1; j < len; j++)
        {
            if (a[i] < a[j])
            {
                char temp = a[i];
                a[i] = a[j];
                a[j] = temp;
            }
        }
    }
}

int main()
{
    char str[200], result[200];
    int j = 0;
    fgets(str, sizeof(str), stdin);
    int len = strlen(str);
    if (str[len - 1] == '\n')
    {
        str[len - 1] = '\0';
    }

    for (int i = 0; str[i] != '\0'; i++)
    {
        if (str[i] != ' ' && !present(result, str[i], j))
        {
            result[j++] = str[i];
        }
    }

    des(result, j);
    result[j] = '\0';

    printf("Resulting string: %s\n", result);
}
    

πŸ“˜ Deep Explanation:

This C program performs three core operations on a string entered by the user:

  • πŸ”Ή 1. It removes duplicate characters from the string.
  • πŸ”Ή 2. It ignores spaces.
  • πŸ”Ή 3. It sorts the unique characters in descending order.
  • πŸ”Έ present() checks if a character is already in the result array.
  • πŸ”Έ des() uses a simple bubble sort in descending (Z to A) order.
  • πŸ”Έ fgets() is used instead of scanf() to read spaces.
  • πŸ”Έ Only unique characters are printed after sorting.

πŸ§ͺ Sample Output:


Input:
Hello World

Output:
Resulting string: roledWH
    

Comments

Popular Posts

πŸŒ™