Well, I bumped into an interesting C++ problem last night while I was aimlessly surfing the Internet, and, of course, it caught my attention. It said something like this:
"From a file it's read a text that contains a phrase, on a line. The program rearranges the letters in each phrase, in alphabetical order, keeping the words' place. only letters from 'A' to 'Z' are affected. The other chars remain untouched. Print on the screen the result."
Too bad I don't remember the link, but I'm sure this was the requirement.
It looked quite simple, but, what I got from my ideas was..uhh NULL...

I thought I could find the number of words and then somehow, using Bubble sort, to put them in alphabetical order. Of course, I got nothing but a broken mind :|
If you have any ideas, I'd like to know how this thing it's solved :)

PS: I'm posting what I tried to make by myself, too :P

#include <iostream.h>
#include <string.h>

int main ()
{

		char text[100];
		int i, ok;

		cout<<"text ";
		cin.get (text, 100);

		do
		{
			ok=0;
			for (i=0; i<strlen (text )-1; i++)
			if (text[i] > text[i+1])
			{
				char x=text[i];
				text[i]=text[i+1];
				text[i]=x;
				ok=1;
			}
		}
		while (ok==1);
			 for (i=0; i<strlen (text); i++)

		cout<<text[i]<<" ";
		cout<<endl;

return 0;
}

Recommended Answers

All 10 Replies

Er, I'm not really sure what you are trying to do with your do loop.

But here's how I would go about it. If I had time, I'd write the actual code but I'm just procrastinating for another project...

while  (until the end of line character)
   if letter is between 65~90 ascii value
        put it in an arrary

sort the array (using bubble sort or w/e you want)

while (until the end of line character)
   if letter is between 65~90 ascii value
       replace the letter with the sorted letter from the sorted array

Okay, so I tried your idea, though something went wrong (probably because of me) :-/
Anyway, here's the monster I created, and which gave an error that closed my poor Borland twice :(

LE: It should've appeared something like AEE EEHMMNO RST TT. if the entrance variables were THE MONSTER ATE ME.

#include <iostream.h>
#include <string.h>

int main ()
{
		char text[100], lol[100], x;
                int i, j, k, ok=0;

		cout<<"text ";
		cin.get (text, 100);

		while ( i<strlen(text) )
		{
			if ( text[i]>=65 && text[i]<=90 )
			{
			lol[j]=text[i];
			j++;
			}
		i++;
		}

		do
		{
			ok=0;
			for (j=0; j<strlen (lol)-1; j++)
			if ( lol[j] > lol[j+1])
			{
				x=lol[j];
				lol[j]=lol[j+1];
				lol[j+1]=x;
				ok=1;
			}
		}
		while (ok==1);


		while ( i<strlen(text) )
		{
			if (text[i]>=65 && text[i]<=90)
			{
			text[i]=lol[j];
			j++;
			}
		i++;
		}

		cout<<lol;
return 0;
}

Because when you are comparing

if ( text[i]>=65 && text[i]<=90 )
			{
			lol[j]=text[i];
			j++;
			}

what you are doing is you are comparing the content of the array which is not an int. So what you want to do is compare those numbers with ascii representation of the content in the array, rather than the content itself.

You're right :P
Even if I changed there, I still get thrown out from my program :|
I modified the code into this:

if ( text[i]>='A' && text[i]<='Z' )
			{
			lol[j]=text[i];
			j++;
			}
int main()
{
    vector<int>  positions;
    list<char> letters;
    ifstream file("phrases.txt");
    string line;
    list<char>::iterator c;
    size_t i;

    while (getline(file,line)) {
        positions.clear();
        letters.clear();
        for (i = 0; i < line.length(); ++i)
            if (isalpha(line[i])) {
                letters.push_back(line[i]);
                positions.push_back(i);
            }
        letters.sort();
        for (i = 0, c = letters.begin();
            c != letters.end();
            ++c, ++i)
            line[positions[i]] = *c;
        cout << line << '\n';
    }
    cout.flush();

    return 0;
}

Homework: what STL headers needed to compile this improvisation?..

Hmm thanks for your reply :)
But, because I'm just a self-taught C++ newbie, it's gonna take me some time to understand that code you got there.

Thanks again ;)

Well, the code above isn't correct. That takes all alphabets, regardless whether it is a capital letter or a small letter, push it into a vector, sort and prints them out. (vector is like array but more convenient, you can use either) And since what you want to do is take ONLY capital letters and sort them. Am I right?

So for example if it's MONSTER ate Me. Then it will be EMMNORS ate Te. Is this what you are looking for? Because that's how I understood it. If so, you are still better off using ascii value for comparison. Just think about what you are actually comparing before you try to compile it.


Btw, i think the HW answer would be
<iostream>
<fstream>
<vector>
<string>
?

Well, tbh, I didn't understand too much of that code, it simply looks like Japanese to me ( I only know Japanese from anime, so go figure), so I can't really tell if it's right or not.
My C++ level it's kinda n00bish :P (it's the most exciting hobby I ever had)
And that's why I'm still trying the idea to use two vectors, one to keep the original phrase, and another one to keep the sort phrase, so somehow I will replace the original one with the sort one, keeping the spaces, and the printing it. Somehow, it must be possible, right ? :P

Well, I'm a noob myself. Just read the code. Good amount of standard built in functions are very readable. For instance, when you see letters.begin() or letters.end() you know what that means. I've never used vector in my life but I can safely guess what those functions do. If not, go search for reference manual. Keep us updated.

True, but I was simply trying to somehow "translate" that dude's code in something I can actually understand. :)
Thanks for help, Freudian_slip, cheers !

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.