+ 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));
9 ответов
+ 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.
+ 4
Lisa Kinoti
Reverse method should be outside the main method.
+ 3
Where is revString method?
+ 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!
+ 1
AJ, hmmm... one moment.
+ 1
Felix Alcor, thank you! Very helpful.
+ 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);
}
+ 1
Lisa Kinoti Yes
0
AJ, Thank you for always helping me.