how to print a string backwards [Archive] - C Board

PDA

View Full Version : how to print a string backwards


Unregistered
01-25-2002, 12:05 PM
Hey guys - I am just getting started here and have no C# or C++ experience. I am doing an exercise in the back of a chapter in the book I am reading. All it is asking for is a string to be inputed by the user and then for the characters in that string to be printed out backwards. I can print each character out ok, it is just the "backwards" part I am having trouble with. I'll show you what I have but, like I said, it does not print the string out backwards as it is supposed to. How can I modify it? If anyone can help, I would appreciate it. thanks, Ryan

static void Main(string[] args)
{
Console.WriteLine("Enter a phrase.");
string myString = Console.ReadLine();
char [] myChars = myString.ToCharArray();
foreach (char character in myChars)
{
Console.WriteLine("{0}", character);
}
}

-KEN-
01-26-2002, 11:36 AM
There's a Reverse(string) function.

I think I read it hasn't been implemented in Beta2, though, so in your own coding it would be something like...

for(int i=myString.Length; i>0; i--)
Console.Write(myChars[i]);

Mars Terminal
01-27-2002, 01:04 PM
You might not have to convert the string into an array because I think that indexing is build into the string class, but unless you can't find a specific method in the BCL than go with KEN's suggestion.