User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 456,563 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,515 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 1083 | Replies: 7
Reply
Join Date: Oct 2007
Posts: 6
Reputation: woozy is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
woozy woozy is offline Offline
Newbie Poster

Solution to my lexicographical sorting problem

  #1  
Oct 21st, 2007
My problem was over here: http://www.daniweb.com/forums/thread93806.html

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

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

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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2007
Posts: 62
Reputation: Ptolemy is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 8
Ptolemy's Avatar
Ptolemy Ptolemy is offline Offline
Junior Poster in Training

Re: Solution to my lexicographical sorting problem

  #2  
Oct 21st, 2007
>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:
  1. int my_strcmp ( const char *a, const char *b )
  2. {
  3. while ( tolower ( (unsigned char)*a ) == tolower ( (unsigned char)*b ) ) {
  4. if ( *a == '\0' )
  5. return 0;
  6. ++a;
  7. ++b;
  8. }
  9.  
  10. return *a < *b ? -1 : +1;
  11. }
Last edited by Ptolemy : Oct 21st, 2007 at 6:02 pm.
Reply With Quote  
Join Date: Oct 2007
Posts: 6
Reputation: woozy is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
woozy woozy is offline Offline
Newbie Poster

Re: Solution to my lexicographical sorting problem

  #3  
Oct 21st, 2007
I see. I thought by not using the input pointers directly can prevent all the possibilities of modifying them.
Reply With Quote  
Join Date: Oct 2007
Posts: 62
Reputation: Ptolemy is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 8
Ptolemy's Avatar
Ptolemy Ptolemy is offline Offline
Junior Poster in Training

Re: Solution to my lexicographical sorting problem

  #4  
Oct 21st, 2007
>I thought by not using the input pointers directly can prevent all the possibilities of modifying them.
Not quite:
  1. #include <stdio.h>
  2.  
  3. void foo ( int *p )
  4. {
  5. int *temp = p;
  6.  
  7. *temp = 12345;
  8. }
  9.  
  10. int main ( void )
  11. {
  12. int x = 10;
  13.  
  14. foo ( &x );
  15. printf ( "%d\n", x );
  16.  
  17. return 0;
  18. }
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.
Reply With Quote  
Join Date: Dec 2006
Posts: 1,569
Reputation: Aia is a splendid one to behold Aia is a splendid one to behold Aia is a splendid one to behold Aia is a splendid one to behold Aia is a splendid one to behold Aia is a splendid one to behold Aia is a splendid one to behold 
Rep Power: 12
Solved Threads: 114
Aia's Avatar
Aia Aia is offline Offline
Posting Virtuoso

Re: Solution to my lexicographical sorting problem

  #5  
Oct 21st, 2007
Originally Posted by Ptolemy View Post
How about something more like this:
  1. int my_strcmp ( const char *a, const char *b )
  2. {
  3. while ( tolower ( (unsigned char)*a ) == tolower ( (unsigned char)*b ) ) {
  4. if ( *a == '\0' )
  5. return 0;
  6. ++a;
  7. ++b;
  8. }
  9.  
  10. return *a < *b ? -1 : +1;
  11. }

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>?
At the very moment that I find myself in the side of the mayority, I will know that I need to re-think my ideas. ~ In my book.
Reply With Quote  
Join Date: Oct 2007
Posts: 62
Reputation: Ptolemy is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 8
Ptolemy's Avatar
Ptolemy Ptolemy is offline Offline
Junior Poster in Training

Re: Solution to my lexicographical sorting problem

  #6  
Oct 21st, 2007
>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.
Reply With Quote  
Join Date: Dec 2006
Posts: 1,569
Reputation: Aia is a splendid one to behold Aia is a splendid one to behold Aia is a splendid one to behold Aia is a splendid one to behold Aia is a splendid one to behold Aia is a splendid one to behold Aia is a splendid one to behold 
Rep Power: 12
Solved Threads: 114
Aia's Avatar
Aia Aia is offline Offline
Posting Virtuoso

Re: Solution to my lexicographical sorting problem

  #7  
Oct 21st, 2007
>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?
At the very moment that I find myself in the side of the mayority, I will know that I need to re-think my ideas. ~ In my book.
Reply With Quote  
Join Date: Oct 2007
Posts: 62
Reputation: Ptolemy is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 8
Ptolemy's Avatar
Ptolemy Ptolemy is offline Offline
Junior Poster in Training

Re: Solution to my lexicographical sorting problem

  #8  
Oct 21st, 2007
>Where's the case insensitive in the original OP my_strcmp?
  1. temp1 = my_strlwr(str1);
  2. temp2 = my_strlwr(str2);
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 5:43 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC