---
title: "Check string consists of alphabet only"
subject: "Computer Programming"
chapter: 7
key: "semester_1/computer_programming:string-alphabet-check"
source: https://www.bctengineer.com/q/semester_1/computer_programming/string-alphabet-check
site: BCT Engineering
---

# Check string consists of alphabet only

Computer Programming · Chapter 7: Arrays and Strings

- Asked in: 2073 Chaitra, 5 marks
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/string-alphabet-check
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-7

## Question

Write a program to read a string and check whether it consists of Alphabet or not. Use user defined function to accomplish the task.

## Short answer

isAllAlpha() loops through string; if any character is not a letter (via isalpha()), returns0. If all are letters and not empty, returns1. main() reads string and prints appropriate message.

## Answer

**Logic.** The function `isAllAlpha()` visits each character of the string. If any character is not a letter (`isalpha()` from `<ctype.h>` fails, e.g. a digit, space or symbol), it returns 0 at once. If the loop finishes, all characters are letters and it returns 1. An empty string is treated as not valid.

```c
#include <stdio.h>
#include <ctype.h>

/* returns 1 if every character is a letter, else 0 */
int isAllAlpha(char s[])
{
    if (s[0] == '\0')
        return 0;
    for (int i = 0; s[i] != '\0'; i++)
        if (!isalpha((unsigned char)s[i]))
            return 0;
    return 1;
}

int main(void)
{
    char str[100];
    printf("Enter a string: ");
    scanf("%99s", str);
    if (isAllAlpha(str))
        printf("\"%s\" consists of alphabets only\n", str);
    else
        printf("\"%s\" does not consist of alphabets only\n", str);
    return 0;
}
```

**Sample run** (the digit 2 makes the check fail)

Input: `Pulchowk2`

```text
Enter a string: "Pulchowk2" does not consist of alphabets only
```

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. How does isAllAlpha check the string?
   A. Compares with the first letter
   B. Counts spaces
   C. Uses strlen only
   D. Visits each character and fails if isalpha is false
   Answer: D. Any non-letter returns 0 immediately. (Key point: Check method)

2. Which header provides isalpha()?
   A. <string.h>
   B. <ctype.h>
   C. <stdio.h>
   D. <math.h>
   Answer: B. Character classification functions are in ctype.h. (Key point: Header)

3. What is the result for Pulchowk2?
   A. All alphabets
   B. Error
   C. Only the 2 is alphabet
   D. Not all alphabets, because of the digit 2
   Answer: D. The digit fails isalpha. (Key point: Result of Pulchowk2)

## Related in Computer Programming

- [Switch versus user defined function](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aswitch-vs-user-function) — Computer Programming, chapter 5
- [Palindrome string using function](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apalindrome-string-function) — Computer Programming, chapter 7
- [Significance of user defined functions](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Asignificance-user-defined-functions) — Computer Programming, chapter 6
- [nth Fibonacci term prime check](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Afibonacci-nth-prime) — Computer Programming, chapter 6
- [Copy string converting lower to upper using pointer](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Apointer-array-elements-copy-string-upper) — Computer Programming, chapter 9, asked 3×
- [Reverse a string using functions](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Astring-reverse-with-count-function) — Computer Programming, chapter 7, asked 2×

## Related in other subjects

- [Convert user-defined type to basic type](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Auser-to-basic-conversion) — Object Oriented Programming, chapter 5
- [T/F: tower height function of voltage only](https://www.bctengineer.com/question.md?key=semester_8%2Ftransmission_and_distribution_system_design%3Atf-tower-height-voltage-only) — Transmission and Distribution System Design, chapter 4, asked 2×
- [Voltage across string discs is not uniform](https://www.bctengineer.com/question.md?key=semester_4%2Fpower_system_analysis_i%3Astring-voltage-not-uniform) — Power System Analysis I, chapter 2, asked 3×
- [ZOH and FOH defined; ZOH transfer function](https://www.bctengineer.com/question.md?key=semester_6%2Fdigital_control_system%3Azoh-foh-definition) — Digital Control System, chapter 1
