Count Number of Alphabets, Digits, and Special characters in string in C

Program-to-count-number-of-alphabets-digits-and-special-characters-in-string-in-c

Program Description:

Write a Program to Count Number of Alphabets, Digits, and Special characters in string in C programming language. The program should accept a string from the user and count the number of Alphabets, Digits, White spaces, and Other characters/special characters in the given string.

📢 This Program is part of the C String Practice Programs series

Excepted Input and Output:

Input:

Enter the first string : This is @ll Fantasy 2000

Output:

Number of Alphabets: 15

Number of Digits: 4

Number of WhiteSpaces: 4

Number of Other Characters: 1

Prerequisites:

It is recommended to go through the following articles to better understand this program.

Count Number of Alphabets, Digits, and Special characters in a string in C Program Explanation:

  1. Start the program by declaring a string and naming it as inputStr with the size of 100 characters (We are using a global constant to store the SIZE)
  2. Take the input string from the user and update the inputStr string variable.
  3. Initialize the variables to store the total number of Alphabets( nAlpha), Digits( nDigits), White spaces( nWhiteSpaces), and Special Characters( nSpecialChars) with Zeros(0).
  4. The Logic to count the Number of Alphabets, Digits, and Special characters in a String is to Traverse through the string character by character and check if the character is Alphabet, Digit, white space, or special character. We are going to use the functions from the ctype.h header file to verify the characters.
  5. Start a For Loop and Iterate over the string. – for(i = 0; inputStr[i]; i++). At each iteration,
    • Check if the inputStr[i] is an Alphabet:
      • Call the isaplha(inputStr[i]) function with the inputStr[i]. The isalpha() function checks the given character and returns a non-zero value if it is an Alphabet. So If the inputStr[i] is an alphabet, Then Increment the nAlpha variable.
    • Check if the inputStr[i] is a Digit:
      • Call the isdigit(inpuStr[i]) function with the input string element. The isdigit() function checks the given character and returns a non-zero value if the inputStr[i] is a Digit. So increment the nDigits variable if the inputStr[i] is a Digit.
    • Check if the inputStr[i] is a White Space:
      • Similarly, Call the isspace(inputStr[i]) function and increment the nWhitespaces variable if isspace() function returns non-zero value.
    • If none of the above conditions are met, Then the character is a other character / special character. Increment the nSpecialChars variable.
  6. Once the above loop is completed, The nAlpha, nDigits, nWhiteSpaces, and nSpecialChars contains the number of Alphabets, Digits, White space, and Special characters respectively.
  7. Display the results on Console.

Program to Count Number of Alphabets, Digits, and Special characters in string in C:

Here is the C Program to count Alphabets, Digits, Whitespaces, and Special Characters.

📢Don’t forget to include the ctype.h header file

Program Output:

Let’s compile and Run the program using GCC compiler (Any compiler)

Test 1:

count-number-of-alphabets-digits-and-special-characters-in-string-in-c

As we can see from the above output, The input string This is @ll Fantasy 2000. We have 15 Alphabets, 4 Digits, 4 White spaces, and 1 Special character.

Let’s try a few more examples.

Test 2:

In this case, We have 31 Alphabets, Zero digits, 4 Whitespaces, and 1 Special character in Learn Programming at SillyCodes.com input string.

Test 3:

As we can see from the above input and output, The program is properly counting the number of Alphabets, Digits, Whitespaces, and Special Characters in the input string.

Related Programs:

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

1 Response

  1. […] C Program to Count Alphabets, Digits, Whitespaces in String […]

Leave a Reply