Print Only Alphabets In C

A string S is passed as the input. S can contain alphabets, numbers and special characters. The program must print only the alphabets in S.

Input Format:
The first line contains S.

Output Format:
The first line contains only the alphabets in S.

Boundary Conditions:
The length of the input string is between 1 to 1000.

Example Input/Output 1:
Input:
abcd_5ef8!xyz

Output:
abcdefxyz

Example Input/Output 2:
Input:
1239_-87

Output:

Explanation:
As there are no alphabets in the input value nothing is printed as output.

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main() {
    char s[100]; // Assuming the input will not exceed 99 characters plus null terminator
    printf("Enter a string: ");
    fgets(s, sizeof(s), stdin);
    s[strcspn(s, "n")] = 0; // Remove newline character

    for(int i = 0; s[i] != ''; i++) {
        if(isalpha(s[i])) {
            printf("%c", s[i]);
        }
    }
    printf("n");
    return 0;
}

Previous Article

Vertical String Pattern

Next Article

Sum - Alphabet Separated Values

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *