What is Output

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

1) What will the value of txt be after executing MyFunction?

public void Main()
{
string txt = MyFunction( “12345†);
}

public string MyFunction( string str )
{
if( str.Length == 1 )
return str;
else
return MyFunction(str.Substring(1, str.Length - 1)) + str.Substring(0,1);
}
Hint:
String.Substring( startIndex, length )
 
It looks like someone doesn't want to do their own homework... will
eventually become a professional meeting attender (if not burger flipper).

-S





Nicholas Paldino said:
seema,

Have you compiled this and seen what will be output?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

seema said:
1) What will the value of txt be after executing MyFunction?

public void Main()
{
string txt = MyFunction( "12345" );
}

public string MyFunction( string str )
{
if( str.Length == 1 )
return str;
else
return MyFunction(str.Substring(1, str.Length - 1)) + str.Substring(0,1);
}
Hint:
String.Substring( startIndex, length )
 
seema said:
1) What will the value of txt be after executing MyFunction?

public void Main()
{
string txt = MyFunction( “12345†);
}

public string MyFunction( string str )
{
if( str.Length == 1 )
return str;
else
return MyFunction(str.Substring(1, str.Length - 1)) + str.Substring(0,1);
}
Hint:
String.Substring( startIndex, length )

Not really a good hint as that gives no clue as to what will happen.
BTW, this is not the way I would code this, but it appears the goal was
to test the ability to follow a code path through a recursive function
without the aid of a debugger other than your internal debugger (the one
between your ears).
 
My Grandmother used to say that the way to learn about recursion is to study
recursion. She lived to be 100, by the way. It wasn't recursion that killed
her though. I think she had a stack overflow from some Clan McGregor scotch...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top