Solved

c++  making sure string is correct

Posted on 2008-06-16
9
228 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
[X]
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
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

730 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