each character in a string is a letter

Hi guys!
How do I check if each character in a string is a letter ?
I can use ASCII code but I don't know...Is there another way to do this?
I want to write a program that checks if each character is a letter/digit.
And with ASCII code, how am i suppose to do it?
Any help is appreciated.
Thanks!
Look up isalnum(), then iterate across each character in the string and check it.
There is also isalpha(), isdigit(), ...

But it you are just starting out, coding your own version is easy enough and will help you understand how character codes work. (And you could even test your function against the standard versions!)

See the char codes and think about the logic you need. for example:

ASCII Table and Description
http://www.asciitable.com/

It should hopefully pretty obvious when you check the codes for the letters and numbers.

Andy
Yes, isalnum means is alpha numeric but what about ASCII code?
How can I make my program using the ASCII code for letters/digits? I don't know how to do this so please tell me how I should do it.
Yes, isalnum means is alpha numeric but what about ASCII code?


isalnum() tests a char to see if its ascii code corresponds with that of one of the letters or numbers.

Have you come across the char data type? As in

char my_ch = 'x';

If so, the a character variable is usually [1] encoded in ascii.

For the character 'a' the code is 97. You can see it if you cast the character variable to int before writing it out.

cout << my_char << " = " << (int)my_char << endl;


which will produce the following output

a = 97


Andy

[1] C++ uses ascii codes by default. You can store strings using other encoding schemes, but only if you do extra work. So you can assume a char is ascii.
Last edited on
@andywestken Thank you . I will assume a char is ascii.
Please take a look at my program. I want to make a program that checks whether the user entered a valid name or not ( a name that doesn't contain any numbers ) :
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

#include <iostream>
#include <string>
#include <cctype>
using namespace std;

int main()
{ 
    char name[50];

    int i = 0;
    while (true) {
        cout << "Please enter your name.\n";
        cin >> name;


    if (isalpha(name[i])) {
        i = 0;
        i++;

        cout << "Nice to meet you.\n";
    break;
    }
    cout << "No, that's wrong.Enter your name again:\n";
    }

    return 0;

}
    


But if I type for example like this : Mary788 or something like this it will display the message :'nice to meet you'


Please enter your name.
Mary788
Nice to meet you.


So what's wrong ? I think the way I used the isalpha() function? How should I make it work properly without printing the message ' Nice to meet you' when it's not the time?
I want the program to check all the characters in the name entered by the user and see if it contains only letters and then to display the message.
Thanks in advance!
You need to check all the characters in the string, rather than just the i'th

Use strlen() to get the length and use isalpha() with a for loop
I get this error when I try to compile it:
'strlen' was not declared in this scope and I don't know what's wrong. ??
Thank you for helping me out!
If a standard function is not declared it means:
1. you're not including the header that defines it
2. you've forgotten about the namespace

If you are using Visual C++, use either your local help or MSDN :
http://msdn.microsoft.com

When I search for strlen, the first item in the search results list is the entry for this function. Including where it lives.

If you're using GCC, I am not sure where the best places are to look (any Linux types about?). I normally just Google along the line of "+strlen +gcc" and look at the mosy likely entries that get listed.

Here you need <cstring>

Andy

P.S. There is one trick, though. MSDN list the file for strlen as <string.h>

But it is better C++ to use <cstring>, as it wraps the function in the standard namespace (same for <cstdio>, <cstdlib>, ... cf. <stdio.h>, <stdlib.h>, so you should see the pattern!)

So -- for standard library headers -- you should usually try the <cstring> form first, before using the string.h form.
Last edited on
Sorry, it was my mistake. I forgot the 'c' in the <cstring>. I usually take care to be there but this time...
I 'm sorry for bothering you again but I couldn't make it with the for loop and the strlen. It's more likely I didn't use them properly and I mixed things up :
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

#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;

int main()
{ 
char name[50];    int i = 0;
    while (true) {
    cout << "Please enter your name.\n";
    cin >> name;
    break;
cout << "No, that's wrong.Enter your name again:\n";
}
    for (i = 0;i <= strlen(name) ; i++) {
    if (isalpha(name[i])) {
   
    cout << "Nice to meet you.\n";
}
}
return 0;

}
    



Last edited on
It's disgusting code! There are so many errors and bad things... That is corrected it:
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
#include <iostream>
#include <cstring>
#include <cctype>

using namespace std;


int main()
{
    char name[50] = {'\0'};

    for(bool _wrong = true; _wrong; )
    {
        cout << "Please, enter your name:   ";
        cin.getline(name, sizeof name);

        _wrong = false;
        for(int i = 0, _name_length = strlen(name); i < _name_length; i++)
            if(!isalpha(name[i]))
            {
                cout << "Unresolved character at position " << i << "." << endl << endl;
                _wrong = true;
                break;
            }
    }
    cout << "Nice to meet you, " << name << "!" << endl;

    return 0;
}


And see ASCII codes list (you can do it there: http://ascii.org.ru/ascii.pdf )
Last edited on
@PlusPower It would help if your code was better formatted. If part of the problem is with cutting and pasting into your forum posts, you should use the preview to check it turned out as expected before posting.

With a bit more space your code looks like

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
#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;

int main()
{ 
    char name[50];
    int i = 0;

    while (true) {
        cout << "Please enter your name.\n";
        cin >> name;
        break;
        cout << "No, that's wrong.Enter your name again:\n";
    }

    for (i = 0;i <= strlen(name) ; i++) {
        if (isalpha(name[i])) {
               cout << "Nice to meet you.\n";
        }
    }

    return 0;
}


With the extra spaces, it's hopefully clear that you have one loop to read the name, which breaks out of the loop as soon as it gets its first name.

Then there is a loop which checks each char to see if it's a letter, printing out "Nice to meet you" if it is.

For "a2b" I'd expect it to print out the greeting twice!

Then:

- cin >> buffer only reads up to the first white space (space, tab, endline), so "Mickey Mouse" will cause the greeting to be printed out only 6 times, for the first name. Hence Syuf's use of getline(), which reads to the endline, or until the buffer is full

- infinite loops (while(true), ...) have their place, but you should use conditional loop in preference.
Last edited on
Topic archived. No new replies allowed.