943,766 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 8157
  • C RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Mar 12th, 2007
0

Re: Problem in sorting by name

>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.
Reputation Points: 11
Solved Threads: 13
Junior Poster
Lazaro Claiborn is offline Offline
171 posts
since Jan 2007
Mar 12th, 2007
0

Re: Problem in sorting by name

>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.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Mar 12th, 2007
0

Re: Problem in sorting by name

Here is another example program that might help you:
http://www.daniweb.com/code/showsnippet.php?codeid=661
Reputation Points: 11
Solved Threads: 13
Junior Poster
Lazaro Claiborn is offline Offline
171 posts
since Jan 2007
Mar 12th, 2007
0

Re: Problem in sorting by name

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.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Mar 12th, 2007
0

Re: Problem in sorting by name

>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:

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
Reputation Points: 11
Solved Threads: 13
Junior Poster
Lazaro Claiborn is offline Offline
171 posts
since Jan 2007
Mar 12th, 2007
0

Re: Problem in sorting by name

When will you learn? If you're going to submit code, make sure it compiles.
Umm.... Lol.... I do make sure. :lol:
Reputation Points: 11
Solved Threads: 13
Junior Poster
Lazaro Claiborn is offline Offline
171 posts
since Jan 2007
Mar 12th, 2007
0

Re: Problem in sorting by name

>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.)
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Mar 12th, 2007
0

Re: Problem in sorting by name

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.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Mar 12th, 2007
1

Re: Problem in sorting by name

>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.

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:

>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.
Reputation Points: 11
Solved Threads: 13
Junior Poster
Lazaro Claiborn is offline Offline
171 posts
since Jan 2007
Mar 12th, 2007
0

Re: Problem in sorting by name

Well if he is learning then he is a beginner. So what is your point?
Reputation Points: 11
Solved Threads: 13
Junior Poster
Lazaro Claiborn is offline Offline
171 posts
since Jan 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: threading
Next Thread in C Forum Timeline: Operation on Binary number, Need source code for elementar operation (+-*/)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC