Pretty new to coding here. We are working with the isalpha function and I am having trouble using it with a string. My program will prompt the user for a word and then the function will check if the word contains any special characters. Basically, my code will only say there is a special character if they are all special character, not if there is just a couple. I am assuming is has something to do with my for loop but i cannot figure out how to get it to work. I searched quite a bit and can't find much help in C++.
Here is my function. Any help is appreciated.
//*****IsAlphaStr*****
//This function returns true if the cString passed contains all alphabetic characters.
//If the parameter does not contain all alpha characters, a value of false is returned.
bool IsAlphaStr(char wordcheck[25], bool alphabetic)
{
int i = 0;
int n = 0;
for (int i = 0, n = strlen(wordcheck); i < n; i++)
{
if (isalpha(wordcheck[i]) == 0)
alphabetic = false;
else
alphabetic = true;
}
return alphabetic;
}
As mentioned,
IsAlphaStr
shall only returntrue
if all the given characters are alphabetic. This can be achieved by adding abreak
in thefalse
branch of theif
condition, which stops the further execution of thefor
loop.The whole test program is:
The output is:
Hope it helps?