Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
January 15, 2022 02:57 pm GMT

Reverse of a String

Write a program to Reverse a String

package InterviewPrograms;import java.util.Scanner;public class ReverseString {    private static String reveresedString = "";//Method 01 - Using String methods    public static String revString(String str) {        int len = str.length();        for (int i = len - 1; i >= 0; i--) {            reveresedString += str.charAt(i);        }        return reveresedString;    }//Method 02 - Using StringArray    public static String revofString(String str) {        char a[] = str.toCharArray();        int len = a.length;        String revStr = "";        for (int i = len - 1; i >= 0; i--) {            revStr += a[i];        }        return revStr;    }//Method 3 : Using String buffer class    public static StringBuffer revofAString(String str) {    StringBuffer sb=new StringBuffer(str);    StringBuffer revOfString= sb.reverse();    return revOfString;    }    public static void main(String[] args) {        System.out.println("The Actual string is: ");        Scanner input = new Scanner(System.in);        String actualString = input.nextLine();        reveresedString = revString(actualString);        System.out.println("The Reversed string from Method 1 is: " + reveresedString);        System.out.println("Reversed string from Method 2: " + revofString(actualString));        System.out.println("Reversed string from Method 3: " + revofAString(actualString));    }}

Original Link: https://dev.to/sureshayyanna/reverse-of-a-string-g6h

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To