Geeks Talk

Prepare for your Next Interview


Welcome to the Geeks Talk forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.

Problem regarding FUNCTION

This is a discussion on Problem regarding FUNCTION within the C and C++ forums, part of the Software Development category; Help solve a problem regarding FUNCTION What the followng function is doing: string f(const string &x) { return x.empty()? x : f(x.substr(1)) + x[0]; } Guys - Please help solve ...

Go Back   Geeks Talk > Software Development > C and C++
Register Blogs FAQ Tag Cloud Calendar Mark Forums Read
  #1 (permalink)  
Old 01-14-2008
Junior Member
 
Join Date: Jan 2008
Location: Singapore
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
heidi012979 is on a distinguished road
Problem regarding FUNCTION

Help solve a problem regarding FUNCTION

What the followng function is doing:

string f(const string &x) {
return x.empty()? x : f(x.substr(1)) + x[0];
}


Guys - Please help solve this problem. Appreciate it.
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-16-2008
Contributing Member
 
Join Date: Sep 2006
Location: India
Posts: 39
Thanks: 2
Thanked 11 Times in 10 Posts
SahilKabra is on a distinguished road
Re: Problem regarding FUNCTION

Hi Heidi,
This is my analysis:

Take the string HEIDI.

The first time the function will be called with
f("HEIDI")
Quote:
return x.empty()? x : f(x.substr(1)) + x[0];
Now the x.empty() will return false. So the control will call the function again as such f("EIDI").
The above statement can be read as:
return f("EIDI") + "H"
The next time it is called the statement becomes
return f("IDI") + "E"
and so on....


Esentially, this function will reverse the string passed to it.

Assumptions: The substring method returns the substring from the index to the end of the string.

Regards,
Sahil.

--Smile, it makes people wonder what you are thinking.
Reply With Quote
Reply

  Geeks Talk > Software Development > C and C++

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 Off


Similar Threads

Thread Thread Starter Forum Replies Last Post
function goutham_konduru Oracle 1 12-11-2007 03:23 AM
What is the use of abs function Geek_Guest QTP 1 07-23-2007 07:29 AM
throwexception from function to function JobHelper VB.NET 1 06-25-2007 07:24 AM
TSL Function sutnarcha WinRunner 1 12-20-2006 05:40 AM
Function in PHP sripri PHP 1 07-20-2006 12:13 PM


All times are GMT -4. The time now is 08:39 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.3.1
Copyright © 2009 GeekInterview.com. All Rights Reserved