Question

For all of the following words, if you move the first letter to the end of the word, and then spell the result backwards, you will get the original word: banana dresser grammar potato revive uneven assess

I got the first part down, moving the first letter to the end, but I am not able to spell the word in reverse. I have to use a for loop for this, but I have no idea how to use it so it will spell the rest of the word backwards.

Was it helpful?

Solution

Normally the for-loop does not care in which way you modify the index, so you should be able to use something like

string firstAtLast = "otatop";
string reverse = "";

for(int i=string.length-1; i => 0; i--)
{
    reverse += firstAtLast.At(i)
}

Details for i and the string manipulation methods depend on your language of course.

OTHER TIPS

This should do the trick (C#)

public void ReverseWord()
  {
    var word = "banana dresser grammar potato revive uneven assess";
    var length = word.Length;
    string reverse = "";
    for (int i = 0; i < length; i++)
    {
        reverse = word.Substring(0, 1) + reverse;
        word = word.Remove(0, 1);
    }
}

The result I'm getting is : ssessa nevenu eviver otatop rammarg resserd ananab

Why would you move the first letter to the end?

string word = "hello";
string neword = string.Empty;
Array a = word.ToCharArray();
foreach (char c in a)
{
    neword = neword + c;
}

I wrote that in C#, but its the same for mainly anything.

Actually, you might be able to do it this way if the language you are writing in is a bit smart:

string word = "hello";
string neword = string.Empty;
foreach (char c in word)
{
    neword = neword + c;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top