Hello!
Too tough? eh?![]()
This is a discussion on C/C++ Programming Challenge - try this! within the Computer Science & IT Engineering forums, part of the CE : Technical Discussions category; Here's a puzzle for those who still have brain intact:- Write a method that takes a string as input, and ...

Here's a puzzle for those who still have brain intact:-
Write a method that takes a string as input, and outputs that string with the words in reverse order. Punctuation should remain in its place (i.e. attached to whatever word it was originally with)
Example:
Input: "My this is a nice day!"
Output "day! nice a is this My"
Example 2:
Input: "There is a house in New Orleans, they call the rising Sun. "
Output "Sun. rising the call they Orleans, New in house a is There"
Puzzle Source: Programming Challenge #4 - destraynor
█ Founder & Administrator | CrazyEngineers® | admin{@}crazyengineers{dawt}com
█ About CE | Small Talk | Official Blog 'VoiCE' | Advertise On CE
█ The Big K's SuperBlogmt | Join CE! | Guide: How to post on CE

Hello!
Too tough? eh?![]()
█ Founder & Administrator | CrazyEngineers® | admin{@}crazyengineers{dawt}com
█ About CE | Small Talk | Official Blog 'VoiCE' | Advertise On CE
█ The Big K's SuperBlogmt | Join CE! | Guide: How to post on CE


Hi,
See this code snipped below.
I really like to promote the use of C++/STL.
Thats why i use more of C++ and STL, code become more readable as well.
Regards
Sriram
//###########code snipped############################
#include <iostream>
#include <vector>
#include <cstdlib>
using namespace std;
int main()
{
char myStr[100];
cout << "Input :";
cin.getline(myStr, 100);//myStr);//, "\n");
char *word;
vector<char *> revSent;
word = strtok(myStr, " ");
revSent.push_back(word);
while (word = strtok(NULL, " "))
{
revSent.push_back(word);
}
cout << "Output :";
for(int i = revSent.size() - 1; i >= 0 ; i--)
cout << revSent[i] << " ";
cout << endl;
}
/###########code snipped end########################
Run 1
>./a.out
Input :My this is a nice day!
Output :day! nice a is this My
Run 2
>./a.out
Input :There is a house in New Orleans, they call the rising Sun.
Output :Sun. rising the call they Orleans, New in house a is There
hello,
i found that very easy
here is my code
Code:#include<iostream> #include<algorithm> #include<string> using namespace std; int main() { string in; getline(cin,in); reverse(in.begin(),in.end()); int st = 0; for(int i= 0 ; i < (int)in.size() ; i++) { if(in[i]==' ') { reverse(in.begin()+st,in.begin()+i); st = -1; } if(in[i]!=' '&&st == -1) { st = i; } } if(st !=-1) reverse(in.begin()+st,in.end()); cout<<in<<endl; system("pause"); return 0; }


Hi,
Little changes and now its more efficient and less space utilization.
Regards
Sriram
Code:#include <iostream> #include <cstdlib> using namespace std; void reverseRest() { char *word; word = strtok(NULL, " "); if(word != NULL) reverseRest(); else return; printf("%s ",word); return; } int main() { char myStr[100]; cout << "Input :"; cin.getline(myStr, 100); char *word; word = strtok(myStr, " "); reverseRest(); cout << word << endl; }