C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Strings in C

Remove special characters from a string

C Program: Remove special characters from a string

C

#include <stdio.h>

 

int main() {

    char str[200], result[200];

    int i, j = 0;

 

    // Input string

    printf("Enter a string: ");

    fgets(str, sizeof(str), stdin);

 

    // Remove special characters

    for (i = 0; str[i] != '\0'; i++) {

        // Keep only alphabets, digits, and spaces

        if ((str[i] >= 'A' && str[i] <= 'Z') ||

            (str[i] >= 'a' && str[i] <= 'z') ||

            (str[i] >= '0' && str[i] <= '9') ||

            str[i] == ' ' ||

            str[i] == '\n') {

            result[j++] = str[i];

        }

    }

 

    result[j] = '\0'; // Null terminate the result string

 

    printf("String after removing special characters: %s", result);

 

    return 0;

}

Output

 
OUTPUT :
Enter a string: IT@Developer#2025!
String after removing special characters: ITDeveloper2025

Explanation

  • The program reads a string using fgets().
  • It checks every character:
    • Keeps only alphabets, digits, and spaces.
    • Skips punctuation marks and special symbols like @, #, $, etc.
  • The cleaned string is stored in result and displayed.