+ 1

Reverse a word in c#

Can someone tell me what is wrong with the code below? I keep getting CS8600 and CS0103 errors. I am trying to reverse the input word. Console.WriteLine("Enter any word and hit enter to see the word reversed: "); string str = Console.ReadLine(); Console.WriteLine("\nYour word in reverse is: " + revString(str));

17th Aug 2022, 6:30 AM
Lisa Kinoti
Lisa Kinoti - avatar
9 Answers
+ 3
I just give you a new methode while you search for the Methode. Another way would be to convert the string to a char array and use Array.Reverse() Methode.
17th Aug 2022, 7:24 AM
Felix Alcor
Felix Alcor - avatar
+ 4
Lisa Kinoti Reverse method should be outside the main method.
17th Aug 2022, 10:32 AM
A͢J
A͢J - avatar
+ 3
Where is revString method?
17th Aug 2022, 6:41 AM
A͢J
A͢J - avatar
+ 2
1 Hour Later... AJ, I did it!!! Took me a while but your question about the method and Felix Alcor idea combined helped me to convert my string to an array within a reverse method. I was so confused but I kept trying till I did it! I saved it in my code bits but here is the code I used: static void Main(string[] args) { Console.WriteLine("Enter any word and hit enter to see the word reversed: "); string str = Console.ReadLine(); static string Reverse(string str) { char[] charArray = str.ToCharArray(); Array.Reverse(charArray); return new string(charArray); } string reversed = Reverse(str); Console.WriteLine("\nYour word in reverse is: " + reversed); } Thank you so much guys!
17th Aug 2022, 8:41 AM
Lisa Kinoti
Lisa Kinoti - avatar
+ 1
AJ, hmmm... one moment.
17th Aug 2022, 6:55 AM
Lisa Kinoti
Lisa Kinoti - avatar
+ 1
Felix Alcor, thank you! Very helpful.
17th Aug 2022, 8:37 AM
Lisa Kinoti
Lisa Kinoti - avatar
+ 1
AJ, do you mean like this? static string Reverse(string str) { char[] charArray = str.ToCharArray(); Array.Reverse(charArray); return new string(charArray); } static void Main(string[] args) { Console.WriteLine("Enter any word and hit enter to see the word reversed: "); string str = Console.ReadLine(); string reversed = Reverse(str); Console.WriteLine("\nYour word in reverse is: " + reversed); }
17th Aug 2022, 2:16 PM
Lisa Kinoti
Lisa Kinoti - avatar
17th Aug 2022, 2:55 PM
A͢J
A͢J - avatar
0
AJ, Thank you for always helping me.
17th Aug 2022, 6:23 PM
Lisa Kinoti
Lisa Kinoti - avatar