TechMonster
asked on
c++ making sure string is correct
I have a input variable string called LName.
It is exactly 15 characters long.
I need to make sure that users don't put in anything other then characters, hypens, apostrophes as it will mess up my code.
Thanks for any help.
It is exactly 15 characters long.
I need to make sure that users don't put in anything other then characters, hypens, apostrophes as it will mess up my code.
Thanks for any help.
What are the acceptable characters ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Or strcspn if you want the inverse (ie. you have a list of unacceptable characters) :
http://www.cplusplus.com/reference/clibrary/cstring/strcspn.html
http://www.cplusplus.com/reference/clibrary/cstring/strcspn.html
std::string str; // <--- the string that needs to be checked
std::string exclude; // <--- all UNacceptable characters
if (strcspn(str.c_str(), exclude.c_str()) == str.length()) {
// OK
}
else {
// NOT OK
}
Or if you can use any of the standard ctype functions :
http://www.cplusplus.com/reference/clibrary/cctype/
like for example isprint (which returns true if the character is printable - ie. no control character) ...
http://www.cplusplus.com/reference/clibrary/cctype/
like for example isprint (which returns true if the character is printable - ie. no control character) ...
You can use find_first_of and find_first_not_of std::string members to include or exclude specific chars from a string. You search for what you do or don't want included and take appropriate action if something is found.
http://www.cplusplus.com/reference/string/string/find_first_of.html
http://www.cplusplus.com/reference/string/string/find_first_not_of.html
http://www.cplusplus.com/reference/string/string/find_first_of.html
http://www.cplusplus.com/reference/string/string/find_first_not_of.html
ASKER
is the strcspn case sensitive?
Or will I have to list out all upper case and lower case
Or will I have to list out all upper case and lower case
Or use the C++ standard algorithms, like find_first_of for example :
http://www.cplusplus.com/reference/algorithm/find_first_of.html
http://www.cplusplus.com/reference/algorithm/find_first_of.html
std::string str; // <--- the string that needs to be checked
std::string exclude; // <--- all UNacceptable characters
std::string::iterator it = find_first_of(str.begin(), str.end(), exclude.begin(), exclude.end());
if (it == str.end()) {
// OK
}
else {
// NOT OK
}
or ... do you want me to keep going ? ;)
>> is the strcspn case sensitive?
>> Or will I have to list out all upper case and lower case
You will have to list them all. But as I said, if you just want to check for alphabetic characters, take a look at isalpha :
http://www.cplusplus.com/reference/clibrary/cctype/isalpha.html
>> is the strcspn case sensitive?
>> Or will I have to list out all upper case and lower case
You will have to list them all. But as I said, if you just want to check for alphabetic characters, take a look at isalpha :
http://www.cplusplus.com/reference/clibrary/cctype/isalpha.html
ASKER
wow..thanks.. I 'll give your suggestions a try.