tags:

views:

59

answers:

2

Hi,

I need to solve the following question which i can't get to work by myself(newbie^^)..:

Ok, the question: Create a method which will print the central letter of a string (given as a parameter). I need to use the property lenght to determine the lenght.

So for example the string: Books. the middle/central letter is o. Hope its a bit clear..

Thanks in advance.

Edit: I know how to determine the lenght of the string. Now the problem is to divide the word and then write down the next letter or something.

A: 
string middleLetter(string arg)
{
   return arg[arg.Length >> 1];
}
alxx
1. This is a homework question - just putting code here does not help. 2. This is not C#. 3. What happens when the word has an even number of characters? Should be specified more explicitly. 4. Why the shifting? To confuse a newbie?
tanascius
-1 for complexity
Oren A
+1 for being clever, -1 for being inscrutable and not C#
Kirk Woll
@Kirk: why is this clever? It is useless micro optimization which results in more unreadable code ...
tanascius
Sorry, it was not C# indeed. Why shifting? Because I hate to divide by 2 when division helps ^_^ What happens when length is even? >> 1 divides by 2 rounded down.
alxx
@tanascius, you understand that something can be simultaneously clever and unreadable, right? I'd even go so far as to suggest they often go hand in hand. :)
Kirk Woll
@Kirk, I do ... but a '/2' will probably end up like a '>>1' - as the same IL code ... in this case, there is no cleverness ... and *Jay* even stated that he is a newbie ...
tanascius
+2  A: 

Here are some tips:
1. Type string has a Length property.
2 .If you know the index of the character you want, you can ask for it using: myString[index].
3. Knowing what to do with string that has even number of characters is necessary to answer that question.
4. Consider integer devision.

That should get you started.

Oren A