1

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;
}
  • 3
    *I searched quite a bit and can't find mych help in C++.* -- [std::all_of](https://en.cppreference.com/w/cpp/algorithm/all_any_none_of). There is no need to write a loop. – PaulMcKenzie Dec 02 '20 at 04:05
  • A hint: Think about what happens the 2nd time through the loop. You are going to assign a value to `alphabetic` no matter what (true or false). And that means that you are going to _overwrite_ whatever happened in the first iteration of the loop...which means that the first character has no influence on the final result. Your code actually only says there is a special character if it is the **last** character. – Wyck Dec 02 '20 at 04:09
  • 2
    Hint -- why do you keep looping once you know there is a non-alpha character in the string? – PaulMcKenzie Dec 02 '20 at 04:09
  • Tip: `std::string` and strongly avoid fixed-length character buffers, *especially* as arguments. – tadman Dec 02 '20 at 04:10
  • We have never used that before, the chapter I am working on is on Arrays and cStrings, I am pretty sure we are supposed to use a loop to get through every character in the array. – time_in_real_time Dec 02 '20 at 04:10
  • @MeowBox Can you state more clearly what you are expecting the code to do? It will look at every character and ... do what? What should it do after it has encountered one alphabetic character and one non-alphabetic character? When it looks at the next character, *why* is it looking? What is it looking for at that point? – David Schwartz Dec 02 '20 at 04:13
  • The typical thing to do is to initialize your flag to `true` **before** the loop, and then only set it to false when you encounter a non-alpha character. Most importantly, don't set it to true again....especially because you don't want to set it to true if you have previously set it to false. – Wyck Dec 02 '20 at 04:13
  • @MeowBox -- You need to specify all of your restrictions up front. We have no idea what your teacher, book, or class is teaching you. – PaulMcKenzie Dec 02 '20 at 04:14
  • @David Schwartz, sorry I didn't even realized I skipped that part lol, I have a program where I enter a string and the function will go through and check if there are special characters and return false if there is a special character. – time_in_real_time Dec 02 '20 at 04:16
  • You don't need to go through every character in the array if you find a non-alpha character before reaching the end. You can exit the loop once any non-alpha character is found, because the result is clear at that point - the entire string does not consist of alpha characters. – Ken White Dec 02 '20 at 04:23
  • @MeowBox -- Assume the string had 1000 characters, and the very first one is not alpha. If you determine that the first is non-alpha, why waste time looping, going through the rest of the string? As mentioned, just return `false;` immediately. – PaulMcKenzie Dec 02 '20 at 04:28
  • @time_in_real_time So once do you see that there is a special character, nothing you see in any subsequent character should have any effect on the value you return. – David Schwartz Dec 02 '20 at 06:38
  • The definition of "special character" varies a bit, but typically includes characters that are neither alphabetic nor numeric. It generally doesn't just mean non-alphabetic. – Peter Dec 02 '20 at 06:49

4 Answers4

2

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;
}
An0n1m1ty
  • 448
  • 4
  • 17
1

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));
}
Picaud Vincent
  • 10,518
  • 5
  • 31
  • 70
  • I'm not sure what your getting at about coding C++ like you would in C. We have never used the string class yet and it does not come up until next term, for now it seems like the way I did it is the only way that I could possibly know. – time_in_real_time Dec 02 '20 at 04:35
  • Ok, I see what you mean now, thanks for all your help! – time_in_real_time Dec 02 '20 at 04:38
  • @MeowBox C++ is a complex language and there is a lot to learn. I just wanted to point out that coding in C++ with a C style can have bad side effects (e.g. raw pointer versus smart pointer, but I guess that you will see that later). Good learning and good luck! – Picaud Vincent Dec 02 '20 at 04:40
  • @PicaudVincent in the original code, the return result for an empty string was passed into the function as the `alphabetic` parameter, whereas you've modified the behaviour to return `true` for an empty string. This is fundamentally different. Yours is unsurprising, and logical -- but behaves differently in that one way. (I suspect that the original behaviour was likely unintended.) – Wyck Dec 02 '20 at 04:41
  • @Wyck you are absolutly right I missread the original code! Hold on I am going to fix that! – Picaud Vincent Dec 02 '20 at 04:43
  • I am amazed at your abilities to write code so quickly haha – time_in_real_time Dec 02 '20 at 04:50
  • @MeowBox That because I tried to give you a nice answer with compiling codes :) – Picaud Vincent Dec 02 '20 at 04:53
1

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

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?

CKE
  • 1,533
  • 19
  • 18
  • 29
  • 1
    @time_in_real_time: You're welcome. Don't forget to upvote, if you find this useful. – CKE Dec 02 '20 at 06:47