how to print a string backwards

This is a discussion on how to print a string backwards within the C# Programming forums, part of the General Programming Boards category; Hey guys - I am just getting started here and have no C# or C++ experience. I am doing an ...

  1. #1
    Unregistered
    Guest

    how to print a string backwards

    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);
    }
    }

  2. #2
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,230
    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]);

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    12
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 03:45 PM
  2. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  3. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  5. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 12:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21