Solved

c++  making sure string is correct

Posted on 2008-06-16
9
232 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
Technology Partners: 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

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…
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 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 learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

724 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