C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Program to find reverse of a stringExplanationIn this program, we need to find the reverse of the string. This can be done by iterating the string backward and storing each character from the original string into a new string. Original string: Dream big Reverse of the string: big maerD Algorithm
SolutionPythonstring = "Dream big"; #Stores the reverse of given string reversedStr = ""; #Iterate through the string from last and add each character to variable reversedStr for i in range(len(string)-1, -1, -1): reversedStr = reversedStr + string[i]; print("Original string: " + string); #Displays the reverse of given string print("Reverse of given string: " + reversedStr); Output: Original string: Dream big Reverse of given string: gib maerD C#include <stdio.h> #include <string.h> int main() { char string[] = "Dream big"; //Stores the reverse of given string char reversedStr[strlen(string)]; int j = 0; //Iterate through the string from last and add each character to variable reversedStr for(int i = strlen(string)-1; i >= 0; i--){ reversedStr[j++] = string[i]; } reversedStr[j] = '\0'; printf("Original string: %s", string); //Displays the reverse of given string printf("\nReverse of given string: %s", reversedStr); return 0; } Output: Original string: Dream big Reverse of given string: gib maerD JAVApublic class Reverse { public static void main(String[] args) { String string = "Dream big"; //Stores the reverse of given string String reversedStr = ""; //Iterate through the string from last and add each character to variable reversedStr for(int i = string.length()-1; i >= 0; i--){ reversedStr = reversedStr + string.charAt(i); } System.out.println("Original string: " + string); //Displays the reverse of given string System.out.println("Reverse of given string: " + reversedStr); } } Output: Original string: Dream big Reverse of given string: gib maerD C#using System; public class Reverse { public static void Main() { String string1 = "Dream big"; //Stores the reverse of given string String reversedStr = ""; //Iterate through the string from last and add each character to variable reversedStr for(int i = string1.Length-1; i >= 0; i--){ reversedStr = reversedStr + string1[i]; } Console.WriteLine("Original string: " + string1); //Displays the reverse of given string Console.WriteLine("Reverse of given string: " + reversedStr); } } Output: Original string: Dream big Reverse of given string: gib maerD PHP<!DOCTYPE html> <html> <body> <?php $string = "Dream big"; //Stores the reverse of given string $reversedStr = ""; //Iterate through the string from last and add each character to variable reversedStr for($i = strlen($string)-1; $i >= 0; $i--){ $reversedStr = $reversedStr . $string[$i]; } print("Original string: " . $string); //Displays the reverse of given string print("<br>Reverse of given string: " . $reversedStr); ?> </body> </html> Output: Original string: Dream big Reverse of given string: gib maerD
Next Topic#
|