This week’s programming challenge is about Strings! String is a great C# class with a lot of built-in functionality for manipulating characters and text.
Write a program that reads a string from standard console input and then prints it in reverse order on the screen.
To spice this up, allow the program to take in a number of strings and reverse the whole thing before printing it. The console should know that the program is done when it encounters the word END.
Example input:
one
two
three
END
Example output:
three
two
one
See if you can write this program before I post the solution on Friday.
[codesyntax lang=”csharp” title=”Solution” blockstate=”collapsed”]
class Program { static void Main(string[] args) { //create a List that allows you to hold the input strings List<String> wordList = new List<string>(); //get the word input from the user until they tell us there are no more words string input = ""; while(true) { Console.WriteLine("Please enter the word you wish to store or END to quit"); //get the string from the user input = Console.ReadLine(); //test the input to make sure its not the word END if(input == "END") { break; } else { //store the input in the list wordList.Add(input); } } //once the user has entered all of the words they want, we will reverse the list and print it wordList.Reverse(); foreach(string word in wordList) { Console.WriteLine(word); } } }
[/codesyntax]