Console.WriteLine("Please enter in a string that you want to change the word 'yes' into no.");
string usrString = Console.ReadLine().ToLower();
string[] stringToArray = usrString.Split(' ');
string newString = "";
for (int i = 0; i <= stringToArray.Length - 1; i++)
{
if (stringToArray[i] == "yes")
{
stringToArray[i] = "no";
}
stringToArray[i] = stringToArray[i].PadRight(1);
newString = stringToArray[i] +newString;
}
Console.WriteLine("You new string is: \n{0}", newString);
Console.ReadKey();
I was practicing this exercise in the book I read, and it wants me to write a console application that accepts a string and replaces all occurrences of the string yes with no.
Well with what I used, I got it to work. But my problem is that it won't add a space to each element before I add it to the string. I thought .PadRight(1) would add a space to the end of the current element, assign it to that element and then it would add that element to my string with the space. I know I could just use .replace(), but I wanted to try without using .replace().


Sign In
Create Account


Back to top









