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: #-Link-Snipped-#

Replies

  • Kaustubh Katdare
    Kaustubh Katdare
    Hello!

    Too tough? eh?
  • sriramchandrk
    sriramchandrk
    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
    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
    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;
    }
     
    
  • Manish Goyal
    Manish Goyal
    @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
  • Morningdot Hablu
    Morningdot Hablu
    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!!
    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
    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
    @BigK: Thank you.

You are reading an archived discussion.

Related Posts

😎 i need latest seminar topics plz suggest me...........😕
Which encoder is there in Servomotor and wt its function? Can anyone explain in detail? Or any material wud be fine Reply me
Hey every body , me new to this CE... well this for every crazy engineer that We RCEW an engineering college in jaipur coming with a tech fest in month...
collector contact being at the top increases the collector current path thereby increasing the collector series resistance and hence Vce(sat) of the device. how can be?what does it mean,i am...
I figured out before how to control the speed of a motor through a microcontroller by varying the duty cycle of a pulse width modulated signal. But now I want...