•
•
•
•
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
![]() |
•
•
Join Date: Oct 2007
Posts: 6
Reputation:
Rep Power: 0
Solved Threads: 0
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():
In fact, I got the realization from the source code algorithm. The important part is that I understand how this algorithm works now.
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.
>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:
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:
c Syntax (Toggle Plain Text)
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; }
Last edited by Ptolemy : Oct 21st, 2007 at 6:02 pm.
>I thought by not using the input pointers directly can prevent all the possibilities of modifying them.
Not quite:
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.
Not quite:
c Syntax (Toggle Plain Text)
#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; }
•
•
•
•
How about something more like this:
c Syntax (Toggle Plain Text)
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>?
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.
>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.
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?
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.
>Where's the case insensitive in the original OP my_strcmp?
c Syntax (Toggle Plain Text)
temp1 = my_strlwr(str1); temp2 = my_strlwr(str2);
![]() |
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Question: Linear Time Sorting Problem (Computer Science and Software Design)
- Collapsible table sorting problem (JavaScript / DHTML / AJAX)
- Link List Sorting Problem (C++)
- Input for a solution to matrix problem (C)
- Sorting arrays of pointers with function? (C)
- Solution for slow internet browsing (Web Browsers)
Other Threads in the C Forum
- Previous Thread: pointer notation
- Next Thread: c Compiler (Not C++)



Linear Mode