A better example of Chapter 5 Question 6
I have found that using the code from the Downloaded answers can destroy any word that has the no in it such as another would be changed to ayesther. I don't think this would be acceptable but I have a simple cure:
Code on Answer Sheet:
static void Main(string[] args)
{
Console.WriteLine("Enter a string:");
string myString = Console.ReadLine();
myString = myString.Replace("no", "yes");
Console.WriteLine("Replaced \"no\" with \"yes\": {0}", myString);
Preferred code:
static void Main(string[] args)
{
Console.WriteLine("Enter a string with lots of no's");
string myString = Console.ReadLine();
myString = myString.ToLower(); // catch all variations of no
myString = myString.Replace(" no ", " yes "); // note the space before and after no and yes.
Console.WriteLine("I have switched \"no\" with \"yes\" and here is the result\n\n {0}", myString);
Console.ReadKey();
|