English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Comprehensive Collection of C Programming Examples
In this example, you will learn how to remove all characters except letters from the string entered by the user.
To understand this example, you should understand the followingC language programmingTopic:
#include <stdio.h> int main() { char line[150]; printf("Enter a string: "); fgets(line, sizeof(line), stdin); //Accept input for (int i = 0, j; line[i] != '\0'; ++i) { //If the character is not a letter, then enter the loop //It is not a 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 the jth element of line is not a letter //to the element at index j + 1assign the value of the element at index j line[j] = line[j + 1]; } line[j] = '\0'; } } printf("Output string: "); puts(line); return 0; }
Output result
Enter a string: n2'h-o@84oo./ Output string: w3codebox
This program takes a string input from the user and stores it in the line variable. Then, it uses a for loop to traverse the characters of the string.
If the character in the string is not a letter, it will be removed from the string, and the remaining characters will be moved to the left.1positions.