943,651 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 8157
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Mar 12th, 2007
0

Problem in sorting by name

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
ankit_the_hawk is offline Offline
44 posts
since Sep 2005
Mar 12th, 2007
0

Re: Problem in sorting by name

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

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!
Reputation Points: 10
Solved Threads: 0
Light Poster
ankit_the_hawk is offline Offline
44 posts
since Sep 2005
Mar 12th, 2007
0

Re: Problem in sorting by name

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

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.
Reputation Points: 85
Solved Threads: 42
Nearly a Posting Virtuoso
vishesh is offline Offline
1,362 posts
since Oct 2006
Mar 12th, 2007
0

Re: Problem in sorting by name

Yeah.... the code I'd posted was an example. I indicated this when I said
Quote ...
...., but it is an example.
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

strcmp() doesn't interest you...oh well.:rolleyes:
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Mar 12th, 2007
0

Re: Problem in sorting by name

Ankit, like Iamthwee said, strcmp ( ) coupled with qsort ( ) function should work out to be fine in your case.
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
0

Re: Problem in sorting by name

Here is a program that might help you: http://www.daniweb.com/code/snippet660.html

Good luck, 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

>Here is a program that might help you
Somehow I kind of doubt that when the code doesn't even compile. :rolleyes:
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006

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