Problem in sorting by name

Reply

Join Date: Sep 2005
Posts: 44
Reputation: ankit_the_hawk is an unknown quantity at this point 
Solved Threads: 0
ankit_the_hawk's Avatar
ankit_the_hawk ankit_the_hawk is offline Offline
Light Poster

Problem in sorting by name

 
0
  #1
Mar 12th, 2007
I am having a problem in sorting by name. for eg: if given two names Sameer and Sean, I want to print Sameer and not Sean. It needs to check character by character. I have to extend this to a greater no. of names but I need to get the basic right first. Here is the code i have written:
  1.  
  2. #include<string.h>
  3. #include<conio.h>
  4. #include<stdio.h>
  5. int main(void)
  6. {
  7. clrscr();
  8. int flag,x=0;
  9. char name1[20],name2[20],name3[20],ch,*ptr,*ptr2;
  10. gets(name1);
  11. gets(name2);
  12. flag=0;
  13. ch=97;
  14. for(x=0;x<(strlen(name1)) && x<(strlen(name2)) && flag==0;x++)
  15. {
  16. ch=97+x;
  17. ptr = strrchr(name1,ch);
  18. ptr2 = strrchr(name2,ch);
  19. if(ptr==ptr2)
  20. {
  21. flag=0;
  22. }
  23. if(ptr==0 && ptr2!=0)
  24. {
  25. strcpy(name3,name1);
  26. flag=1;
  27. }
  28. if(ptr2==0 && ptr!=0)
  29. {
  30. strcpy(name3,name2);
  31. flag=1;
  32. }
  33. if(ptr<ptr2)
  34. {
  35. strcpy(name3,name1);
  36. }
  37. if(ptr>ptr2)
  38. {
  39. strcpy(name3,name2);
  40. }
  41. }
  42. printf(" %s",name3);
  43. getch();
  44. return 0;
  45. }
Some one pls help me out!
Last edited by ankit_the_hawk; Mar 12th, 2007 at 4:09 am.
" Life is like a box of chocolates, you never know what you are going to get " - Forrest Gump
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
  #2
Mar 12th, 2007
It is not good practice to use strlen in a for loop, especially for loops that must loop many times because strlen iterates through the array of characters one-by-one every time called (i.e. every loop) and takes up a lot of processor time. You could get the strlen then just used the variables.

Good luck, LamaBot
Last edited by Lazaro Claiborn; Mar 12th, 2007 at 4:35 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 44
Reputation: ankit_the_hawk is an unknown quantity at this point 
Solved Threads: 0
ankit_the_hawk's Avatar
ankit_the_hawk ankit_the_hawk is offline Offline
Light Poster

Re: Problem in sorting by name

 
0
  #3
Mar 12th, 2007
Originally Posted by Lazaro Claiborn View Post
It is not good practice to use strlen in a for loop, especially for loops that must loop many times because strlen iterates through the array of characters one-by-one every time called (i.e. every loop) and takes up a lot of processor time. You could get the strlen then just used the variables.

Good luck, LamaBot
Ya rite. thnx. I will keep that in mind. Still waiting for the solution though!
" Life is like a box of chocolates, you never know what you are going to get " - Forrest Gump
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
  #4
Mar 12th, 2007
Originally Posted by ankit_the_hawk View Post
  1. for(x=0;x<(strlen(name1)) && x<(strlen(name2)) && flag==0;x++)
  2. {
  3. ch=97+x;
  4. ptr = strrchr(name1,ch);
  5. ptr2 = strrchr(name2,ch);
What happens if a string doesn't contain a character for ascii 97 to 97+strlen in its name? If strchr encountered a character not located in the string, it'll return NULL. What if the strlen of name1 is larger than name2? You'd be checking 8 char's for name1 and matching character 97+8 to name2 which could contain only 5 char's and even then, not all the ascii char's were checked since you're checking from 97+strlen of a given name, for instance. You could do something like this:
  1. string name1, name2;
  2. int len = name1.length(), len2 = name2.length(), x;
  3. for (x=0;x<len-1 || x<len-1;x++) {
  4. if (name1[x]>name2[x])
  5. name1.swap(name2)
  6. }

The above uses strings and not character arrays, but it is an example. Last, you might want to convert the string to check on lower case characters in my example, because character at x might be larger in name1 if it is lowercase but the alpha character still comes before x in name2 which is a capital letter and thus will always be smaller than any of its lower case counter parts.

Good luck, LamaBot
Last edited by Lazaro Claiborn; Mar 12th, 2007 at 5:10 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 1,311
Reputation: vishesh is on a distinguished road 
Solved Threads: 36
vishesh's Avatar
vishesh vishesh is offline Offline
Nearly a Posting Virtuoso

Re: Problem in sorting by name

 
0
  #5
Mar 12th, 2007
I think you you use Turbo C, so string class wont work with your compiler, because Turbo C++ is old compiler and supports AT&T C++ not ANSI C++.

And yes you should also not use gets() and puts() functions. Also clrscr() and getch() are also not standardised.

Here's the code. I have removed some unnecessary variables.

  1. #include<string.h>
  2. #include<conio.h>
  3. #include<stdio.h>
  4.  
  5.  
  6. int main(void)
  7. {
  8. clrscr();
  9.  
  10. char name1[20],name2[20];
  11. int *ptr;
  12. gets(name1);
  13. gets(name2);
  14.  
  15. int s1 = strlen(name1);
  16. int s2 = strlen(name2);
  17.  
  18. (s1<=s2)?ptr=&s1:ptr=&s2;
  19.  
  20. int x, flag=0;
  21.  
  22. for( x=0;x<*ptr;x++)
  23. {
  24.  
  25. int ch = (int)name1[x], _ch = (int)name2[x]; // takes one character
  26.  
  27. if(ch<97) { ch+= 32; } // converts to lowercase
  28. else if(_ch<97) { _ch+= 32; }
  29.  
  30. // if bot characters are not equal, loop is terminated.
  31. // flag is -1 for name2 and 1 for name2 and 0 if equal
  32. if(ch!=_ch)
  33. {
  34. if(ch>_ch)flag=-1; else flag=1;
  35. break;
  36. }
  37. }
  38.  
  39. if(flag==1)
  40. puts(name1);
  41. else if(flag==-1)
  42. puts(name2);
  43.  
  44. getch();
  45. return 0;
  46. }
Last edited by vishesh; Mar 12th, 2007 at 6:13 am.
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
  #6
Mar 12th, 2007
Yeah.... the code I'd posted was an example. I indicated this when I said
...., but it is an example.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Problem in sorting by name

 
0
  #7
Mar 12th, 2007
strcmp() doesn't interest you...oh well.:rolleyes:
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,609
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: 464
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
  #8
Mar 12th, 2007
Ankit, like Iamthwee said, strcmp ( ) coupled with qsort ( ) function should work out to be fine in your case.
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

 
0
  #9
Mar 12th, 2007
Here is a program that might help you: http://www.daniweb.com/code/snippet660.html

Good luck, LamaBot
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
  #10
Mar 12th, 2007
>Here is a program that might help you
Somehow I kind of doubt that when the code doesn't even compile. :rolleyes:
"Technological progress is like an axe in the hands of a pathological criminal."
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