| | |
Problem in sorting by name
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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:
Some one pls help me out!
C Syntax (Toggle Plain Text)
#include<string.h> #include<conio.h> #include<stdio.h> int main(void) { clrscr(); int flag,x=0; char name1[20],name2[20],name3[20],ch,*ptr,*ptr2; gets(name1); gets(name2); flag=0; ch=97; for(x=0;x<(strlen(name1)) && x<(strlen(name2)) && flag==0;x++) { ch=97+x; ptr = strrchr(name1,ch); ptr2 = strrchr(name2,ch); if(ptr==ptr2) { flag=0; } if(ptr==0 && ptr2!=0) { strcpy(name3,name1); flag=1; } if(ptr2==0 && ptr!=0) { strcpy(name3,name2); flag=1; } if(ptr<ptr2) { strcpy(name3,name1); } if(ptr>ptr2) { strcpy(name3,name2); } } printf(" %s",name3); getch(); return 0; }
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
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
Good luck, LamaBot
Last edited by Lazaro Claiborn; Mar 12th, 2007 at 4:35 am.
•
•
•
•
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
" Life is like a box of chocolates, you never know what you are going to get " - Forrest Gump
•
•
•
•
C Syntax (Toggle Plain Text)
for(x=0;x<(strlen(name1)) && x<(strlen(name2)) && flag==0;x++) { ch=97+x; ptr = strrchr(name1,ch); ptr2 = strrchr(name2,ch);
c Syntax (Toggle Plain Text)
string name1, name2; int len = name1.length(), len2 = name2.length(), x; for (x=0;x<len-1 || x<len-1;x++) { if (name1[x]>name2[x]) name1.swap(name2) }
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.
I think you you use Turbo C, so
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.
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.
c Syntax (Toggle Plain Text)
#include<string.h> #include<conio.h> #include<stdio.h> int main(void) { clrscr(); char name1[20],name2[20]; int *ptr; gets(name1); gets(name2); int s1 = strlen(name1); int s2 = strlen(name2); (s1<=s2)?ptr=&s1:ptr=&s2; int x, flag=0; for( x=0;x<*ptr;x++) { int ch = (int)name1[x], _ch = (int)name2[x]; // takes one character if(ch<97) { ch+= 32; } // converts to lowercase else if(_ch<97) { _ch+= 32; } // if bot characters are not equal, loop is terminated. // flag is -1 for name2 and 1 for name2 and 0 if equal if(ch!=_ch) { if(ch>_ch)flag=-1; else flag=1; break; } } if(flag==1) puts(name1); else if(flag==-1) puts(name2); getch(); return 0; }
Last edited by vishesh; Mar 12th, 2007 at 6:13 am.
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.
Here is a program that might help you: http://www.daniweb.com/code/snippet660.html
Good luck, LamaBot
Good luck, LamaBot
![]() |
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 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 problem 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






