detecting numbers in a code of line

This is a discussion on detecting numbers in a code of line within the C++ Programming forums, part of the General Programming Boards category; Is there any way to detect how many times cretin characters can appear in a serial number ? EX if ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    58

    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;
    }

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You can test if a character is in range ['0', '9'], or use the isdigit() function from <cctype>.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    11
    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

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    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

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    11

    Smile

    Quote Originally Posted by Todd Burch View Post
    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

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    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

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    24
    Quote Originally Posted by jawahar View Post
    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.
    Last edited by drrcknlsn; 01-23-2008 at 09:07 AM.

  8. #8
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,758
    > 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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Detecting end of LINE in text files
    By kiknewbie in forum C Programming
    Replies: 6
    Last Post: 12-09-2008, 09:34 AM
  2. ASM beginner gets 24 compiler errors on hello world...
    By Desolation in forum Tech Board
    Replies: 12
    Last Post: 06-16-2007, 11:21 PM
  3. Backgammon
    By glo in forum Game Programming
    Replies: 5
    Last Post: 10-02-2006, 11:26 PM
  4. Need help with reading numbers from the command line
    By Nterpol in forum C Programming
    Replies: 4
    Last Post: 06-01-2006, 02:40 AM
  5. SSCANF help
    By mattz in forum C Programming
    Replies: 7
    Last Post: 12-10-2001, 04:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21