A string comparison function

This is a discussion on A string comparison function within the C++ Programming forums, part of the General Programming Boards category; No, this is not a question! I'm just informing everyone I wrote a pretty good string comparison function which I ...

  1. #1
    harryp
    Guest

    A string comparison function

    No, this is not a question! I'm just informing everyone I wrote a pretty good string comparison function which I think is a bit more powerful than strcmp(). For instance, strcmp() doesn't return false if the second string has spaces where the first doesn't. I've fixed this. Here's the code, it's pretty basic. It returns true if string 2 is the same as string 1, and false if it is different:
    Code:
    bool StrComp(char * str1, char * str2)
    {
    	// loops through and checks, char by char, if equal
    	int i=0;
    	bool fail=false;
    
    	for( ;; )
    	{
    		// quit if str1[i] != str2[i]
    		if (str1[i] != str2[i])
    		{
    			fail = true;
    			break;
    		}
    		// this will even fail if str1[i] is a space and str2[i] isn't, unlike strcmp()
    		if (str1[i] == ' ' && str2[i] != ' ')
    		{
    			fail = true;
    			break;
    		}
    		// quit if reach end of string
    		if (str1[i] == '\0')
    		{
    			// return false if only one of the strings is done
    			if (str2[i] == '\0')
    				break;
    			else
    			{
    				fail = true;
    				break;
    			}
    		}
    		i++;
    	}
    	// fail
    	if (fail)
    		return false;
    	// dont fail
    	else
    		return true;
    }
    Just wanted to share that with everyone. I've found a little useful. I've got this thing with using pre-made functions. If I can, I always like to things my own way. Like with this: I'd much rather use something I personally created than something that the compiler includes. It makes me feel good that I can do something just as good as theirs. I'm wierd that way Anyways, hope you like it.

    Brendan

  2. #2
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065

    Re: A string comparison function

    Originally posted by harryp
    I'm wierd that way
    Not weird at all. It's a great learning tool to design/implement your own functions for common tasks. Not to mention the confidence boost you get from successfully doing it. Good work!

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    That's cool, but a couple of minor problems. For one, strcmp doesn't return true or false. It returns 0 if they match, negative if the second is greater, positive otherwise. But anyhow I know what you meant

    By the way, since you are tesing for equality in the first 'if', there is no need for the second if statement. Moreover, you can also return right from the loop if it 'fails'. Here's an example:

    bool foo( char *a, char *b)
    {
    for( int i = 0; a[i] != 0 && b[i] != 0; i++)
    {
    if(a[i] != b[i]) return false;
    }

    if(a[i] != b[i]) return false; //...only one was null terminated..

    return true;
    }
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  4. #4
    harryp
    Guest
    Yeah, I know about the strcmp . Anyways, I did need the second if because, just like strcmp, if the user enters a space as the last character of their string, and the second string does not have that space, it still says they're the same. And puting the second inside the first wouldn't work either because it doesn't say it's wrong in the first place. So I modified it alightly to this:
    Code:
    if (str1[i] != str2[i] || str1[i] == ' ' && str2[i] != ' ')
         return false;
    That shortened it a lot. And about returning false right away, I was going to do that, but I didn't know if I could do that in a loop. I thought I had to break it first. Oh well, so I learned something. Thanks!

    Brendan

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Another strcmp version:
    Code:
    /* strcmp:  return <0 if s<t, 0 if s==t, >0 if s>t */
       int strcmp(char *s, char *t)
       {
           for ( ; *s == *t; s++, t++)
               if (*s == '\0')
                   return 0;
           return *s - *t;
       }
    (Taken from K&R2, btw).
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    Good ole k&r i really like that version best.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    There's nothing wrong with making your own string comparison function. You learn a lot by doing things yourself.
    Anyways, I did need the second if because, just like strcmp, if the user enters a space as the last character of their string, and the second string does not have that space, it still says they're the same.
    This is not true. For example:
    Code:
       char a[80] = "Tiger";
       char b[80] = "Tiger ";
    
       if (strcmp(a,b) == 0)
          cout << "Strings are the same." << endl;
       else
          cout << "Strings aren't the same." << endl;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM

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