C program to remove all Characters in a String Except Alphabet – When given a problem statement before directly jumping into solution, we need to first understand the problem statement thoroughly and then, also note down if any constraints are mentioned. Next, we have to determine the inputs necessary to arrive at a solution along with the output to expect at the end.
Later, we come up with the logical solution to the given problem statement so that, we can get our desired output using the inputs.
The given problem statement is, to write a C program to remove all characters except alphabets in a given string. To do so initially we require a string whose non-alphabetic characters are to be removed as our input. In the end, the output we desire is also a string where, there are only alphabets from the input string.
So firstly, we have to read our input string (c) at runtime. To do so, we can use gets() function which reads entire line input and stores it in a string pointer or character array. This reads input until it reaches either a newline or end-of-file (EOF).
char c[150];
printf(“Enter a string : “);
gets(c);
Now, we traverse through the character array (String) until we reach the end (‘\0’) in a loop. If a character is an alphabet then, it can either be uppercase or lowercase. Uppercase alphabets have ASCII values in the range 65-90 (‘A’-‘Z’) while, lowercase alphabets are in the range 97-122(‘a’-‘z’). If a character is in either one of these ranges then, we retain it in the character array (c) at index j (initially zero) and increment the value by 1.
The same happens to every character in the given string or character array. Once we reach the end of iteration, at the last index j we place the null value or end value i.e.,’\0′, marking the end of string.
for(i = 0,j=0;c[i] != ‘\0’; ++i) {
if((c[i]>=65 && c[i]<=90)||(c[i]>=97 && c[i]<=122)) {
c[j++]=c[i];
}
}
c[j]=’\0′;
This last value after the end of iteration in the string or character array (c) is nothing but, our resultant output string with no other characters except for the alphabets for a given input string. This value is then displayed on the console screen using printf().
printf(“String after removal : %s”,c);
Remove All Characters in A String Except Alphabets
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#include <stdio.h> #include <string.h> int main() { char c[150]; int i, j; printf("Enter a string : "); gets(c); for(i = 0,j=0;c[i] != '\0'; ++i) { if((c[i]>=65 && c[i]<=90)||(c[i]>=97 && c[i]<=122)) { c[j++]=c[i]; } } c[j]='\0'; printf("String after removal : %s",c); return 0; } |
Output:
1 2 |
Enter a string : hello world9 welcome87--- to c 77programming String after removal : helloworldwelcometocprogramming |
Using Function
As we can observe above, the entire code is within main method. When we write the code that way, we can not make it reusable and also the code is not quite readable. Instead, if we use functions and split code accordingly then we can reuse it and enhance the readability.
This way, if the same logic is required somewhere else in the code then, we can just make function call and make it reusable. Also, code is more clean and if we need to find a particular part of code, we can directly go to the respective function instead of searching the entire main method.
Here, the main method will read the input string (c) using gets() function. Then, it calls the user-defined function (Remove) and passes the input string as parameter. This user-defined function (Remove) uses the same logic as discussed above to remove all the non-alphabetic characters in the parameter passed.
Then, this newly updated string that is our desired output is displayed on the console screen. Therefore, the main method handles all the necessary i/o operations for this problem statement while, the user-defined function (Remove) has the logic for removing non-alphabetic character.
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 |
#include <stdio.h> #include <string.h> int Remove(char *c) { int i, j; for(i = 0,j=0;c[i] != '\0'; ++i) { if((c[i]>=65 && c[i]<=90)||(c[i]>=97 && c[i]<=122)) { c[j++]=c[i]; } } c[j]='\0'; } int main() { char c[150]; printf("Enter a string : "); gets(c); Remove(c); printf("String after removal : %s",c); return 0; } |
Output:
1 2 |
Enter a string : java92programming String after removal : javaprogramming |