| | |
Problem in sorting by name
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
>Then I suggest you get a new compiler
I suggest that you fix the 2 typos that you made.
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:
I suggest that you fix the 2 typos that you made.
c Syntax (Toggle Plain Text)
/* Converts upper case letters to lower case */ void toLower(char *str) { int len = strlen(str)-1, i; for (i=0;i<len;i++) /* 'name' has never been declared, perhaps you meant 'str'? */ if (!(name[i]>=97 && name[i] <= 122)) name[i] += 32; }
c Syntax (Toggle Plain Text)
int cmpstr(char *str1, char *str2) { int len1, len2, i; len1 = strlen(str1); len2 = strlen(str2); for (i=0;i<len1 && i<len2;i++) { if (str1[i] < str2[i]) return 1; /* change 'str' to 'str1' */ else if (str[i] > str2[i]) return 2; } return 0; }
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:
c Syntax (Toggle Plain Text)
/* Converts upper case letters to lower case */ void toLower(char *str) { int len = strlen(str), i; /* removed the -1 part... why skip the last character? */ for (i=0;i<len;i++) if ((str[i]>='A' && str[i] <= 'Z')) /* only lowercases letters now*/ str[i] += 32; }
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.
All my posts may be freely redistributed under the terms of the MIT license.
Here is another example program that might help you:
http://www.daniweb.com/code/showsnippet.php?codeid=661
http://www.daniweb.com/code/showsnippet.php?codeid=661
•
•
•
•
Here is another example program that might help you:
http://www.daniweb.com/code/snippet661.html
"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.
All my posts may be freely redistributed under the terms of the MIT license.
•
•
•
•
>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'? */
•
•
•
•
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:
c Syntax (Toggle Plain Text)
/* Converts upper case letters to lower case */ void toLower(char *str) { int len = strlen(str), i; /* removed the -1 part... why skip the last character? */ for (i=0;i<len;i++) if ((str[i]>='A' && str[i] <= 'Z')) /* only lowercases letters now*/ str[i] += 32; }
c Syntax (Toggle Plain Text)
/* Converts upper case letters to lower case */ .... 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:)*/ }
Joe, thanks for all your help....
LamaBot
>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.)
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.
All my posts may be freely redistributed under the terms of the MIT license.
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.
It should be something like: (not the exact implementation)
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.
c Syntax (Toggle Plain Text)
for (i=0;i<len1 && i<len2;i++) { if (str1[i] < str2[i]) return 1; else if (str[i] > str2[i]) return 2; } return 0 ; }
It should be something like: (not the exact implementation)
c Syntax (Toggle Plain Text)
{ for (i=0;i<len1 && i<len2;i++) { if (str1[i] < str2[i]) return str1 [i] - str2 [i] ; else if (str[i] > str2[i]) return str1 [i] - str2 [i] } return 0 ; }
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.
•
•
•
•
>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.)
You should be more considerate when make such absolute claims. ![]() |
Similar Threads
- Sorting and Searching to get rid of Duplicate Numbers (C++)
- Need Urgent Help in Sorting (Shell Scripting)
- Problem of sorting words of each string using pointers (C++)
- Need Help with ArrayList sorting (Java)
Other Threads in the C Forum
- Previous Thread: threading
- Next Thread: Operation on Binary number, Need source code for elementar operation (+-*/)
| Thread Tools | Search this Thread |
adobe ansi api array arrays asterisks binarysearch calculate centimeter char convert copyanyfile copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax directory dynamic fflush file fork forloop frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop initialization interest kernel km linked linkedlist linux linuxsegmentationfault list lists locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix power probleminc program programming pyramidusingturboccodes radix read recursion recv repetition research scanf scheduling scripting segmentationfault send sequential shape socketprograming stack standard string strings structures systemcall testautomation turboc unix user variable voidmain() wab win32api windows.h




Lol. 

