0

I have the following code.

Console.Clear();
string encodedMessage = "";

Console.WriteLine("Enter a word to encode");
char[] stringtoencode = Console.ReadLine().ToCharArray();

for (int i = 1; i < stringtoencode.Length; i++)
{
    string currentCharAsString = stringtoencode[i].ToString();
    encodedMessage += currentCharAsString;
}

encodedMessage = encodedMessage + stringtoencode[0].ToString() + "ay";
Console.WriteLine("Your string encodes to backslang as          " +encodedMessage);

It takes a string input from the user and encodes it into a form of backslang (which is a phonetic encryption, which simply moves the first letter of the word to the end of the word and adds 'ay' to the end of the word)

I am using Console.ReadLine() to retrieve the input string. How can I modify the above code so that it only allows the user to enter a single word, following the prompt 'enter a word to encode'?

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
GaryDev
  • 15
  • 8

1 Answers1

2

This will ask the user to input a (new) word if the line read contains a space.

string word;
do
{
    Console.WriteLine("Enter a word to encode");
    word = Console.ReadLine();
} while (word.Contains(' '));
var encodedMessage = word.Substring(1) + word[0]  + "ay";
Console.WriteLine("Your string encodes to backslang as " + encodedMessage);
Magnus
  • 45,362
  • 8
  • 80
  • 118