Is there a more concise method?

This is a discussion on Is there a more concise method? within the C++ Programming forums, part of the General Programming Boards category; I would appreciate it if someone could look over this code and tell me if there is a more precise ...

  1. #1
    aji
    aji is offline
    Registered User
    Join Date
    Oct 2005
    Posts
    2

    Is there a more concise method?

    I would appreciate it if someone could look over this code and tell me if there is a more precise method of going through and comparing individual characters of a string. The code below is a function called upon by the main function to compare individual characters of 2 strings.

    Code:
    int CheckAnswer(string)
    {
    	int count;
    	
    	if(key == answer)
    	{
    		score = 20;
    	}
    	else if (key.length() == answer.length())
    	{
    		count = 0;
    		score = 0;
    		while (count <= 20 && score <= 20)
    		{
    			if (key.substr(0,1) == answer.substr(0,1))
    			{
    				score++;
    				count++;
    			}
    			else
    			{
    				count++;
    			}
    			if (key.substr(1,2) == answer.substr(1,2))
    			{
    				score++;
    				count++;
    			}
    			else
    			{
    				count++;
    			}
    			if (key.substr(2,3) == answer.substr(2,3))
    			{
    				score++;
    				count++;
    			}
    			else
    			{
    				count++;
    			}
    			if (key.substr(3,4) == answer.substr(3,4))
    			{
    				score++;
    				count++;
    			}
    			else
    			{
    				count++;
    			}
    			if (key.substr(4,5) == answer.substr(4,5))
    			{
    				score++;
    				count++;
    			}
    			else
    			{
    				count++;
    			}
    			if (key.substr(5,6) == answer.substr(5,6))
    			{
    				score++;
    				count++;
    			}
    			else
    			{
    				count++;
    			}
    			if (key.substr(6,7) == answer.substr(6,7))
    			{
    				score++;
    				count++;
    			}
    			else
    			{
    				count++;
    			}
    			if (key.substr(7,8) == answer.substr(7,8))
    			{
    				score++;
    				count++;
    			}
    			else
    			{
    				count++;
    			}
    			if (key.substr(8,9) == answer.substr(8,9))
    			{
    				score++;
    				count++;
    			}
    			else
    			{
    				count++;
    			}
    			if (key.substr(9,10) == answer.substr(9,10))
    			{
    				score++;
    				count++;
    			}
    			else
    			{
    				count++;
    			}
    			if (key.substr(10,11) == answer.substr(10,11))
    			{
    				score++;
    				count++;
    			}
    			else
    			{
    				count++;
    			}
    			if (key.substr(11,12) == answer.substr(11,12))
    			{
    				score++;
    				count++;
    			}
    			else
    			{
    				count++;
    			}
    			if (key.substr(12,13) == answer.substr(12,13))
    			{
    				score++;
    				count++;
    			}
    			else
    			{
    				count++;
    			}
    			if (key.substr(13,14) == answer.substr(13,14))
    			{
    				score++;
    				count++;
    			}
    			else
    			{
    				count++;
    			}
    			if (key.substr(14,15) == answer.substr(14,15))
    			{
    				score++;
    				count++;
    			}
    			else
    			{
    				count++;
    			}
    			if (key.substr(15,16) == answer.substr(15,16))
    			{
    				score++;
    				count++;
    			}
    			else
    			{
    				count++;
    			}
    			if (key.substr(16,17) == answer.substr(16,17))
    			{
    				score++;
    				count++;
    			}
    			else
    			{
    				count++;
    			}
    			if (key.substr(17,18) == answer.substr(17,18))
    			{
    				score++;
    				count++;
    			}
    			else
    			{
    				count++;
    			}
    			if (key.substr(18,19) == answer.substr(18,19))
    			{
    				score++;
    				count++;
    			}
    			else
    			{
    				count++;
    			}
    			if (key.substr(19,20) == answer.substr(19,20))
    			{
    				score++;
    				count++;
    			}
    			else
    			{
    				count++;
    			}
    		}
    
    	}
    	else if (key > answer)
    	{
    		score = -1;
    	}
    	else if (key < answer)
    	{
    		score = -2;
    	}
    	else if (something)
    	{
    		score = -3
    	}
    return 0;
    
    }
    Thanks-

  2. #2
    Registered User
    Join Date
    Oct 2005
    Posts
    13
    something like this maybe?

    Code:
    for(int X = 0;X<20;X++)
    {
           if (key.substr(X,X+1) == answer.substr(X,X+1))
    	{
    		score++;
    	}
    }
    Last edited by shadovv; 10-23-2005 at 11:39 PM.

  3. #3
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    Are the lengths of the two strings always 20? If so, then that for loop will be OK.

    Otherwise I might use:
    Code:
    for(int X = 0; x != key.length(); ++X)
    Your larger problem is in your calls to substr(). substr(i,n) gets the substring starting at position i, n characters long. If your key was "abcdefgh" then substr(i,n) is below:
    Code:
    i    n     substr(i,n)
    -----------------------
    1   1     "a"
    2   3     "bcd"
    3   4     "cdef"
    4   5     "defgh"
    5   6      error!
    That last one should give you an error since there is no string starting at position 5, 6 characters long. Try this instead:
    Code:
    for(int i = 0; i != key.length(); ++i)
    {
          if (key.substr(i,1) == answer.substr(i,1))
                ++score;
    }
    That way you're just comparing one character at a time. Come to think of it, you don't even need substr to compare one character at a time, I'm pretty sure you can just do this:
    Code:
    for(int i = 0; i != key.length(); ++i)
    {
          if (key[i] == answer[i])
              ++score;
    }
    I'm pretty sure that will work, but I don't have a compiler on this machine, so I left the substr version in the post, just in case - you'll have to test it.
    There is a difference between tedious and difficult.

  4. #4
    aji
    aji is offline
    Registered User
    Join Date
    Oct 2005
    Posts
    2
    Hey, I just wanted to say thanks for your help guys. Your timly reply was much appreciated and your advice worked with a little tweaking of my program... and now i have a better understanding of the way that substr() function works.
    -Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. on method pointers and inheritance
    By BrownB in forum C++ Programming
    Replies: 2
    Last Post: 03-02-2009, 06:50 PM
  2. stuck on display method
    By shintaro in forum C++ Programming
    Replies: 2
    Last Post: 02-01-2009, 04:17 PM
  3. Best communication method to thousand childs?
    By Ironic in forum C Programming
    Replies: 8
    Last Post: 11-07-2008, 11:30 PM
  4. Overriding a method in C
    By DavidDobson in forum C Programming
    Replies: 1
    Last Post: 07-05-2008, 07:51 AM
  5. Replies: 2
    Last Post: 01-22-2008, 03:22 PM

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