I'm trying to output the two words in a string in reverse order.
For example, if the first word in the string is "Baseball" then the second word is "Basketball".
I want the output to be "Basketball Baseball".
The problem I have with this is that the program returns the answer with a space at the beginning.
I get something like this = " Basketball Baseball"
I don't want that space. I tried using "\b" and that doesn't work.
1 2
for (int i = 0; i < ch.length() / 2; i++)
switch(ch[i], ch[ch.length() - i - 1]);
Is there a way to fix this within the loop or is there a way for me to do it in "cout"?
I don't want to have multiple strings. I know how to do it that way. I want to be able to make the user enter two word separated with a space then I can swap them.
The problem is the for loop. Yours doesn't work on my program neither. The one I showed gives me the closest answer, but it just adds that extra space at the beginning.
None of this helps with a “manipulate a string (array)” homework.
@joe809
I do not see anything weird or wrong with your code (excepting you typed “switch” instead of “swap” or whatever you used to exchange characters).
I suspect there is something amiss with your input method. Can I see that?
The problem is greater now. I was getting closer into fixing it, but it looks worse. I just simply want to return back both input lines. The program is just returning all of the words from the string in different order.
1 2 3 4 5 6 7 8 9 10
Input: Hello World
New Day
Expected Output:
olleH dlroW
weN yaD
My Output:
olleH weN
dlroW
yaD
I know the problem is in the swap() function. I have no idea how to fix it as I have tried several methods and done some tweakings.
PS Before I forget, the challenge now is to do it in a single loop which is the obvious and probably simple refinement I have got time for at the moment.
@lastchance. Without denigrating you valiant attempt at sensible and mature programming <strings> of any sort aren't acceptable in this xyz problem.
Of course feel free to take the 1st, dare I say it, poll position because unlike the 'fatuous' present here, I am driven by pride and the greater good but not by my ego and self-aggrandizement - a trait we have in common despite the different paths.
Now, the only problem I am getting is the both lines are getting returned in the same line.
For example,
Expected Output:
Baskbetall Baseball
Soccer Football
My Output:
Football Soccer Baseball Basketball
I believe the problem might be in the while loop inside of the if ("word"). Maybe it's in the swap() function. I can't tell because if I try to change anything inside of those two, the output changes but incorrectly.
#include <iostream>
#include <sstream>
#include <string>
usingnamespace std;
string transpose( const string &line )
{
string result;
stringstream ss( line );
for ( string word; ss >> word; ) result = word + ' ' + result;
if ( !result.empty() ) result.resize( result.size() - 1 );
return result;
}
int main()
{
stringstream in( "The owl and the pussy-cat went to sea \n""In a beautiful pea-green boat\n""They took some money and plenty of honey\n""Wrapped up in a five-pound note\n" );
for ( string line; getline( in, line ); ) cout << transpose( line ) << '\n';
}
sea to went pussy-cat the and owl The
boat pea-green beautiful a In
honey of plenty and money some took They
note five-pound a in up Wrapped