Problem in sorting by name

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2007
Posts: 171
Reputation: Lazaro Claiborn is an unknown quantity at this point 
Solved Threads: 13
Lazaro Claiborn's Avatar
Lazaro Claiborn Lazaro Claiborn is offline Offline
Junior Poster

Re: Problem in sorting by name

 
0
  #11
Mar 12th, 2007
Originally Posted by joeprogrammer View Post
>Here is a program that might help you
Somehow I kind of doubt that when the code doesn't even compile. :rolleyes:
Then I suggest you get a new compiler Lol.
Last edited by Lazaro Claiborn; Mar 12th, 2007 at 9:10 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Problem in sorting by name

 
0
  #12
Mar 12th, 2007
>Then I suggest you get a new compiler
I suggest that you fix the 2 typos that you made.
  1. /* Converts upper case letters to lower case */
  2. void toLower(char *str) {
  3. int len = strlen(str)-1, i;
  4. for (i=0;i<len;i++)
  5. /* 'name' has never been declared, perhaps you meant 'str'? */
  6. if (!(name[i]>=97 && name[i] <= 122))
  7. name[i] += 32;
  8. }

  1. int cmpstr(char *str1, char *str2) {
  2. int len1, len2, i;
  3.  
  4. len1 = strlen(str1);
  5. len2 = strlen(str2);
  6.  
  7. for (i=0;i<len1 && i<len2;i++) {
  8. if (str1[i] < str2[i])
  9. return 1;
  10. /* change 'str' to 'str1' */
  11. else if (str[i] > str2[i])
  12. return 2;
  13. }
  14. return 0;
  15. }

Really, when posting code snippets, the least you can do is make sure it works under normal circumstances. The corrections I showed makes it compile and run, although the code snippet still doesn't take into account numbers.

[edit] I have made a couple of other minor fixes to your toLower() function:
  1. /* Converts upper case letters to lower case */
  2. void toLower(char *str) {
  3. int len = strlen(str), i; /* removed the -1 part... why skip the last character? */
  4. for (i=0;i<len;i++)
  5. if ((str[i]>='A' && str[i] <= 'Z')) /* only lowercases letters now*/
  6. str[i] += 32;
  7. }
Last edited by John A; Mar 12th, 2007 at 10:21 pm.
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 171
Reputation: Lazaro Claiborn is an unknown quantity at this point 
Solved Threads: 13
Lazaro Claiborn's Avatar
Lazaro Claiborn Lazaro Claiborn is offline Offline
Junior Poster

Re: Problem in sorting by name

 
0
  #13
Mar 12th, 2007
Here is another example program that might help you:
http://www.daniweb.com/code/showsnippet.php?codeid=661
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Problem in sorting by name

 
0
  #14
Mar 12th, 2007
Originally Posted by Lazaro Claiborn View Post
Here is another example program that might help you:
http://www.daniweb.com/code/snippet661.html
When will you learn? If you're going to submit code, make sure it compiles.
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 171
Reputation: Lazaro Claiborn is an unknown quantity at this point 
Solved Threads: 13
Lazaro Claiborn's Avatar
Lazaro Claiborn Lazaro Claiborn is offline Offline
Junior Poster

Re: Problem in sorting by name

 
0
  #15
Mar 12th, 2007
Originally Posted by joeprogrammer View Post
>Then I suggest you get a new compiler
I suggest that you fix the 2 typos that you made.
/* 'name' has never been declared, perhaps you meant 'str'? */
Yeah, the only compiler I have access to is on a Windows workstation, which I'm not allowed to transport any data electronically. So I simply copy from the compiler IDE to the code posting. But thanks for pointing that out. :mrgreen:

Originally Posted by joeprogrammer View Post
Really, when posting code snippets, the least you can do is make sure it works under normal circumstances. The corrections I showed makes it compile and run, although the code snippet still doesn't take into account numbers.

[edit] I have made a couple of other minor fixes to your toLower() function:
  1. /* Converts upper case letters to lower case */
  2. void toLower(char *str) {
  3. int len = strlen(str), i; /* removed the -1 part... why skip the last character? */
  4. for (i=0;i<len;i++)
  5. if ((str[i]>='A' && str[i] <= 'Z')) /* only lowercases letters now*/
  6. str[i] += 32;
  7. }
Oh thanks! That is much better. I'll even make it better such as formatting :cheesy::
  1. /* Converts upper case letters to lower case */
  2. ....
  3. if ((str[i] >= 65 && str[i] <= 90)) /* Only lowercases letters now???? The other one did handle lower case letters I just !=notted the result of the condition:)*/
  4. }

Joe, thanks for all your help....
LamaBot
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 171
Reputation: Lazaro Claiborn is an unknown quantity at this point 
Solved Threads: 13
Lazaro Claiborn's Avatar
Lazaro Claiborn Lazaro Claiborn is offline Offline
Junior Poster

Re: Problem in sorting by name

 
0
  #16
Mar 12th, 2007
Originally Posted by joeprogrammer View Post
When will you learn? If you're going to submit code, make sure it compiles.
Umm.... Lol.... I do make sure. :lol:
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Problem in sorting by name

 
0
  #17
Mar 12th, 2007
>Only lowercases letters now???? The other one did handle lower case letters I just !=notted the result of the condition
No, I used "lowercases" as a verb. Thus, it only converts (or "lowercases") uppercase letters now, not any non-lowercase letter.

And this is just my opinion, but: I don't really see why using numbers in place of characters makes the code any clearer.

>Umm.... Lol.... I do make sure.
Being unable to compile code is not a good excuse for posting lousy code. If you are unable to check the code with a compiler, you'd be better off not posting anything. (You can actually get away with posting code with syntax errors here on the forum as long as you warn the person, but that's ONLY for the forums -- code snippets should be always compile.)
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,629
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Problem in sorting by name

 
0
  #18
Mar 12th, 2007
Lazaro, make sure your code is up to the specified standards like Joey said. I see a lot of beginner style programming which may confuse other people trying to learn the language.

  1. for (i=0;i<len1 && i<len2;i++) {
  2. if (str1[i] < str2[i])
  3. return 1;
  4. else if (str[i] > str2[i])
  5. return 2;
  6. }
  7. return 0 ;
  8. }

It should be something like: (not the exact implementation)

  1. {
  2. for (i=0;i<len1 && i<len2;i++) {
  3. if (str1[i] < str2[i])
  4. return str1 [i] - str2 [i] ;
  5. else if (str[i] > str2[i])
  6. return str1 [i] - str2 [i]
  7. }
  8. return 0 ;
  9. }

though I would even modify the loop to make it better, but then again, its okay.
This is not how string comparision function works. The string comparision functions returns the ASCII difference between the strings if they are unequal while a zero if they are equal.

Why try to write a half baked comparision function when the library itself provides for its implementation.
Last edited by ~s.o.s~; Mar 12th, 2007 at 11:08 pm.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 171
Reputation: Lazaro Claiborn is an unknown quantity at this point 
Solved Threads: 13
Lazaro Claiborn's Avatar
Lazaro Claiborn Lazaro Claiborn is offline Offline
Junior Poster

Re: Problem in sorting by name

 
1
  #19
Mar 12th, 2007
Originally Posted by joeprogrammer View Post
>Only lowercases letters now???? The other one did handle lower case letters I just !=notted the result of the condition
No, I used "lowercases" as a verb. Thus, it only converts (or "lowercases") uppercase letters now, not any non-lowercase letter.
Well considering the context of this debate, any considerate person would clarify such a deceiving addition.

Originally Posted by joeprogrammer View Post
And this is just my opinion, but: I don't really see why using numbers in place of characters makes the code any clearer.
Yeah.... well it is a good thing it is your opinion. :lol:

Originally Posted by joeprogrammer View Post
>Umm.... Lol.... I do make sure.
Being unable to compile code is not a good excuse for posting lousy code. If you are unable to check the code with a compiler, you'd be better off not posting anything. (You can actually get away with posting code with syntax errors here on the forum as long as you warn the person, but that's ONLY for the forums -- code snippets should be always compile.)
Lousy code? Lol, um ok. You tell that to my colleague who happen to see me swiftly write the code and who checked it. As I specified earlier, as you seemed to have ignored, it did compile sucessfully. Perhaps not on the Linux machine I'm on now, but it did on the XP box, but I can just look the code over to make sure. You should be more considerate when make such absolute claims.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 171
Reputation: Lazaro Claiborn is an unknown quantity at this point 
Solved Threads: 13
Lazaro Claiborn's Avatar
Lazaro Claiborn Lazaro Claiborn is offline Offline
Junior Poster

Re: Problem in sorting by name

 
0
  #20
Mar 12th, 2007
Well if he is learning then he is a beginner. So what is your point?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC