c++isalpha

How do I check if there is a special character in a string in C++ using isalpha?


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;
}

Solution

  • As mentioned, IsAlphaStr shall only return true if all the given characters are alphabetic. This can be achieved by adding a break in the false branch of the if condition, which stops the further execution of the for loop.

            if (isalpha(wordcheck[i]) == 0)
            {
                alphabetic = false;
                break;
            }
    

    The whole test program is:

    #include <iostream>
    
    //*****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;
                break;
            }
            else
                alphabetic = true;
        }
        return alphabetic;
    }
    
    int main()
    {
        char test1[25] = "abcdefghijklmnopqrstuvwx";
        char test2[25] = "0123456789ABCDEFGHIJKLMN";
        char test3[25] = "abcdefghijklmnopqres++-A";
        char test4[25] = "abcdefABCDEF";
        bool alphabetic = false;
    
        alphabetic = IsAlphaStr(test1, alphabetic);
        std::cout << "test1 = " << alphabetic << std::endl;
        alphabetic = IsAlphaStr(test2, alphabetic);
        std::cout << "test2 = " << alphabetic << std::endl;
        alphabetic = IsAlphaStr(test3, alphabetic);
        std::cout << "test3 = " << alphabetic << std::endl;
        alphabetic = IsAlphaStr(test4, alphabetic);
        std::cout << "test4 = " << alphabetic << std::endl;
    
        return 0;
    }
    

    The output is:

    test1 = 1
    test2 = 0
    test3 = 0
    test4 = 1
    

    Hope it helps?