Code to check if characters on string are alphabetic

I need to write a program that will tell me if all the characters in a string are alphabetic or not with a return bool giving me "true" if they are alphabetic and "false" if they are not.

I have a prototype code, but I have a strong feeling this isn't going to work. Could anyone help me with this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <cmath>
#include <string>
#include <cctype>
#include <sstream>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::stringstream;

bool CheckAlphabetic(string letter) {
  if(letter=='A'){
    if(letter=='B'){
      if(letter=='C'){
        if(letter=='D'){
          if(letter=='E'){
            if(letter=='F'){
              if(letter=='G'){
                if(letter=='H'){
                  if(letter=='I'){
                    if(letter=='J'){
                      if(letter=='K'){
                        if(letter=='L'){
                          if(letter=='M'){
                            if(letter=='N'){
                              if(letter=='O'){
                                if(letter=='P'){
                                  if(letter=='Q'){
                                    if(letter=='R'){
                                      if(letter=='S'){
                                        if(letter=='T'){
                                          if(letter=='U'){
                                            if(letter=='V'){
                                              if(letter=='W'){
                                                if(letter=='X'){
                                                  if(letter=='Y'){
                                                    if(letter=='Z'){ 
                                                  }
                                                }
                                              }
                                            }
                                          }
                                        }
                                      }
                                    }
                                  }
                                }
                              }
                            }
                           }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }     
        } 
      }
    }
  }
  return true;
}else{
  return false;
}
}
Last edited on
1. Your code is syntactically incorrect. It won't compile because strings aren't comparable against characters. Use double quotes to enclose a string.

2. Where is the string to be checked? If I don't misunderstand your intention, then the string letter should contain only one letter.

You should better loop over the string containing the word to be checked, testing it letter by letter. A for-loop isn't a bad choice.(See References of this web page)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
bool isAlphabetic(string check)
{
	bool verify = true;
	for (int i = 0; i < check.length(); i++)
	{
		if (check[i] >= 'a' && check[i] <= 'z' || check[i] >= 'A' && check[i] <= 'Z')
			verify = true;
		else
		{
			verify = false;
			break;
		}
	}
	return verify;
}


This is a very simple code to do this, there are countless other ways. This method won't read in any spaces. Only checks for both capital & lowercase letters.
that isnt a prototype code.

you can use looping for alphabet instead of doing it manually with if statements
I tried compiling the offered code, but it came up with some problems. I *think* I fixed one of them but I'm still coming up with problems.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
bool isAlphabetic(string check)
{
	bool verify = true;
	for (int i = 0; i << check.length(); i++)
	{
		if (check[i] >= 'a' && check[i] <= 'z' || check[i] >= 'A' && check[i] <= 'Z')
			verify = true;
		else
		{
			verify = false;
			break;
		}
	}
	return verify;
}


When compiling, I get this:

$ make
/usr/bin/g++ -Wall -Wextra -Werror -pedantic -g assignment_1.cpp -o assignment_1
assignment_1.cpp: In function ‘bool isAlphabetic(std::string)’:
assignment_1.cpp:26:23: error: suggest parentheses around ‘&&’ within ‘||’ [-Werror=parentheses]
if (check[i] >= 'a' && check[i] <= 'z' || check[i] >= 'A' && check[i] <= 'Z')
^
cc1plus: all warnings being treated as errors
makefile:10: recipe for target 'assignment_1' failed
make: *** [assignment_1] Error 1


But I'm not sure what it means...
Last edited on
You are telling the compiler to treat warnings as errors with -Werror; you should not use that flag if you don't want to make the suggested change.
> but it came up with some problems
¿is it so hard to say what the problems were?

> for (int i = 0; i << check.length(); i++)
`<<' is the bitshift operator


http://www.cplusplus.com/reference/locale/isalpha/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <string>
#include <cctype>
#include <algorithm>

bool all_alpha( const std::string& str )
{
    // // http://www.stroustrup.com/C++11FAQ.html#for
    // http://en.cppreference.com/w/cpp/string/byte/isalpha
    for( char c : str ) if( !std::isalpha(c) ) return false ;

    return true ;
}

bool all_alpha2( const std::string& str )
{
    // http://en.cppreference.com/w/cpp/algorithm/all_any_none_of
    return std::all_of( std::begin(str), std::end(str),
                        []( char c ) { return std::isalpha(c) ; } ) ;
}

bool all_alpha3( const std::string& str ) // legacy C++
{
    for( std::size_t i = 0 ; i < str.size() ; ++i )
        if( !std::isalpha( str[i] ) ) return false ;

    return true ;
}
Zhuge

I've been instructed to treat all warnings as errors and remove them all, so I had to set it that way.

ne555

I'm sorry if I wasn't clear. The italicized text is what the compiler said what wrong, which is as close as I can get to explaining the problem.
Add flag -std=c++11 for g++. If your version of g++ does not accept it, then check from man gcc what values the -std option can have.

The thing is that the GCC's default is to support rather old version of C++ standard but with some non-standard GNU extensions. It should be better for learning to stick to latest standard, if at all possible.
I think I got it now. Thanks for the help!
Topic archived. No new replies allowed.