943,929 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 767
  • C RSS
Jan 9th, 2009
0

sorting

Expand Post »
how can i arranged the input words alphabetically, like in the dictionary..please help me....please!!!!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
taichou is offline Offline
5 posts
since Sep 2008
Jan 9th, 2009
0

Re: sorting

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. }
Reputation Points: 0
Solved Threads: 1
Light Poster
nitu_thakkar is offline Offline
25 posts
since Jan 2009
Jan 9th, 2009
0

Re: sorting

@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?
Reputation Points: 344
Solved Threads: 116
Practically a Master Poster
Murtan is offline Offline
670 posts
since May 2008
Jan 13th, 2009
0

Re: sorting

I would recommend using a bubble sort with the strcmp function as a comparison.
Reputation Points: 128
Solved Threads: 43
Posting Whiz
death_oclock is offline Offline
389 posts
since Apr 2006
Jan 14th, 2009
0

Re: sorting

you will input the words...and then after sorting, you will print them in an alphabetical order....tnx for the help..appriciate it...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
taichou is offline Offline
5 posts
since Sep 2008
Jan 14th, 2009
0

Re: sorting

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.
Reputation Points: 11
Solved Threads: 11
Junior Poster in Training
Alibeg is offline Offline
81 posts
since Aug 2008
Jan 18th, 2009
0

Re: sorting

taichou, I provided links with the hope that you would read them and maybe learn from them.
Reputation Points: 128
Solved Threads: 43
Posting Whiz
death_oclock is offline Offline
389 posts
since Apr 2006
Jan 18th, 2009
0

Re: sorting

Click to Expand / Collapse  Quote originally posted by taichou ...
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. }
Reputation Points: 6
Solved Threads: 9
Junior Poster in Training
ajay.krish123 is offline Offline
90 posts
since Nov 2008
Jan 18th, 2009
0

Re: sorting

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.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Jan 19th, 2009
0

Re: sorting

thank you for correcting me.
we can use
  1. int main()
  2. {
  3. ----
  4. ----
  5. return(0);
  6. }
Reputation Points: 6
Solved Threads: 9
Junior Poster in Training
ajay.krish123 is offline Offline
90 posts
since Nov 2008

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: bitwise shift opratore....
Next Thread in C Forum Timeline: how to learn c





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC