| | |
Sorting words alphabetically goes wrong.
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Dec 2007
Posts: 3
Reputation:
Solved Threads: 0
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:
Code:
c++ Syntax (Toggle Plain Text)
#include <stdio.h> #include <string.h> #include <conio.h> #include <iostream.h> #include <stdlib.h> void main () { clrscr(); char rinda[200]; printf("Enter string [ words are separated with space ]\n"); gets(rinda); char * pch; char * pch2[100]; char * temp[100]; int words,j; j=0; words=0; pch = strtok (rinda," "); while (pch != NULL) { pch2[j]=pch; words=words+1; j=j+1; pch = strtok (NULL, " ,.-"); } cout << "Before sorting:" <<endl; for (int x=0;x<words;x++) cout << pch2[x]<<endl; for (int i=1; i<=words; i++) { for ( int z=0;z<=i;z++ ) { if (strcmp(pch2[z],pch2[z+1]) > 0) { temp[z]=pch2[z]; pch2[z]=pch2[z+1]; pch2[z+1]=temp[z]; } } } cout << "After sorting:" <<endl; for (int g=0;g<words;g++) cout << pch2[g]<<endl; getche(); }
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
Maybe
should be:-
C++ Syntax (Toggle Plain Text)
for (int i=1; i<=words; i++) { 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*
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.
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.
C++ Syntax (Toggle Plain Text)
char* temp; for (int i=0; i< words-1; i++) { for ( int z= i+1 ;z < words; z++ ) { if (strcmp(pch2[ i ],pch2[ z ]) > 0) { temp = pch2[i]; pch2[i]=pch2[z]; pch2[z]=temp; } } }
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.
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.
>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):
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.
![]() |
Other Threads in the C++ Forum
- Previous Thread: network program query
- Next Thread: help me
| Thread Tools | Search this Thread |
add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data database delete desktop developer directshow dll dynamic encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory microsoft multidimensional newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive return string strings struct studio template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






