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:

#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();
}
Member Avatar for iamthwee

Maybe

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++ ) {

It doesn't work, inputting "x b a" it prints " b a x"

Member Avatar for iamthwee

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.

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.

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;
     }
  }
}

Ooo, looks like everything is ok now. Thank you Ancient Dragon.

do u want another program than urs if yes plz give me about 20 min reply plz

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.

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!

>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.

ok ok i dont have any problem with ur policies in fact i like ur policies cause its the first time that i can see a real website.... i am not kidding c ya

>in fact i like ur policies
Then could you please stop using those silly chat abbreviations like u, ur, and c ya? Not everyone is well versed in that particular language, it's unprofessional, and that's precisely why you received a warning.

hmm ok i can see your (...) rules are very tough can u please send me the link of the webpage describing the rules of this website

>i can see your (...) rules are very tough
Hardly, as long as you have a modicum of common sense.

>can u please send me the link of the webpage describing the rules of this website
Well, you could click on the big fat link at the top of every page that says "RULES", but I'll give you another link anyway, so that you don't miss it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.