In this tutorial you will learn about the C Program to Remove all Characters in a String Except Alphabet and its application with practical example.
C Program to Remove all Characters in a String Except Alphabet
In this tutorial, we will learn to create a C program that will Remove all Characters in a String Except Alphabet in C programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following C programming topics:
- Operators in C Programming.
- Basic Input and Output function in C Programming.
- Basic C programming.
- For loop in c programming.
- While loop in c programming.
Remove all Characters in a String Except Alphabet.
As we all know the String is a collection of character data types. In strings, only one variable is declared which can store multiple values. First will take the string from the user. Then will pass that string to a for loop to filter the numbers from that string.
With the help of this program, we can Remove all Characters in a String Except Alphabet.
Algorithm:-
1 2 3 4 5 6 7 8 9 10 11 |
1. Declare the variables. 2. Take the input string from the user. 3. Pass that string type variable to a loop. 4. matching the number value if there is a value then will remove it. 5. Print the string without numbers. 6. End the program. |
Program to Remove all Characters in a String Except Alphabet:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
#include <stdio.h> int main() { // declaring local variable for the program. char line[150]; //Taking input string from the user. printf("Enter a string: "); //Scanning the string from the user. fgets(line, sizeof(line), stdin); //Extracting numbers from the code. for (int i = 0, j; line[i] != '\0'; ++i) { // enter the loop if the character is not an alphabet // and not the null character while (!(line[i] >= 'a' && line[i] <= 'z') && !(line[i] >= 'A' && line[i] <= 'Z') && !(line[i] == '\0')) { for (j = i; line[j] != '\0'; ++j) { // if j is not alphabet, // assign the value of (j+1)th element to the jth element line[j] = line[j + 1]; } line[j] = '\0'; } } //printing output printf("Output String: "); puts(line); return 0; } |
Output:-
In the above program, we have first initialized the required variable.
- line[150] = it will hold the string value.
- i = it will hold the integer value for the loop.
- j = it will hold the integer value for the loop.
Taking input string from the user.
Scanning the input string from the user.
Storing the string and extracting only string data by for loop.
Printing output of the program.