Solved

c++  making sure string is correct

Posted on 2008-06-16
9
198 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now