View Feed
group-icon
Coffee Room
Discuss anything here - everything that you wish to discuss with fellow engineers.
12921 Members
Join this group to post and comment.
Kaustubh Katdare
Kaustubh Katdare • Oct 10, 2008

C/C++ Programming Challenge - try this!

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
Kaustubh Katdare
Kaustubh Katdare • Oct 13, 2008
Hello!

Too tough? eh?
sriramchandrk
sriramchandrk • Oct 13, 2008
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
emYth
emYth • Oct 13, 2008
hello,
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
sriramchandrk • Oct 13, 2008
Hi,

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;
}
 
@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 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.
sam!!
sam!! • Dec 6, 2011
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
Kaustubh Katdare • Dec 9, 2011
Posting code on behalf of CEan Simplycoder:-

@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
simplycoder • Dec 9, 2011
@BigK: Thank you.

Share this content on your social channels -