Thursday, July 30, 2009

Need code in turbo C++ for the following output?

The user will input 9 characters in the following manner :


ABCDEDCBA


and the program will output :


ABCDEDCBA


ABCD DCBA


ABC CBA


AB BA


A A





If anybody could provide the source code in turbo C++ that would be much appreciated

Need code in turbo C++ for the following output?
See below for my solution. There's nothing 'Turbo' about it, it's just portable code. As you see, I don't require input of 9 chars, just an odd number %26gt; 2 to get the pattern you want. I recommend you do the same. Don't just take my code as-is and pass it off as your own. Study it, understand it, and make it better if you can.





#include %26lt;iostream%26gt;


#include %26lt;string%26gt;





using namespace std;





int main(int argc, char *argv[]) {


string s;


int mid;





do {


cout %26lt;%26lt; endl %26lt;%26lt; "Enter an odd number of 3 or more chars: ";


getline(cin,s);


} while ((s.length() %26lt; 3) || ((s.length() % 2) == 0));


cout %26lt;%26lt; s %26lt;%26lt; endl;


s.replace(mid = s.length()/2,1,1,' ');


do {


cout %26lt;%26lt; s %26lt;%26lt; endl;


s.erase(mid-1,1);


s.erase(mid,1);


} while (mid = s.length()/2);


return 0;


}


No comments:

Post a Comment