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

1.8k Views Asked by At

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

There are 4 best solutions below

1
On BEST ANSWER

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?

7
On

You have two kind of problems:

  1. a logic related one
  2. a C++ related one

The logic is:

(1) is alpha string <=> all chars are alpha

the contraposition

(2) is not alpha string <=> it exists at least one non alpha char

hence the code is something like:

For all char c in string
   if c is not char return false    <--- (2 in action)
End for

return true <--- (1 in action)

You have to choose between C or C++. Please do not use C++ to code like in C.

If you want to learn C++ the site https://en.cppreference.com/w/ is a great source of information.

A possible C++ solution is as follows:

#include <string>
#include <iostream>

bool isAlphaStr(const std::string& to_check)
{
  for(auto c:to_check) 
    if(!std::isalpha(c)) return false;
  
  return true;
}

int main()
{
 char string_1[]="Hello world!";
 std::string string_2{"Hello"};

  std::cout << "\nIs alpha? " << std::boolalpha << isAlphaStr(string_1);
  std::cout << "\nIs alpha? " << std::boolalpha << isAlphaStr(string_2);
}

To compare C++ style versus C style I have added a pure C version:

#include <string.h>
#include <ctype.h> // for isalpha
#include <stdio.h>
#include <stdbool.h>
 
bool isAlphaStr(const char *const to_check)
{
  const size_t n = strlen(to_check);
  for(size_t i=0;i<n;++i) 
    if(!isalpha(to_check[i])) return false;
  
  return true;
}

int main()
{
 char string_1[]="Hello world!";
 char string_2[]="Hello";

 printf("\nIs alpha? %d", isAlphaStr(string_1));
 printf("\nIs alpha? %d", isAlphaStr(string_2));
}

Regarding to Wyck comment, here is version with the bool alphabetic variable:

C++:

#include <string>
#include <iostream>
#include <type_traits>

bool isAlphaStr(const std::string& to_check, bool alphabetic)
{
  if(to_check.empty()) return alphabetic;
  
  for(auto c:to_check) 
    if(!std::isalpha(c)) return false;
  
  return true;
}

int main()
{
 char string_1[]="Hello world!";
 std::string string_2{"Hello"};

 std::cout << "\nIs alpha? " << std::boolalpha << isAlphaStr(string_1,false);
 std::cout << "\nIs alpha? " << std::boolalpha << isAlphaStr(string_2,false);
}

C:

#include <stdio.h>
#include <stdbool.h>
 
bool isAlphaStr(const char *const to_check, bool alphabetic)
{
  const size_t n = strlen(to_check);

  if(!n) return alphabetic; // empty string special case
  
  for(size_t i=0;i<n;++i) 
    if(!isalpha(to_check[i])) return false;
  
  return true;
}

int main()
{
 char string_1[]="Hello world!";
 char string_2[]="Hello";

 printf("\nIs alpha? %d", isAlphaStr(string_1,false));
 printf("\nIs alpha? %d", isAlphaStr(string_2,false));
}
0
On

Try the following:

bool IsAllSpecialCharacters(char wordcheck[25], bool alphabetic)
{
    int i = 0;
    int n = strlen(wordcheck)
    for (int i = 0; i < n; i++)
    {   
        if (isalpha(wordcheck[i])) return false
    }
    return true;
}
0
On

Thanks for the help everyone, the following code seems to work well. I had my variable alphabetic declared as false and I changed it to true and then deleted the else statement that would change it back to true from false. Heres the code,

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

    return alphabetic;
}