質問

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.

役に立ちましたか?

解決

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.

他のヒント

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;
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top