Reversing a String in C#, Java, and JavaScript: A Step-by-Step Guide

Kevin Peery
2 min readSep 26, 2023

--

In this article, we’ll explore how to reverse a string using three popular programming languages: C#, Java, and JavaScript. We’ll dive into the code, providing detailed explanations and step-by-step instructions for each language. Whether you’re a beginner looking to understand the basics of string manipulation or an experienced developer seeking language-specific solutions, this guide has got you covered.

Reversing a String in C#

using System;

class Program
{
static void Main()
{
string input = "Hello, World!";
string reversed = ReverseString(input);
Console.WriteLine(reversed);
}

static string ReverseString(string input)
{
char[] charArray = input.ToCharArray();
Array.Reverse(charArray);
return new string(charArray);
}
}

In C#, we can convert the input string to a character array, reverse it using Array.Reverse, and then create a new string from the reversed character array.

Reversing a String in Java

public class Main {
public static void main(String[] args) {
String input = "Hello, World!";
String reversed = reverseString(input);
System.out.println(reversed);
}

static String reverseString(String input) {
StringBuilder reversed = new StringBuilder();
for (int i = input.length() - 1; i >= 0; i--) {
reversed.append(input.charAt(i));
}
return reversed.toString();
}
}

In Java, we can achieve string reversal by iterating through the input string from the end to the beginning and appending characters to a StringBuilder.

Reversing a String in JavaScript

function reverseString(input) {
return input.split('').reverse().join('');
}

const input = 'Hello, World!';
const reversed = reverseString(input);
console.log(reversed);

In JavaScript, we can split the input string into an array of characters, reverse the array using reverse(), and then join the characters back together into a string.

In this exploration of string reversal in C#, Java, and JavaScript, we’ve seen how each language brings its unique approach to solving a common problem. While the goal remains the same — to reverse a string — the implementation varies.

C# leverages array manipulation with ease, Java showcases the power of StringBuilder, and JavaScript's concise array methods provide a quick solution. It's fascinating to observe how these languages, with their distinct syntax and features, achieve a common objective.

As you delve into the code examples, you’ll likely notice that the choice of language can influence the simplicity and efficiency of your solution. Whether you prefer the elegance of C#, the flexibility of Java, or the brevity of JavaScript, each language has its strengths.

So, which one is the best? The answer depends on your specific project requirements and your familiarity with the language. The beauty of programming lies in having a range of tools at your disposal, each suited to a particular task.

In your coding journey, remember that learning different programming languages not only broadens your horizons but also equips you with a diverse set of problem-solving skills. Keep experimenting, keep coding, and keep exploring the world of programming!

--

--