-2

I have made the code so that it takes in an input, a string, and then;

  1. Outputs the string, no changes made
  2. Shows the string but from the array, so that it would be listed
  3. It would then output the list after it has been reversed.

So far with my code i have done this:

        string input;
        int arLENGTH;
        Console.WriteLine("Enter the string you want to be reversed.");
        input = Console.ReadLine(); 
        
        char[] chars = input.ToCharArray();
        Console.WriteLine("Original string: " + input);
        Console.WriteLine("Character array(Not Reversed):");
        for (int a = 0; a < chars.Length; a++)
        {
            Console.WriteLine(chars[a]);
        }
        arLENGTH = chars.Length;
        Console.WriteLine("Reversed String: ");
        while (arLENGTH > chars.Length)
        {
            Console.WriteLine(chars[arLENGTH]);
            arLENGTH--;
        }
        Console.ReadLine();

When i run it, it outputs Reversed String, and then just leaves it blank.

3
  • 1
    take a look at this post: stackoverflow.com/questions/20771798/… Commented Oct 1, 2020 at 19:22
  • @NolanBradshaw Ty dude, i was looking for something like this but i couldn't find it. I usually use W3Schools, and im pretty sure it wasn't there. Commented Oct 1, 2020 at 19:27
  • @NolanBradshaw The real answer is not the accepted one for that SO question. And it's a little dated. Commented Oct 1, 2020 at 19:36

4 Answers 4

1

This become pretty trivial with C# index operators:

    static void Main(string[] _)
    {
        string input = "my input string";

        Console.WriteLine(input);

        for (int i = 1; i <= input.Length; ++i)
            Console.Write(input[^i]);

        Console.WriteLine();
    }
Sign up to request clarification or add additional context in comments.

4 Comments

I like it...but the syntax still looks weird to my brain! =)
@Idle_Mind it is different. I think your answer, which is also the one linked to by Nolan Bradshaw in the comment above, is clearer. Having the "a--" makes it obvious you're going in reverse. But the new stuff is always fun to play around with.
@OrkhanAlikhanov, from Building C# 8.0: "We’re adding a type Index, which can be used for indexing. You can create one from an int that counts from the beginning, or with a prefix ^ operator that counts from the end". Or see the System.Index section.
@Idle_Mind Thank you for a detailed explanation! I've used C#6 long ago, it seems cool updates has happened since then
0

if you put arLENGTH > chars.Length

that's mean you put Length > Length if Length is 5 then you said 5 > 5 you should put while arLENGTH > 0

hope you understand.

--- EDITED

I just notice something else you should put [length -1]

            string input;
            int arLENGTH;
            Console.WriteLine("Enter the string you want to be reversed.");
            input = Console.ReadLine();

            char[] chars = input.ToCharArray();
            Console.WriteLine("Original string: " + input);
            Console.WriteLine("Character array(Not Reversed):");
            for (int a = 0; a < chars.Length; a++)
            {
                Console.WriteLine(chars[a]);
            }
            arLENGTH = chars.Length;
            Console.WriteLine("Reversed String: ");
            while (arLENGTH > 0)
            {
                Console.WriteLine(chars[arLENGTH - 1]);
                arLENGTH--;
            }
            Console.ReadLine();

2 Comments

I know you're mirroring his original, but you don't need the char arrays at all.
I'm Just correcting him, that's the process of learning, if he wants indexes or something that's another question and learning. he asked what is wrong and I point what's wrong after a couple minutes I edited just by looking. just I repeat different is another question and not please solve my homework. (sorry my english)
0

The issue is with your while loop. You are assigning arLENGTH = chars.Length; and then checking in while arLENGTH > chars.Length, which will never be true, because both contain the same value.

Update your while loop so that

while (arLENGTH > 0)
{
   Console.WriteLine(chars[arLENGTH-1]);
   arLENGTH--;
}

Comments

0

Why not use a for loop to print the reverse, just like you did for forwards?

You just need start at Length - 1, continue as long as you are greater than or equal to zero, and decrement the value.

Looks like this:

Console.WriteLine("Reversed String:");
for (int a = (chars.Length - 1); a >= 0; a--)
{
    Console.WriteLine(chars[a]);
}

2 Comments

I tried this but i did it a different way so it didnt work. Good to know that i was on the right track though.
@ExiusCain or using the index operators.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.