CrazyEngineers V4: Early Access
We are developing the next version of CrazyEngineers. If you wish to receive latest updates and early access, click the link below.
C/C++ Programming Challenge - try this!
Question asked by Kaustubh Katdare in #Programming on Oct 10, 2008

Kaustubh Katdare · Oct 10, 2008
Rank A1 - PRO
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 Posted in: #Programming
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 Posted in: #Programming

Kaustubh Katdare · Oct 13, 2008
Rank A1 - PRO
Hello!
Too tough? eh?
Too tough? eh?

sriramchandrk · Oct 13, 2008
Rank D1 - MASTER
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
#include
#include
using namespace std;
int main()
{
char myStr[100];
cout << "Input :";
cin.getline(myStr, 100);//myStr);//, "\n");
char *word;
vector 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 << " ";
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
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
#include
#include
using namespace std;
int main()
{
char myStr[100];
cout << "Input :";
cin.getline(myStr, 100);//myStr);//, "\n");
char *word;
vector
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 << " ";
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

emYth · Oct 13, 2008
Rank E1 - BEGINNER
hello,
i found that very easy
here is my code
i found that very easy
here is my code
#include#include #include 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<

sriramchandrk · Oct 13, 2008
Rank D1 - MASTER
Hi,
Little changes and now its more efficient and less space utilization.
Regards
Sriram
Little changes and now its more efficient and less space utilization.
Regards
Sriram
#include#include 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; }

Manish Goyal · Oct 16, 2010
Rank A2 - PRO
@big_k
i haven't seen any programming challenges like this from you since the day i had joined ce .Please bring in more if you encountered any
i haven't seen any programming challenges like this from you since the day i had joined ce .Please bring in more if you encountered any

Morningdot Hablu · Oct 16, 2010
Rank B2 - LEADER
I didn't seen this thread.
I also want to participate in this challenge but didn't have window os.So either i have to use virtual machine or g++ compiler.
It may need some time to start working with g++.
Till now i post my university question here.Try to solve this.
You have to write a program which take the input in numeric number but it will display that output in words.
.
Input-> 123.
Output-> one two three.
I also want to participate in this challenge but didn't have window os.So either i have to use virtual machine or g++ compiler.
It may need some time to start working with g++.
Till now i post my university question here.Try to solve this.
You have to write a program which take the input in numeric number but it will display that output in words.
.
Input-> 123.
Output-> one two three.

sam!! · Dec 6, 2011
Rank E2 - BEGINNER
here is my code...
#include#include using namespace std; int main() { int N,i,j; char a[10],b[10]; cout<<"\n enter no of string :"; cin>>N; string k[N],t[N]; cout<<"\n enter string : "; for(i=0;i >k; } cout<<"\n reverse string is : "; for(i=N-1,j=0;i>=1,j

Kaustubh Katdare · Dec 9, 2011
Rank A1 - PRO
Posting code on behalf of CEan Simplycoder:-
@mohit007kumar00
@mohit007kumar00
#include#include #include /*Author : simplycoder*/ long reverse(long n); long reverse_remove(long p); void process(long num); int main(int argc, char *argv[]) { long num=0; scanf("%ul",&num); process(num); printf("\n\n\n"); system("PAUSE"); return 0; } void process(long num) { while(num!=0) { num=reverse(num); switch(num%10) { case 0: printf(" zero"); break; case 1: printf(" one"); break; case 2: printf(" two"); break; case 3: printf(" three"); break; case 4: printf(" four"); break; case 5: printf(" five"); break; case 6: printf(" six"); break; case 7: printf(" seven"); break; case 8: printf(" eight"); break; case 9: printf(" nine"); break; } num=reverse_remove(num); num=reverse(num); } printf("."); } long reverse(long n) { long p=0; int l=0; while(n!=0) { l=n%10; p=p*10+l; n/=10; } return p; } long reverse_remove(long p) { return (p-p%10); }

simplycoder · Dec 9, 2011
Rank B3 - LEADER
@BigK: Thank you.

Divisha Madupalli · Feb 12, 2020
Rank C1 - EXPERT
#include
#include
#include
using namespace std;
int main()
{
string s;
cin>>s;
reverse(s.begin(),s.end());
int delim = 0;
for(int i= 0 ; i < s.length() ; i++)
{
if(s[i]==' ')
{
reverse(s.begin()+delim,s.begin()+i);
delim = -1;
}
if(s[i]!=' '&&delim == -1)
{ delim = i;
}
} if(delim !=-1)
reverse(s.begin()+delim,s.end());
cout<