Sorting words alphabetically goes wrong.

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2007
Posts: 3
Reputation: iCare is an unknown quantity at this point 
Solved Threads: 0
iCare iCare is offline Offline
Newbie Poster

Sorting words alphabetically goes wrong.

 
0
  #1
Dec 2nd, 2007
I'm using Borland C++ and i need to make program where you enter a string in which the words are seprated by space. After input i need to sort these words into alphabetical order, but something is wrong. Cause when i enter like " x b a" it prints out "a b" but when i enter "abc abb aba" it sorts it ok. Where is the problem? I would be really thankful if anybody could help me. Thank you.

Code:

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <conio.h>
  4. #include <iostream.h>
  5. #include <stdlib.h>
  6.  
  7. void main ()
  8. {
  9. clrscr();
  10. char rinda[200];
  11. printf("Enter string [ words are separated with space ]\n");
  12. gets(rinda);
  13. char * pch;
  14. char * pch2[100];
  15. char * temp[100];
  16. int words,j;
  17. j=0;
  18. words=0;
  19. pch = strtok (rinda," ");
  20. while (pch != NULL)
  21. {
  22. pch2[j]=pch;
  23. words=words+1;
  24. j=j+1;
  25. pch = strtok (NULL, " ,.-");
  26. }
  27. cout << "Before sorting:" <<endl;
  28. for (int x=0;x<words;x++)
  29. cout << pch2[x]<<endl;
  30.  
  31. for (int i=1; i<=words; i++) {
  32. for ( int z=0;z<=i;z++ ) {
  33. if (strcmp(pch2[z],pch2[z+1]) > 0) {
  34. temp[z]=pch2[z];
  35. pch2[z]=pch2[z+1];
  36. pch2[z+1]=temp[z];
  37. }
  38. }
  39. }
  40. cout << "After sorting:" <<endl;
  41. for (int g=0;g<words;g++)
  42. cout << pch2[g]<<endl;
  43.  
  44. getche();
  45. }
Last edited by Ancient Dragon; Dec 2nd, 2007 at 9:36 am. Reason: add line numbers to code tags, you did not violate any DaniWeb rules, this is just for convenience
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
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: Sorting words alphabetically goes wrong.

 
0
  #2
Dec 2nd, 2007
Maybe

  1. for (int i=1; i<=words; i++) {
  2. for ( int z=0;z<=i;z++ ) {

should be:-

for (int i=0; i<words; i++) {
  for ( int z=0;z<i;z++ ) {
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 3
Reputation: iCare is an unknown quantity at this point 
Solved Threads: 0
iCare iCare is offline Offline
Newbie Poster

Re: Sorting words alphabetically goes wrong.

 
0
  #3
Dec 2nd, 2007
It doesn't work, inputting "x b a" it prints " b a x"
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
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: Sorting words alphabetically goes wrong.

 
0
  #4
Dec 2nd, 2007
I'm guessing there is something wrong with your sorting or you need to use strcpy to swap your variables. Using gets() and all the other misnomers probably doesn't help matters.
Last edited by iamthwee; Dec 2nd, 2007 at 8:12 am.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,508
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1480
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Sorting words alphabetically goes wrong.

 
0
  #5
Dec 2nd, 2007
line 12: never ever use gets() because it can destroy your program. Use fgets() instead.

line 15: you don't need an array of temp pointers. Just one temp pointer will do.

I've always coded the bubble sort like this: Note that the inner loop begins one slot beyond the current position of the outer loop. There is no point in checking positions 0 to i because they have already been sorted.
  1. char* temp;
  2. for (int i=0; i< words-1; i++) {
  3. for ( int z= i+1 ;z < words; z++ ) {
  4. if (strcmp(pch2[ i ],pch2[ z ]) > 0) {
  5. temp = pch2[i];
  6. pch2[i]=pch2[z];
  7. pch2[z]=temp;
  8. }
  9. }
  10. }
Last edited by Ancient Dragon; Dec 2nd, 2007 at 9:46 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 3
Reputation: iCare is an unknown quantity at this point 
Solved Threads: 0
iCare iCare is offline Offline
Newbie Poster

Re: Sorting words alphabetically goes wrong.

 
0
  #6
Dec 2nd, 2007
Ooo, looks like everything is ok now. Thank you Ancient Dragon.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 34
Reputation: tarekkkkk is an unknown quantity at this point 
Solved Threads: 0
tarekkkkk's Avatar
tarekkkkk tarekkkkk is offline Offline
Light Poster

Re: Sorting words alphabetically goes wrong.

 
0
  #7
Dec 2nd, 2007
do u want another program than urs if yes plz give me about 20 min reply plz
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,117
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 282
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Sorting words alphabetically goes wrong.

 
0
  #8
Dec 2nd, 2007
tarekkkkk, please use full English so we can all understand you. Don't use web-speak because a lot of people here don't understand it.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 34
Reputation: tarekkkkk is an unknown quantity at this point 
Solved Threads: 0
tarekkkkk's Avatar
tarekkkkk tarekkkkk is offline Offline
Light Poster

Ok try next time messaging me first!!!!

 
0
  #9
Dec 3rd, 2007
Ok its not a big deal for me to speak pure english or british but why did u make a warning stuff about my language well u can just send me a private message about ur problem with me and sorry i dont know u try next time introducing urself to me c ya until next time!
Last edited by tarekkkkk; Dec 3rd, 2007 at 11:32 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,787
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 745
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Sorting words alphabetically goes wrong.

 
0
  #10
Dec 3rd, 2007
>but why did u make a warning stuff about my language
Because that's what our policies dictate (see the emphasized areas for your specific case):
Keep It Clean
We strive to be a community geared towards the professional. We strongly encourage all posts to be in full-sentence English. Please do not use "leet" speak or "chatroom" speak.

Our privacy policy requires members to be at least 13 years of age, but demographic surveys have shown the average age of our members to be over 30. Mature language and concepts are therefore acceptable on DaniWeb to the point that would be acceptable on daytime family television, but offensive postings are not. To ensure that standards are maintained we have implemented a filter that will edit out inappropriate language in your postings. However, common sense should prevail and if you are unsure if a word is acceptable or not, please don’t post it. It is prohibited to use alternative characters in an attempt to circumvent the bad words filter.

What will happen: Offensive material will be snipped out of offending post or censored by bad words filter.
First offense: Member will be warned via user infraction system. Warning will be documented.
Second offense: 5 point infraction will remain on user's record for 3 months.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC