Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String Validation

Status
Not open for further replies.

chmilz

Programmer
Jan 10, 2001
94
CA
Hi!

I am writing a program that will keep track of books through their ISBN numbers. However, I have a function that will test to ensure that the string IS a valid ISBN Number and thats where I am having trouble. My function prototype is:

static bool IsValidISBN(const CString & sISBN)
//sISBN is the name of the string
{

}
there are three criteria the string has to meet before it is assumed valid:
1) The string must be exactly 13 char's long
2) It must have three dashes in the correct spots
3) There cannot be two dashes in a row
This is what a VALID ISBN looks like: 0-672-30787-1
If you could give me some help on developing code for this function I would REALLY appreciate it!

Thank You!!!
 
Your specification is ambigious.

> 2) It must have three dashes in the correct spots
> 3) There cannot be two dashes in a row

Item two seems to indicate that there are specific locations for the dashes, while item three indicates there are not specific locations rather they must not not be adjacent.

Hence I could say that this qualifies as a valid ISBN:
7y67bt4-0-jj-

-pete
 
Sorry, I worded that wrong. What I meant was:

The string must be exactly 13 char's long and contain three dashes in valid locations (eg. The 12th char MUST contain the last '-'; the first dash cannot be in the first position within the string; no two dashes can appear in consecutive locations!)

After that check I have to remove the dashes and verify that the remaining char's are numeric.

Hope that clarifys things!

Curtis
 
CString remove( CSTring my_string )
{
for (int w=0;w<strlen(my_string);w++)
if (my_string[w]=='-')
for (int k=w;<strlen(my_string);k++)
my_string[k]=my_string[k+1];

return my_string;
}

bool IsValidISBN(const CString &amp; sISBN)
//sISBN is the name of the string
{
int q,counter=0;
bool result=true;
for (q=0;q<strlen(sISB);q++)
if (sISBN[q]=='-') counter++;

if ((counter>3) || (strlen(sISBN)!=13)) result=false;
if ((sISBN[1]!='-') || (sISBN[5]!='-') || (sISBN[11]!='-')) result=false;

if (result) remove(sISBN);
return result;

} Best Regards,

aphrodita@mail.krovatka.ru {uiuc rules}
 
Curtis,

I can't vouch for the efficiency but this seems to work.

Good luck
-pete


class ISBNValidator{
public:
static bool isValid(CString&amp; str){

bool bret = ( ( 13 == str.GetLength() )
&amp;&amp;
('-' == str.GetAt(11) )
&amp;&amp;
isdigit( str.GetAt(12))
&amp;&amp;
isdigit( str.GetAt(0)) );

int dashes = 0;
for(int n=1; bret &amp;&amp; dashes < 2 &amp;&amp; n<11; n++){

char c = str.GetAt(n);
if ( '-' == c) dashes++;
else if (!isdigit(c)) bret = false;
}

return bret &amp;&amp; ( 2 == dashes );
}
};
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top