CrazyEngineers Forum
Navigation
Go Back   CrazyEngineers Forum > CE : Technical Discussions > Computer Science & IT Engineering


Advertisements
Reply
 
LinkBack (1) Thread Tools Display Modes

  1 links from elsewhere to this Post. Click to view. #1 (permalink)
Old 10th October 2008, 07:27 PM
Good Administrator
 
The_Big_K's Avatar
 
Join Date: 26th November 2005
I'm a Crazy Electrical Engineer
Location: Terra-Firma
Posts: 8,551
Send a message via Yahoo to The_Big_K
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
The_Big_K is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote


  #2 (permalink)
Old 13th October 2008, 03:04 PM
Good Administrator
 
The_Big_K's Avatar
 
Join Date: 26th November 2005
I'm a Crazy Electrical Engineer
Location: Terra-Firma
Posts: 8,551
Send a message via Yahoo to The_Big_K
Default Re: C/C++ Programming Challenge - try this!

Hello!

Too tough? eh?
The_Big_K is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)
Old 13th October 2008, 03:06 PM
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
sriramchandrk is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)
Old 13th October 2008, 03:42 PM
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;
}
emYth is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)
Old 13th October 2008, 04:06 PM
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;
}
sriramchandrk is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


LinkBacks (?)
LinkBack to this Thread: http://www.crazyengineers.com/forum/computer-science-engineering/6206-c-c-programming-challenge-try.html
Posted By For Type Date
C/C++ Programming Challenge - try this! - CrazyEngineers Forum This thread Refback 10th October 2008 07:50 PM

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help Engadget Energize Education in the 2008 DonorsChoose Blogger Challenge CE-InFocus CE - InFocus 0 7th October 2008 11:11 PM
Programming exercise for newbies The_Big_K Computer Science & IT Engineering 14 6th October 2008 07:59 AM
Help Engadget Energize Education in the 2008 DonorsChoose Blogger Challenge CE-InFocus CE - InFocus 0 3rd October 2008 12:40 AM
Help Engadget Energize Education in the 2008 DonorsChoose Blogger Challenge CE-InFocus CE - InFocus 0 2nd October 2008 04:54 AM
PIC Programming help (Urgent) prwp_39 Electrical & Electronics Engineering 0 10th July 2008 09:42 PM


All times are GMT +6.5. The time now is 01:59 PM.
Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.3.0
Member comments are owned by the poster. Copyright © 2005-2009 CrazyEngineers.com. All rights reserved.

Advertisements