I am trying to match a string to see if it follows a given pattern. I have seen this example in my Orielly books but do not understand the logic. Here is the a small snippet of example code:

const string digits("0123456789");
const string float_digit = digits + '.' + digits;

string test_string

The object is to determine if the test_string is a digit or a float_digit. If this is the case what would be a good way. Do you try to compare the test_string to the digit string (using the compare function) or do you have to use an loop to test each element in the test_string to see if it is a digit? What is the best or easiest way to do this?

One very convenient way to test this is with a regular expression, ^\d+$ and ^\d+\.\d+$ in this case. However, that would require the installation of a library for that purpose. But if you ever get this problem while using Perl or Ruby, that would be the solution.

In the case of C++, in your situation, you'll have to use a loop to test each element in test_string to see if it's a digit. That's not actually a big deal, is it? You know that x is a digit if '0' <= x && x <= '9' . Have it count the number of times it sees a '.' -- if the number grows past 1, you have an invalid string. If any characters are neither a digit nor '.', you have an invalid string. Otherwise, the result depends on whether you've seen a decimal point or not.

One very convenient way to test this is with a regular expression, ^\d+$ and ^\d+\.\d+$ in this case. However, that would require the installation of a library for that purpose. But if you ever get this problem while using Perl or Ruby, that would be the solution.

In the case of C++, in your situation, you'll have to use a loop to test each element in test_string to see if it's a digit. That's not actually a big deal, is it? You know that x is a digit if '0' <= x && x <= '9' . Have it count the number of times it sees a '.' -- if the number grows past 1, you have an invalid string. If any characters are neither a digit nor '.', you have an invalid string. Otherwise, the result depends on whether you've seen a decimal point or not.

Would something like this work..

int i, 
char x;

for(i = 0; i <= test_string.length; i++) //checks the entire length of the string
{
  while('0' <= x && x <= '9') //checks for ints between 0 to 9
   {
      test_string[x]

   }
   cout<<" test string is full of digits"<<endl;
}

I am pretty sure this is wrong. What am I doing wrong here?

Simulate your code by hand with an example input and find out. Is test_string.length supposed to be test_string.size() ?

Simulate your code by hand with an example input and find out. Is test_string.length supposed to be test_string.size() ?

Yes. I used .length but I think .size will do the same. I will test out both.

If you want to do it by using regular expressions you would need a lexical analyzer. For C/C++ lex is usually used. Here is a tutorial page
http://dinosaur.compilertools.net/lex/index.html

as for the code

int i, 
char x;
 
for(i = 0; i <= test_string.length; i++) //checks the entire length of the string
{
  if('0' <= test_string[i] && test_string[i] <= '9') //checks for ints between 0 to 9
   {
      // character is a digit
 
   }
else if(test_string[i]=='.')
{
    // string is a floating point
}
   cout<<" test string is full of digits"<<endl;
}

That code is broken: you're iterating off the end of the string.

>>line 4: test_string.length
Length is a function call, not a data object. test_string.length()

Probably this might work for you

const string strDigit = "0123456789";
const string strFloat = ".0123456789";

string strInput; // This the input string.

//// Logic 
int nPos = -1;
if ( ( nPos = strInput.find_first_not_of( strDigit ) ) >= 0 )
{
	if ( ( nPos = strInput.find_first_not_of( strFloat ) ) >= 0 )
	{
		// Not Float or a digit
	}
	else
	{
		//Float
	}
}
else
{
		//Digit
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.