+ Reply to Thread
Results 1 to 5 of 5

C/C++ Programming Challenge - try this!

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 ...

  1. #1
    Good Administrator The_Big_K's Avatar
    Join Date
    26th November 2005
    I'm a crazy: Electrical engineer

    Location
    Terra-Firma
    Posts
    12,889

    Cool 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
    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

  2. #2
    Good Administrator The_Big_K's Avatar
    Join Date
    26th November 2005
    I'm a crazy: Electrical engineer

    Location
    Terra-Firma
    Posts
    12,889

    Default Re: C/C++ Programming Challenge - try this!

    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

  3. #3
    CE - Regular Member
    Join Date
    25th September 2008
    I'm a crazy: CSE engineer

    Posts
    65

    Default Re: C/C++ Programming Challenge - try this!

    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

  4. #4
    CE - Newbie
    Join Date
    13th October 2008
    I'm a crazy: Software engineer

    Location
    EGYPT
    Posts
    6

    Default Re: C/C++ Programming Challenge - try this!

    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;
    }
    

  5. #5
    CE - Regular Member
    Join Date
    25th September 2008
    I'm a crazy: CSE engineer

    Posts
    65

    Default Re: C/C++ Programming Challenge - try this!

    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;
    }
    

+ Reply to Thread

LinkBacks (?)


Similar Threads

  1. Replies: 0
    Last Post: 7th October 2008, 11:11 PM
  2. Programming exercise for newbies
    By The_Big_K in forum Computer Science & IT Engineering
    Replies: 14
    Last Post: 6th October 2008, 07:59 AM
  3. Replies: 0
    Last Post: 3rd October 2008, 12:40 AM
  4. Replies: 0
    Last Post: 2nd October 2008, 04:54 AM
  5. PIC Programming help (Urgent)
    By prwp_39 in forum Electrical & Electronics Engineering
    Replies: 0
    Last Post: 10th July 2008, 09:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts