Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

c++  making sure string is correct

Posted on 2008-06-16
9
221 Views
Last Modified: 2010-05-18
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.
0
Comment
Question by:TechMonster
  • 6
  • 2
9 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 21791510
What are the acceptable characters ?
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 250 total points
ID: 21791525
You could use strspn for example :

        http://www.cplusplus.com/reference/clibrary/cstring/strspn.html
std::string str;              // <--- the string that needs to be checked
std::string charset;          // <--- all acceptable characters
if (strspn(str.c_str(), charset.c_str()) == str.length()) {
  // OK
}
else {
  // NOT OK
}

Open in new window

0
 
LVL 53

Expert Comment

by:Infinity08
ID: 21791530
Or strcspn if you want the inverse (ie. you have a list of unacceptable characters) :

        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
}

Open in new window

0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 53

Expert Comment

by:Infinity08
ID: 21791543
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) ...
0
 
LVL 40

Expert Comment

by:evilrix
ID: 21791549
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

0
 

Author Comment

by:TechMonster
ID: 21791550
is the strcspn case sensitive?
Or will I have to list out all upper case and lower case
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 21791556
Or use the C++ standard algorithms, like find_first_of for example :

        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
}

Open in new window

0
 
LVL 53

Expert Comment

by:Infinity08
ID: 21791562
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
0
 

Author Comment

by:TechMonster
ID: 21791570
wow..thanks.. I 'll give your suggestions a try.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question