java Write a program to reverse an array or string
// Iterative java program to reverse an// arraypublicclassGFG{/* Function to reverse arr[] from
start to end*/staticvoidrvereseArray(int arr[],int start,int end){int temp;while(start < end){
temp = arr[start];
arr[start]= arr[end];
arr[end]= temp;
start++;
end--;}}/* Utility that prints out an
array on a line */staticvoidprintArray(int arr[],int size){for(int i =0; i < size; i++)System.out.print(arr[i]+" ");System.out.println();}// Driver codepublicstaticvoidmain(String args[]){int arr[]={1,2,3,4,5,6};printArray(arr,6);rvereseArray(arr,0,5);System.out.print("Reversed array is
");printArray(arr,6);}}
// Not the best way i know but i wanted to challenge myself to do it this way so :)publicstaticStringreverse(String str){char[] oldCharArray = str.toCharArray();char[] newCharArray=newchar[oldCharArray.length];int currentChar = oldCharArray.length-1;for(char c : oldCharArray){
newCharArray[currentChar]= c;
currentChar--;}returnnewString(newCharArray);}
publicclassReverseStringByFavTutor{publicstaticvoidmain(String[] args){String stringExample ="FavTutor";System.out.println("Original string: "+stringExample);// Declaring a StringBuilder and converting string to StringBuilderStringBuilder reverseString =newStringBuilder(stringExample);
reverseString.reverse();// Reversing the StringBuilder// Converting StringBuilder to StringString result = reverseString.toString();System.out.println("Reversed string: "+result);// Printing the reversed String}}
1)String str ="Hello";String result ="";for(int i = str.length()-1; i>=0; i--){
result += str.charAt(i);// first solution, charAt method// result += str1.substring(i, i+1); // first solution, substring method}System.out.println(result);}
// java program to reverse a wordimportjava.io.*;importjava.util.Scanner;classGFG{publicstaticvoid main (String[] args){String str="Geeks", nstr="";char ch;System.out.print("Original word: ");System.out.println("Geeks");//Example wordfor(int i=0; i<str.length(); i++){
ch= str.charAt(i);//extracts each character
nstr= ch+nstr;//adds each character in front of the existing string}System.out.println("Reversed word: "+ nstr);}}//Contributed by Tiyasa
// Java Program to reverse a String// without using inbuilt String functionimportjava.util.regex.Pattern;publicclassExp{// Method to reverse words of a StringstaticStringreverseWords(String str){// Specifying the pattern to be searchedPattern pattern =Pattern.compile("s");// splitting String str with a pattern// (i.e )splitting the string whenever their// is whitespace and store in temp array.String[] temp = pattern.split(str);String result ="";// Iterate over the temp array and store// the string in reverse order.for(int i =0; i < temp.length; i++){if(i == temp.length -1)
result = temp[i]+ result;else
result =" "+ temp[i]+ result;}return result;}// Driver methods to test above methodpublicstaticvoidmain(String[] args){String s1 ="Welcome to geeksforgeeks";System.out.println(reverseWords(s1));String s2 ="I love Java Programming";System.out.println(reverseWords(s2));}}