Write a function countAlphaChars to count the number of alphabetic characters ar
ID: 3888108 • Letter: W
Question
Write a function countAlphaChars to count the number of alphabetic characters are in a string. You will receive an input parameter of type string called str and you should return an integer value for the number of alphabetic characters in the string. An alphabetic character is a lowercase or uppercase character present in the alphabet: a-z and A-Z. For your reference, here is a function countNumericChars that counts the number of numeric (0-9) characters in a string. int countNumericChars(string str)f int i = 0; int numericCharacters = 0; while (i = .0' && str[1]Explanation / Answer
The function is given below countAlphaCharacters, it takes input as a string and returns number of alphabets in the given string. The alphabets can be either capital letters (A-Z) or small letters (a-z). The below function scans each and every input character in the given string and checks whether it is an alphabet or not by checking the ASCII value of each character. For alphabets the ASCII value of string should be between either 65 to 90 or 97 to 122. If it is an alphabet then it adds 1 to the value of alphaCharacters variable.
Example:
Consider a string "Chegg123"
the function returns output 5 since there are 5 alphabets (C, h, e, g, g)
Note : The text mentioned between /* .... */ are comments
Function:
int countAlphaCharacters(string str)
{
int i = 0;
int alphaCharacters = 0;
while( i < str.length())
{
/*Condition to test if either A-Z (capital letters) or a-z (small letters) are present in a given string
'A' value is 65
'Z' value is 90
'a' value is 97
'z' value is 122 */
if( (str[i] >= 'A' && str[i] <= 'Z') || (str[i] >= 'a' && str[i] <= 'z'))
{
alphaCharacters++;
}
i++;
}
return alphaCharacters;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.