detecting numbers in a code of line

• 01-22-2008
aama100
detecting numbers in a code of line
Is there any way to detect how many times cretin characters can appear in a serial number ? EX if I have a code of 12 characters including letters and numbers in this code. I want to make sure that user will use only number four for once. Using if statement what's the possibility to get that function?

In this following code I made sure that the user will promote 12 characters only. Should I copy the serial code into an array then do searching for number four and once the program detects that the user used number four more than once it sends an error, or is there an easier way to do so?

Code:

```#include <iostream> #include<string> using namespace std; int main() {   string serial_number;         int length=0;                         cout<< "please enter a serial number of 12 characters:";         cin >> serial_number;         length=serial_number.length();                 if (length!=12 )                 cout << "The code you have entered cannot be processed because the code must be contained of 12 characters exactly";         else                 cout << " that's correct code";         return 0; }```
• 01-22-2008
anon
You can test if a character is in range ['0', '9'], or use the isdigit() function from <cctype>.
• 01-22-2008
jawahar
yeah....suppose
Code:

```int freq[256]; ////possible alll characters....u might restrict the limit if u r using fixed set of chars for(int i=0;i<length;i++)  freq[(int)s.at(i)]++```
this will count the frequiences and hence u can know the frequency of each character and then give approriate error msg to the user

Note:
s is string
lenght is lenght of s

cheers :)
• 01-22-2008
Dino
Something like this nested FOR loop construct would work:
Code:

```for (element = 0 ; element < string_length-1 ; ++element)         {         for (nextelement = element+1 ; nextelement < string_length; ++nextelement )         {                 if ( string[element] == string[nextelement] ) dup_char = true ;         } }```
Todd
• 01-23-2008
jawahar
Quote:

Originally Posted by Todd Burch
Something like this nested FOR loop construct would work:
Code:

```for (element = 0 ; element < string_length-1 ; ++element)         {         for (nextelement = element+1 ; nextelement < string_length; ++nextelement )         {                 if ( string[element] == string[nextelement] ) dup_char = true ;         } }```
Todd

Hi there...the algo u suggested would run in O(n-square) i guess....It is much better u hash the values as i told...is there anything wrong in my implementation...please correct me...

cheers :)
• 01-23-2008
Dino
I'm not sure which one would be faster. On the surface, it looks like yours might be faster since it only makes 12 "touches" to the input string.

To check for dups in 12 characters, with my routine, there are 66 compares.

With your routine, there is one quick pass of 12 characters, plus the add of the frequency to the frequency table. Then, you would iterate through the frequency table and do 256 compares for a value of greater than 1.

Both routines would benefit from "early exit" logic if a duplicate were found.

Neither routine takes into consideration case sensitivity, if that matters at all.

Maybe we need to code up a test!! Perhaps the efficiency is determined by how fast a duplicate is or is not found.

Good discussion!

Todd
• 01-23-2008
drrcknlsn
Quote:

Originally Posted by jawahar
Code:

```#define MAX_CHARS 256 // possible alll characters....u might restrict the limit if u r using fixed set of chars int freq[MAX_CHARS]; // memset ( freq, 0, sizeof ( freq ) ); for ( i = 0; i < MAX_CHARS; ++i )     freq[i] = 0; for(int i=0;i<length;i++)  freq[(int)serial_number.at(i)]++;```

...can't increment before initialization.
• 01-23-2008
whiteflags
> Then, you would iterate through the frequency table and do 256 compares for a value of greater than 1.

Checking the frequency of any letter in jawahar's lookup table is as simple as lookup:

std::cout<<serial[i]<<" appears "<<freq[ serial[i] ]<<" times.\n";

If you are aware of the character set the computer is using, then there are a bunch of non-portable speedups to lookup in linear time if you wanted to check say, all the alphanumeric characters in the serial. But '0' through '9' are always in order, so checking the frequency of those should occur in no time at all.

> Neither routine takes into consideration case sensitivity, if that matters at all.

As long as you create a table big enough, it would. If you wanted a smaller table you would have to make use of the functions in <cctype>

Although one has to wonder if, with a serial number this small, a lookup table is worth implementing.