Member Avatar for woozyking

My problem was over here: http://www.daniweb.com/forums/thread93806.html

After many advices, helps, I could eventually solve this problem myself, happy~~:D

All I did was to write a replacement function for strcmp() and then use a bubble sort function to sort input strings in lexicographical order.

Much simpler than nasty nested loops :P

my_strcmp():

int my_strcmp (char *str1, char *str2)
{  
	int id = 0;

	char *temp1, *temp2;

	temp1 = my_strlwr(str1);
	temp2 = my_strlwr(str2);

	while(!(id = *temp1 - *temp2) && *temp2)
	{
		++temp1, ++temp2;
	}

	if (id < 0)
	{
		id = -1;
	}
	else if (id > 0)
	{
		id = 1;
	}

	return id;
}

In fact, I got the realization from the source code algorithm. The important part is that I understand how this algorithm works now.

>int my_strcmp (char *str1, char *str2)
When I'm comparing strings, I expect them to remain unchanged. So you should make str1 and str2 pointers to const char.

>temp1 = my_strlwr(str1);
>temp2 = my_strlwr(str2);
When I'm comparing strings, I expect them to remain unchanged. I also expect the comparison to be relatively efficient and useful compared to my own hand rolled implementation. As such, any possible way you could have written my_strlwr will diminish the value of my_strcmp because it's either going to change str1 and str2, allocate memory (which is extremely slow), or use static buffers (severely limiting the usefulness of my_strlwr).

>while(!(id = *temp1 - *temp2) && *temp2)
Integer underflow will give you funky results.

How about something more like this:

int my_strcmp ( const char *a, const char *b )
{
  while ( tolower ( (unsigned char)*a ) == tolower ( (unsigned char)*b ) ) {
    if ( *a == '\0' )
      return 0;
    ++a;
    ++b;
  }

  return *a < *b ? -1 : +1;
}
Member Avatar for woozyking

I see. I thought by not using the input pointers directly can prevent all the possibilities of modifying them.

>I thought by not using the input pointers directly can prevent all the possibilities of modifying them.
Not quite:

#include <stdio.h>

void foo ( int *p )
{
  int *temp = p;

  *temp = 12345;
}

int main ( void )
{
  int x = 10;

  foo ( &x );
  printf ( "%d\n", x );

  return 0;
}

It doesn't matter how many pointers you have to an object, you can still use any one of them to modify it. There's no way to prevent all of the possibilities of modifying something, but const goes a long way toward that goal.

How about something more like this:

int my_strcmp ( const char *a, const char *b )
{
  while ( tolower ( (unsigned char)*a ) == tolower ( (unsigned char)*b ) ) {
    if ( *a == '\0' )
      return 0;
    ++a;
    ++b;
  }

  return *a < *b ? -1 : +1;
}

You are making MY_STRING the same that my_string. And that's not what strcmp() does.
Why to use tolower and include <ctype.h > to avoid the use of strcmp and include <string.h>?

>You are making MY_STRING the same that my_string.
What's my_string?

>And that's not what strcmp() does.
It's exactly what strcmp does, with the exception that my_strcmp is case insensitive. I made it that way because the OP's my_strcmp was case insensitive as well.

>Why to use tolower and include <ctype.h > to avoid
>the use of strcmp and include <string.h>?
Because the original requirement was that strcmp and strncmp couldn't be used.

>It's exactly what strcmp does, with the exception that my_strcmp is case insensitive.
It's to that exception to what I was referning.

>I made it that way because the OP's my_strcmp was case insensitive as well.
Where's the case insensitive in the original OP my_strcmp?

>Where's the case insensitive in the original OP my_strcmp?

temp1 = my_strlwr(str1);
temp2 = my_strlwr(str2);
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.