Quick actions

cmd+k|ctrl+k

Navigation

Languages

Reverse A String in C#

Snippet info

Language

Csharp

Visibility

public

Author

patrykstachowiak

Created

2025-03-06T19:25:02.058858Z

Updated

2025-03-06T19:47:56.929871Z

using System;
using System.Collections.Generic;
using System.Linq;

class MainClass {
    static void Main() {
        Console.WriteLine(ReverseString("Hello World!"));
    }
    
    public static string ReverseString(string randomString)
    {
        if (string.IsNullOrEmpty(randomString) || randomString.Length < 2) return string.Empty;
        // char[] characters = randomString.ToCharArray();
        char[] characters = new char[randomString.Length];
        for (int i=0; i<characters.Length/2; i++)
        {
            characters[i] = randomString[characters.Length - 1 - i];
            characters[characters.Length - 1 - i] = randomString[i];
        }
        return new string(characters);
    }
}
INFO