Solved

c++  making sure string is correct

Posted on 2008-06-16
9
206 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Quicksort a dynamic deque 33 66
Need some help with listview limit text 20 98
Microsoft C++ code failing in executable that worked 9 84
Beginner to Unreal Engine 4 5 57
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 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…

914 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

20 Experts available now in Live!

Get 1:1 Help Now