sorting

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2008
Posts: 5
Reputation: taichou is an unknown quantity at this point 
Solved Threads: 0
taichou taichou is offline Offline
Newbie Poster

sorting

 
0
  #1
Jan 9th, 2009
how can i arranged the input words alphabetically, like in the dictionary..please help me....please!!!!
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 25
Reputation: nitu_thakkar has a little shameless behaviour in the past 
Solved Threads: 1
nitu_thakkar's Avatar
nitu_thakkar nitu_thakkar is offline Offline
Light Poster

Re: sorting

 
0
  #2
Jan 9th, 2009
first intialize one string such as str[12]
take two variable of integer type such as i,j
take one temporary variable of char type such as temp
  1.  
  2. for(i=0;str[i]!='\0';i++) /*loop will be continue untill the it not found the null charater from str string*/
  3. {
  4. for(j=1;str[j]!='\0';j++)
  5. {
  6.  
  7. if(str[j]>str[i]) /*chack whether the next charater is smaller than the cuurent charater */
  8. {
  9. temp=str[j];
  10. str[j]=str[i];
  11. str[i]=temp;
  12. }
  13.  
  14. }
  15. }
  16.  
  17. for(i=0;str[i]!='\0';i++) /*print the string */
  18. {
  19. printf("%c",str[i]);
  20.  
  21. }
  22.  
  23. }
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 561
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 90
Murtan Murtan is offline Offline
Posting Pro

Re: sorting

 
0
  #3
Jan 9th, 2009
@nitu_thakkar

That appears to be a sort for the characters in a string, to put the highest character in the string as the first character.

@taichou

The above sort could be adapted to sort strings in a list.

Where do the words come from?

How do we know how many there are (or will be)?

Once we get them in dictionary order, do we just output the list?
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 357
Reputation: death_oclock will become famous soon enough death_oclock will become famous soon enough 
Solved Threads: 37
death_oclock's Avatar
death_oclock death_oclock is offline Offline
Posting Whiz

Re: sorting

 
0
  #4
Jan 13th, 2009
I would recommend using a bubble sort with the strcmp function as a comparison.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 5
Reputation: taichou is an unknown quantity at this point 
Solved Threads: 0
taichou taichou is offline Offline
Newbie Poster

Re: sorting

 
0
  #5
Jan 14th, 2009
you will input the words...and then after sorting, you will print them in an alphabetical order....tnx for the help..appriciate it...
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 71
Reputation: Alibeg is an unknown quantity at this point 
Solved Threads: 10
Alibeg Alibeg is offline Offline
Junior Poster in Training

Re: sorting

 
0
  #6
Jan 14th, 2009
i agree with death_oclock and what i type here is actually just more detailed explanation of his suggestion

problem is that if you use standard input (keyboard), you will have to make dynamic array, or to predetermine number of maximum words.
bubble sort is pretty good option since it's simple and easy to use, and you will input form keyboard which means there wont be too many entries.
if you know how to use more complex structures as dynamic arrays, then all you need to do is make one such array. dynamic array works in way that expands with each entry and you will know the length of your array. strcmp or strcmpl (same as previous but capital letters are considered the same as lower case letters) are function that compares two strings. strcmp (string1, string2) evaluates (equals) to 0 if two string are identical, evaluates less than 0 if first string is "lower" (alphabetically comes before second one), and evaluates more than 0 if first string is "higher" (goes after the second).
everything said here expects that you know how to work bubble sort, strings, and dynamic array thing is just optional. you can say that there will be no more than 100 words and you will spare yourself from dynamic arrays.
Last edited by Alibeg; Jan 14th, 2009 at 3:46 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 357
Reputation: death_oclock will become famous soon enough death_oclock will become famous soon enough 
Solved Threads: 37
death_oclock's Avatar
death_oclock death_oclock is offline Offline
Posting Whiz

Re: sorting

 
0
  #7
Jan 18th, 2009
taichou, I provided links with the hope that you would read them and maybe learn from them.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 90
Reputation: ajay.krish123 is an unknown quantity at this point 
Solved Threads: 8
ajay.krish123 ajay.krish123 is offline Offline
Junior Poster in Training

Re: sorting

 
0
  #8
Jan 18th, 2009
Originally Posted by taichou View Post
how can i arranged the input words alphabetically, like in the dictionary..please help me....please!!!!
The simple program on alphabetical sorting is:
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<string.h>
  4. void main()
  5. {
  6. clrscr();
  7. char a[6][7];
  8. int i,j,p;
  9. char c[10];
  10. printf("enter the name:");
  11. for(i=0;i<6;i++)
  12. scanf("%s",&a[i]);
  13. printf("the names entered are:");
  14. for(i=0;i<6;i++)
  15. {
  16. printf("%s",a[i]);
  17. printf("\n");
  18. }
  19. for(i=0;i<6;i++)
  20. {
  21. for(j=i;j<6;j++)
  22. {
  23. p=strcmp(a[i],a[j+1]);
  24. if(p>0)
  25. {
  26. strcpy(c,a[i]);
  27. strcpy(a[i],a[j+1]);
  28. strcpy(a[j+1],c);
  29. }
  30. }
  31. }
  32. printf("After sorting:\n");
  33. for(i=0;i<6;i++)
  34. printf("%s\n",a[i]);
  35. getch();
  36. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,033
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: sorting

 
0
  #9
Jan 18th, 2009
Your indentation could use some more noticeable spaces.
#include<conio.h> is not a standard header file.
clrscr(); and getch(); are not portable C standard functions.
void main() should always return an int, except when there's not host operating system.
printf("enter the name:"); you ask for a name, but expect six to be entered via a loop.
scanf("%s",&a[i]); a[i] is a pointer already. What if the input is more than seven which is the limit of char a[i][7]? scanf() should always be limited on reading and checked at return for failure.
strcmp(a[i],a[j+1]); strcmp(a[5], a[5+1]); What's in a[6]? Oh, there's not an a[6]. Same it is applicable for strcpy()
Last edited by Aia; Jan 18th, 2009 at 2:01 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 90
Reputation: ajay.krish123 is an unknown quantity at this point 
Solved Threads: 8
ajay.krish123 ajay.krish123 is offline Offline
Junior Poster in Training

Re: sorting

 
0
  #10
Jan 19th, 2009
thank you for correcting me.
we can use
  1. int main()
  2. {
  3. ----
  4. ----
  5. return(0);
  6. }
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