Program to Reverse String
- Write a program to Reverse String in C
- Write a program to Reverse String in C++
- Write a program to Reverse String in Python
- Write a program to Reverse String in PHP
- Write a program to Reverse String in Java
- Write a program to Reverse String in Java Script
- Write a program to Reverse String in C#
Explanation:
When a string is reversed, its characters are rearranged to appear in the opposite order. To reverse a string, use this general logic:
Logic Steps:
- Initialize Variables:
- Use a variable to hold the reversed string (or reverse in-place if allowed).
- Iterate Through the String:
- Start from the end of the string and move toward the beginning.
- Rebuild the String:
- Append or swap characters to reverse their order.
- Output the Result:
- Print or return the reversed string.
Program to Reverse String
-
C
-
C++
-
Python
-
PHP
-
JAVA
-
Java Script
-
C#
#include <stdio.h> #include <string.h> int main() { char str[100], reversed[100]; int i, j, length; // Scan the string printf("Enter a string: "); fgets(str, sizeof(str), stdin); // Remove newline character if present str[strcspn(str, "\n")] = '\0'; // Find the length of the string length = strlen(str); // Reverse the string for(i = 0, j = length - 1; j >= 0; i++, j--) { reversed[i] = str[j]; } // Null-terminate the reversed string reversed[i] = '\0'; // Print the reversed string printf("Reversed string: %s\n", reversed); return 0; }
#include <iostream> #include <string> #include <algorithm> int main() { std::string str; // Scan the string std::cout << "Enter a string: "; std::getline(std::cin, str); // Reverse the string std::reverse(str.begin(), str.end()); // Print the reversed string std::cout << "Reversed string: " << str << std::endl; return 0; }
str = input("Enter a string: ") # Reverse the string reversed_str = str[::-1] # Print the reversed string print("Reversed string:", reversed_str)
<?php // Scan the string echo "Enter a string: "; $str = trim(fgets(STDIN)); // Reverse the string $reversed_str = strrev($str); // Print the reversed string echo "Reversed string: " . $reversed_str . "\n"; ?>
import java.util.Scanner; public class Main { public static void main(String[] args) { // Initialize scanner object for user input Scanner scanner = new Scanner(System.in); // Scan the string System.out.print("Enter a string: "); String str = scanner.nextLine(); // Reverse the string String reversed_str = new StringBuilder(str).reverse().toString(); // Print the reversed string System.out.println("Reversed string: " + reversed_str); } }
using System; class Program { static void Main() { // Scan the string Console.Write("Enter a string: "); string str = Console.ReadLine(); // Reverse the string char[] charArray = str.ToCharArray(); Array.Reverse(charArray); string reversed_str = new string(charArray); // Print the reversed string Console.WriteLine("Reversed string: " + reversed_str); } }
using System; class Program { static void Main() { // Scan the string Console.Write("Enter a string: "); string str = Console.ReadLine(); // Reads the entire line // Find and print the length of the string int length = str.Length; Console.WriteLine("Length of the string: " + length); } }