C Programming: Remove characters in String Except Alphabets
Write a program in C to remove characters from a string except alphabets.
Sample Solution:
C Code:
#include <stdio.h>
#include <string.h>
int main() {
char str[150]; // Declare a character array to store the string
int i, j; // Declare variables for iteration
printf("\n\nRemove characters in String Except Alphabets :\n"); // Display information about the task
printf("--------------------------------------------------\n");
printf("Input the string : ");
fgets(str, sizeof str, stdin); // Read a string from the standard input (keyboard)
for (i = 0; str[i] != '\0'; ++i) {
while (!((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z' || str[i] == '\0'))){
// Loop to remove non-alphabetic characters from the string
for (j = i; str[j] != '\0'; ++j) {
str[j] = str[j + 1]; // Shift characters to the left to remove non-alphabetic characters
}
str[j] = '\0'; // Set the end of the string after removal
}
}
printf("After removing the Output String : %s\n\n", str); // Display the modified string after removal
return 0; // Return 0 to indicate successful execution of the program
}
Sample Output:
Remove characters in String Except Alphabets : -------------------------------------------------- Input the string : W3resource.com After removing the Output String : Wresourcecom
Flowchart :
C Programming Code Editor:
Improve this sample solution and post your code through Disqus.
Previous: Write a program in C to find the number of times a given word 'the' appears in the given string.
Next: Write a program in C to Find the Frequency of Characters.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics