Printing reverse of any line without using any predefined function?

How to print the return line String java is object orientated language without using any predefined function like reverse() ?

+4
source share
33 answers

You can do this either recursively or iteratively (loop).

Iteratively:

  static String reverseMe(String s) { StringBuilder sb = new StringBuilder(); for(int i = s.length() - 1; i >= 0; --i) sb.append(s.charAt(i)); return sb.toString(); } 

Recursive:

  static String reverseMe(String s) { if(s.length() == 0) return ""; return s.charAt(s.length() - 1) + reverseMe(s.substring(0,s.length()-1)); } 
+45
source

This is the simplest solution:

 System.out.print("egaugnal detatneiro tcejbo si avaj"); 
+52
source

Well, the print itself will offer a predefined function ...

Presumably, however, you could get the characters and combine them manually in the opposite direction (i.e. flip it back). Of course, you could say that concatenation is a predefined function ... so maybe the char array itself. But then again ... why?

Does the source allow "egaugnal detatneiro tcejbo si avaj" ;-p

Also, note that line-wrap is actually quite complicated if you are considering Unicode character encoding, surrogate pairs, etc. It should be noted that most of the line feed mechanisms will deal only with more common cases, but can work with i18n.

+8
source

How about a simple traverse from the end of the line to beg:

 void printRev(String str) { for(int i=str.length()-1;i>=0;i--) System.out.print(str.charAt(i)); } 
+6
source
 String reverse(String s) { int legnth = s.length(); char[] arrayCh = s.toCharArray(); for(int i=0; i< length/2; i++) { char ch = s.charAt(i); arrayCh[i] = arrayCh[legnth-1-i]; arrayCh[legnth-1-i] = ch; } return new String(arrayCh); } 
+5
source

First of all: why reinvent the wheel?

What is said: Loop from string length to 0 and concatenation to another string.

+3
source
 String a="Siva"; for(int i=0;i<=a.length()-1;i++){ System.out.print(a.charAt(i)); } for(int i = a.length() - 1; i >= 0; --i){ System.out.println(a.charAt(i)); } 
+3
source
 public class StringReverse { public static void main(String[] args) { String s= (args[0]); for (int i =s.length()-1; i >= 0; i--) { System.out.print(s.charAt(i)); } } } 

Prints an inverted input line.

+3
source

Here's a recursive solution that just prints the string in reverse order. This should be educational if you are trying to learn recursion. I also did this β€œwrong”, actually having 2 print statements; one of them should be commented. Try to figure out who is mentally, or just run experiments. In any case, learn this.

 static void printReverse(String s) { if (!s.isEmpty()) { System.out.print(s.substring(0, 1)); printReverse(s.substring(1)); System.out.print(s.substring(0, 1)); } } 

Bonus points if you answer these questions:

+1
source
 final String s = "123456789"; final char[] word = s.toCharArray(); final int l = s.length() - 2; final int ll = s.length() - 1; for (int i = 0; i < l; i++) { char x = word[i]; word[i] = word[ll - i]; word[ll - i] = x; } System.out.println(s); System.out.println(new String(word)); 

You can do this either recursively or iteratively (loop).

Iteratively:

 static String reverseMe(String s) { StringBuilder sb = new StringBuilder(); for (int i = s.length() - 1; i >= 0; --i) sb.append(s.charAt(i)); return sb.toString(); } 

Recursive:

 static String reverseMe(String s) { if (s.length() == 0) return ""; return s.charAt(s.length() - 1) + reverseMe(s.substring(1)); } 

 Integer i = new Integer(15); test(i); System.out.println(i); test(i); System.out.println(i); public static void test (Integer i) { i = (Integer)i + 10; } 
+1
source
 public class StringReverse { public static void main(String ar[]){ System.out.println(reverseMe("SRINIVAS")); } static String reverseMe(String s){ StringBuffer sb=new StringBuffer(); for(int i=s.length()-1;i>=0;--i){ sb.append(s.charAt(i)); } return sb.toString(); } } 
+1
source
 private void rev() { String st="hello"; String b=""; for(int i=st.length()-1;i>=0;i--){ b=b+st.charAt(i); } System.out.println("reverse:::"+b); } 
+1
source

Try the following:

 public class Test { public static void main(String[] args) { String s = "welcome"; for( int i=0, j = (s.length())-1; i <= j; j-- ) { char c=s.charAt(j); System.out.print(c); } } } 
+1
source

What is surprising is that most of the answers are incorrect! When Unicode is used. Nobody seems to understand that java uses UTF-16 under the hood to encode text.

Here is my simple answer.

 static String reverseMe(String s) { StringBuilder sb = new StringBuilder(); int count = s.codePointCount(0,s.length()); for(int i = count - 1; i >= 0; --i) sb.append(Character.toChars(s.codePointAt(i))); return sb.toString(); } 
+1
source

this is the best solution for this

 public class String_rev { public static void main(String[] args) { String str="Karan Rajput"; int ln=str.length(); for (int i = ln; i > 0; i--) { System.out.print(str.charAt(i-1)); } } 

}

+1
source

I had this a while ago, and responding with the obvious answer to StringBuffer.reverse (), they asked: β€œCan you change the char array without using these API methods and achieve the result without buffering into a new char array? '

At that time, when I found out that I only need to iterate over half the length of the char array, but made a little hash explaining the actual code that was supposed to go into it (it was an oral question), Anyway, I tried when I got home and came up with this:

 public class StringReverse { public static void main(String[] args){ String a = "String"; char[] aChar = a.toCharArray(); for (int i = (aChar.length-1)/2 ; i >= 0 ; i--){ int posA = i; int posB = (aChar.length-1-i); char tmpA = aChar[posA]; char tmpB = aChar[posB]; System.out.println("Setting " + posA + " to " + tmpB); System.out.println("Setting " + posB + " to " + tmpA); aChar[posA] = tmpB; aChar[posB] = tmpA; } System.out.println(aChar); } } 

Obviously, you can achieve this with less code, but I think that temporary assignments in a method make it clearer what the code does.

Outputs something like:

 Setting 2 to i Setting 3 to r Setting 1 to n Setting 4 to t Setting 0 to g Setting 5 to S gnirtS 

More interviews than homework, I would say.

0
source
 public class ReverseString { public static void main(String [] args) { String s = "reverse string" ; String b = ""; for (int i = 0; i < s.length(); i++ ){ b= b + s.substring(s.length()-1-i, s.length()-i); } System.out.println(b); } 
0
source
 import java.util.*; public class Restring { public static void main(String[] args) { String input,output; Scanner kbd=new Scanner(System.in); System.out.println("Please Enter a String"); input=kbd.nextLine(); int n=input.length(); char tmp[]=new char[n]; char nxt[]=new char[n]; tmp=input.toCharArray(); int m=0; for(int i=n-1;i>=0;i--) { nxt[m]=tmp[i]; m++; } System.out.print("Reversed String is "); for(int i=0;i<n;i++) { System.out.print(nxt[i]); } } 
0
source
 public String reverse(String arg) { String tmp = null; if (arg.length() == 1) { return arg; } else { String lastChar = arg.substring(arg.length()-1,arg.length()); String remainingString = arg.substring(0, arg.length() -1); tmp = lastChar + reverse(remainingString); return tmp; } } 
0
source
 String x = "stack overflow"; String reversed = ""; for(int i = x.length()-1 ; i>=0; i--){ reversed = reversed+ x.charAt(i); } System.out.println("reversed string is : "+ reversed); 
0
source
 public static void main(String[] args) { String str = "hello world here I am"; StringTokenizer strToken = new StringTokenizer(str); int token = strToken.countTokens(); String str1 [] = new String[token]; char chr[] = new char[str.length()]; int counter = 0; for(int j=0; j < str.length(); j++) { if(str.charAt(j) != ' ') { chr[j] = str.charAt(j); }else { str1[counter++] = new String(chr).trim(); chr = new char[str.length()]; } } str1[counter++] = new String(chr).trim(); for(int i=str1.length-1; i >= 0 ; i--) { System.out.println(str1[i]); } } 

O / P: i'm here hello world

0
source
  public class ReverseWithoutStringAPI { public static void main(String[] args) { String st="hello"; StringBuffer b=new StringBuffer(); for(int i=st.length()-1;i>=0;i--){ b.append(st.charAt(i)); } System.out.println("reverse:::"+b); } } 
0
source

Here you go:

 public static void main (String[] args) { System.out.println(reverserString("Akshay")); } private static String reverserString(String src) { char[] sArr = src.toCharArray(); char[] dArr = new char[sArr.length]; for(int i=sArr.length; i>0; i--) { dArr[sArr.length-i] = sArr[i-1]; } return new String(dArr); } 

code>

0
source
 public class MyStack { private int maxSize; private char[] stackArray; private int top; public MyStack(int s) { maxSize = s; stackArray = new char[maxSize]; top = -1; } public void push(char j) { stackArray[++top] = j; } public char pop() { return stackArray[top--]; } public char peek() { return stackArray[top]; } public boolean isEmpty() { return (top == -1); } public boolean isFull() { return (top == maxSize - 1); } public static void main(String[] args) { MyStack theStack = new MyStack(10); String s="abcd"; for(int i=0;i<s.length();i++) theStack.push(s.charAt(i)); for(int i=0;i<s.length();i++) System.out.println(theStack.pop()); } 
0
source

The code will look like this:

 public class RemoveString { public static void main(String[] args) { Scanner scanner=new Scanner(System.in); String s=scanner.next(); String st=""; for(int i=s.length()-1;i>=0;i--){ st=st+s.charAt(i); } System.out.println(st); } } 
0
source
 public class ReverseString { public static void main(String[] args) { reverseString("HELLO"); } public static String reverseString(String s){ char []arr=s.toCharArray(); for(int i= (arr.length)-1;i>=0;i--){ System.out.print(arr[i]); } String str=String.copyValueOf(arr); return str; } 
0
source

The code will look like this:

  public class A{ public static void main(String args[]){ String str="hello"; for(int i=str.length()-1;i>=0;i--){ String str1=str.charAt(i); system.out.print(str1); } } } 
0
source
 package com.ofs; public class ReverseWordsInString{ public static void main(String[] args) { String str = "welcome to the new world and how are you feeling ?"; // Get the Java runtime Runtime runtime = Runtime.getRuntime(); // Run the garbage collector runtime.gc(); // Calculate the used memory long firstUsageMemory = runtime.totalMemory() - runtime.freeMemory(); System.out.println("Used memory in bytes: " + firstUsageMemory); System.out.println(str); str = new StringBuffer(str).reverse().toString(); int count = 0; int preValue = 0; int lastspaceIndexVal = str.lastIndexOf(" "); int strLen = str.length(); for (int i = 0; i < strLen - 1; i++) { if (Character.isWhitespace(str.charAt(i))) { if (i - preValue == 1 && count == 0) { str = str.substring(0, preValue) + str.charAt(i - 1) + str.substring(i, strLen); preValue = i; count++; } else if (i - preValue == 2 && count == 0) { str = str.substring(0, preValue) + str.charAt(i - 1) + str.charAt(i - 2) + str.substring(i, strLen); preValue = i; count++; } else if (i - preValue == 3 && count == 0) { str = str.substring(0, preValue) + str.charAt(i - 1) + str.charAt(i - 2) + str.charAt(i - 3) + str.substring(i, strLen); preValue = i; count++; } else if (i - preValue == 4 && count == 0) { str = str.substring(0, preValue) + str.charAt(i - 1) + str.charAt(i - 2) + str.charAt(i - 3) + str.charAt(i - 4) + str.substring(i, strLen); preValue = i; count++; } else if (i - preValue == 5 && count == 0) { str = str.substring(0, preValue) + str.charAt(i - 1) + str.substring(i - 2, i - 1) + str.charAt(i - 3) + str.charAt(i - 3) + str.charAt(i - 5) + str.substring(i, strLen); preValue = i; count++; } else if (i - preValue == 6 && count == 0) { str = str.substring(0, preValue) + str.charAt(i - 1) + str.charAt(i - 2) + str.charAt(i - 3) + str.charAt(i - 4) + str.charAt(i - 5) + str.charAt(i - 6) + str.substring(i, strLen); preValue = i; count++; } else if (i - preValue == 7 && count == 0) { str = str.substring(0, preValue) + str.charAt(i - 1) + str.charAt(i - 2) + str.charAt(i - 3) + str.charAt(i - 4) + str.charAt(i - 5) + str.charAt(i - 6) + str.charAt(i - 7) + str.substring(i, strLen); preValue = i; count++; } else if (i - preValue == 8 && count == 0) { str = str.substring(0, preValue) + str.charAt(i - 1) + str.charAt(i - 2) + str.charAt(i - 3) + str.charAt(i - 4) + str.charAt(i - 5) + str.charAt(i - 6) + str.charAt(i - 7) + str.charAt(i - 8) + str.substring(i, strLen); preValue = i; count++; } else if (i - preValue == 2 && count != 0) { str = str.substring(0, preValue) + str.charAt(i - 1) + str.substring(i, strLen); preValue = i; } else if (i - preValue == 3 && count != 0) { str = str.substring(0, preValue + 1) + str.charAt(i - 1) + str.charAt(i - 2) + str.substring(i, strLen); preValue = i; } else if (i - preValue == 4 && count != 0) { str = str.substring(0, preValue + 1) + str.charAt(i - 1) + str.charAt(i - 2) + str.charAt(i - 3) + str.substring(i, strLen); preValue = i; } else if (i - preValue == 5 && count != 0) { str = str.substring(0, preValue + 1) + str.charAt(i - 1) + str.charAt(i - 2) + str.charAt(i - 3) + str.charAt(i - 4) + str.substring(i, strLen); preValue = i; count++; } else if (i - preValue == 6 && count != 0) { str = str.substring(0, preValue + 1) + str.charAt(i - 1) + str.charAt(i - 2) + str.charAt(i - 3) + str.charAt(i - 4) + str.charAt(i - 5) + str.substring(i, strLen); preValue = i; count++; } else if (i - preValue == 7 && count != 0) { str = str.substring(0, preValue + 1) + str.charAt(i - 1) + str.charAt(i - 2) + str.charAt(i - 3) + str.charAt(i - 4) + str.charAt(i - 5) + str.charAt(i - 6) + str.substring(i, strLen); preValue = i; count++; } else if (i - preValue == 8 && count != 0) { str = str.substring(0, preValue + 1) + str.charAt(i - 1) + str.charAt(i - 2) + str.charAt(i - 3) + str.charAt(i - 4) + str.charAt(i - 5) + str.charAt(i - 6) + str.charAt(i - 7) + str.substring(i, strLen); preValue = i; count++; } if (lastspaceIndexVal == preValue) { if (strLen - lastspaceIndexVal == 2 && count != 0) { str = str.substring(0, preValue + 1) + str.charAt(strLen - 1); preValue = i; } else if (strLen - lastspaceIndexVal == 3 && count != 0) { str = str.substring(0, preValue + 1) + str.charAt(strLen - 1) + str.charAt(strLen - 2); preValue = i; } else if (strLen - lastspaceIndexVal == 4 && count != 0) { str = str.substring(0, preValue + 1) + str.charAt(strLen - 1) + str.charAt(strLen - 2) + str.charAt(strLen - 3); preValue = i; count++; } else if (strLen - lastspaceIndexVal == 5 && count != 0) { str = str.substring(0, preValue + 1) + str.charAt(strLen - 1) + str.charAt(strLen - 2) + str.charAt(strLen - 3) + str.charAt(strLen - 4); preValue = i; } else if (strLen - lastspaceIndexVal == 6 && count != 0) { str = str.substring(0, preValue + 1) + str.charAt(strLen - 1) + str.charAt(strLen - 2) + str.charAt(strLen - 3) + str.charAt(strLen - 4) + str.charAt(strLen - 5); preValue = i; count++; } else if (strLen - lastspaceIndexVal == 7 && count != 0) { str = str.substring(0, preValue + 1) + str.charAt(strLen - 1) + str.charAt(strLen - 2) + str.charAt(strLen - 3) + str.charAt(strLen - 4) + str.charAt(strLen - 5) + str.charAt(strLen - 6); preValue = i; } else if (strLen - lastspaceIndexVal == 8 && count != 0) { str = str.substring(0, preValue + 1) + str.charAt(strLen - 1) + str.charAt(strLen - 2) + str.charAt(strLen - 3) + str.charAt(strLen - 4) + str.charAt(strLen - 5) + str.charAt(strLen - 6) + str.charAt(strLen - 7); preValue = i; } } } } runtime.gc(); // Calculate the used memory long SecondaryUsageMemory = runtime.totalMemory() - runtime.freeMemory(); System.out.println("Used memory in bytes: " + SecondaryUsageMemory); System.out.println(str); } } 
0
source

ReverseString.java

 public class ReverseString { public static void main(String[] args) { String str = "Ranga Reddy"; String revStr = reverseStr(str); System.out.println(revStr); } // Way1 - Recursive public static String reverseStr(String str) { char arrStr[] = reverseString(0, str.toCharArray()); return new String(arrStr); } private static char[] reverseString(int charIndex, char[] arr) { if (charIndex > arr.length - (charIndex+1)) { return arr; } int startIndex = charIndex; int endIndex = arr.length - (charIndex+1); char temp = arr[startIndex]; arr[startIndex] = arr[endIndex]; arr[endIndex] = temp; charIndex++; return reverseString(charIndex++, arr); } // Way2 private static String strReverse(String str) { char ch[] = new char[str.length()]; for (int i = str.length() - 1, j = 0; i >= 0; i--) { ch[j++] = str.charAt(i); } return new String(ch); } } 
0
source

Very simple to use while loop

 public class Test { public static void main(String[] args) { String name = "subha chandra"; int len = name.length(); while(len > 0){ len--; char c = name.charAt(len); System.out.print(c); // use String.valueOf(c) to convert char to String } } } 
0
source

Source: https://habr.com/ru/post/1306554/


All Articles