Removing all characters from a string except alphabets is a common text processing task. This can be useful in various applications such as data sanitization, preprocessing for text analysis, and more. This article presents three different C Program to Remove All Characters in a String Except Alphabets, complete with explanations, example code, and outputs.
Prerequisites
Before diving into the solutions, ensure you have the following prerequisites:
- Basic understanding of C programming.
- Familiarity with strings, arrays, and loops.
- A C compiler installed on your system (e.g., GCC).
Solution 1: Using a Temporary Array
1.1 Explanation
In this approach, we use a temporary array to store only the alphabet characters from the original string. We then copy the filtered characters back to the original string.
1.2 Program
#include <stdio.h>
#include <ctype.h>
void removeNonAlphabets(char *str) {
char temp[100];
int j = 0;
for (int i = 0; str[i] != '\0'; i++) {
if (isalpha(str[i])) {
temp[j++] = str[i];
}
}
temp[j] = '\0';
strcpy(str, temp);
}
int main() {
char str[100];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
str[strcspn(str, "\n")] = '\0'; // Remove newline character
removeNonAlphabets(str);
printf("String after removing non-alphabets: %s\n", str);
return 0;
}
1.3 Output
Enter a string: Hello, World! 123
String after removing non-alphabets: HelloWorld
Solution 2: In-Place Removal
2.1 Explanation
This approach removes non-alphabet characters in-place without using any extra array. It shifts the alphabet characters to the left as it encounters them.
2.2 Program
#include <stdio.h>
#include <ctype.h>
void removeNonAlphabets(char *str) {
int i = 0, j = 0;
while (str[i]) {
if (isalpha(str[i])) {
str[j++] = str[i];
}
i++;
}
str[j] = '\0';
}
int main() {
char str[100];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
str[strcspn(str, "\n")] = '\0'; // Remove newline character
removeNonAlphabets(str);
printf("String after removing non-alphabets: %s\n", str);
return 0;
}
Output
Enter a string: C Programming 101!
String after removing non-alphabets: CProgramming
Solution 3: Using Pointers
3.1 Explanation
In this approach, we use pointers to traverse the string and filter out non-alphabet characters. This method is efficient and can be more readable for those familiar with pointer manipulation in C.
3.2 Program
#include <stdio.h>
#include <ctype.h>
void removeNonAlphabets(char *str) {
char *src = str, *dst = str;
while (*src) {
if (isalpha(*src)) {
*dst++ = *src;
}
src++;
}
*dst = '\0';
}
int main() {
char str[100];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
str[strcspn(str, "\n")] = '\0'; // Remove newline character
removeNonAlphabets(str);
printf("String after removing non-alphabets: %s\n", str);
return 0;
}
Output
Enter a string: Data! Science@2024
String after removing non-alphabets: DataScience
Conclusion
Removing non-alphabet characters from a string is a common text processing task that can be approached in several ways in C programming. This article demonstrated three different methods: using a temporary array, in-place removal, and using pointers. Each method has its advantages and can be chosen based on the specific requirements and constraints of your application. Understanding and experimenting with these techniques will enhance your text processing capabilities and improve your overall programming skills in C.