tag:blogger.com,1999:blog-8712770457197348465.post6809244783916050775..comments2024-03-18T23:01:42.768-07:00Comments on Javarevisited: How to Reverse String in Java Using Iteration and Recursion - Examplejavin paulhttp://www.blogger.com/profile/15028902221295732276noreply@blogger.comBlogger49125tag:blogger.com,1999:blog-8712770457197348465.post-20020111981345439952022-02-04T10:16:49.194-08:002022-02-04T10:16:49.194-08:00//Reverse String withiout Recursive, no StringBuff...//Reverse String withiout Recursive, no StringBuffer needed<br />public static String reverseNoRecursive(String str) {<br /><br /> String reverse = ""; <br /> <br /> for(int i = str.length(); i > 0; i--) { <br /> reverse = reverse + str.substring(i- 1, i); <br /> } <br /> <br /> return reverse;<br /> <br />}// end method reverseNoRecursive()tanrobles2011https://www.blogger.com/profile/14998863643490828408noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-83464787114100004522021-04-06T04:59:25.916-07:002021-04-06T04:59:25.916-07:00Hi,
In return statemnt 'reverseRecursively(str...Hi,<br />In return statemnt 'reverseRecursively(str.substring(1))+ str.charAt(0)' why charAt(0) is appended. But when I try to print the string passed as parameter I don't see any appended character at end.But if I try removing str.charAt(0) the code doesn't work.Please explain why we are using str.charAt(0) and how this recursion works.Anonymoushttps://www.blogger.com/profile/09549861075062931717noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-65256162428510345492021-01-27T20:43:43.027-08:002021-01-27T20:43:43.027-08:00Hello @Unknow, the original solution is a brute fo...Hello @Unknow, the original solution is a brute force one for simplicity one. Your solution is optimized and yes, no need to iterate whole table, as you can swap characters. Also, using array is better than StringBuffer here for the reason you mentioned that Stringbuffer will have same length. javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-30222902156967678732021-01-23T09:56:35.596-08:002021-01-23T09:56:35.596-08:00Not sure if there's been such suggestions in c...Not sure if there's been such suggestions in comments, but I don't see why iterating over whole table and not its half and why using StringBuilder while we know it's going to be a table of same size: <br /><br />public String iteratively(String given){<br /> var letters = given.toCharArray();<br /> for (int i = 0; i < letters.length/2; i++) {<br /> var tempAnonymoushttps://www.blogger.com/profile/18093998776297028506noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-44843564798046671352020-12-25T06:49:20.808-08:002020-12-25T06:49:20.808-08:00package helloworld;
import java.util.*;
import jav...package helloworld;<br />import java.util.*;<br />import java.lang.*;<br /><br />public class countingnoofchar {<br /><br /> public static void main(String[] args) <br /> {<br /> String str1="mamonfire";<br /> String str2="";<br /> int i,p=str1.length();<br /> for(i=p-1;i>=0;i--)<br /> {<br /> str2+=str1.charAt(i);<br /> }<br /> manojhttps://www.blogger.com/profile/05911623657884171147noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-47855646345482640212020-03-28T09:21:45.105-07:002020-03-28T09:21:45.105-07:00String reversedString = "";
public v... String reversedString = "";<br /> public void reverseString(String word){<br /> int stringLength = word.length();<br /><br /> String lastChar = word.substring(stringLength-1,stringLength);<br /> reversedString += lastChar;<br /> if(word.length() != 1) {<br /> String updatedString = word.substring(0, stringLength - 1);<br /> rajhttps://www.blogger.com/profile/07114661722950333755noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-32516278572023250982020-02-09T04:22:49.196-08:002020-02-09T04:22:49.196-08:00package com.pageTest;
import java.util.*;
import ...package com.pageTest;<br /><br />import java.util.*;<br />import java.util.stream.Collectors;<br /><br />public class DuplicateCharacters {<br /><br /> private String stringReverse(String string) {<br /> String reverseString = "";<br /> char chars[] = string.toUpperCase().toCharArray();<br /> for (int i = chars.length-1; i >=0; i--) {<br /><br /> srinuhttps://www.blogger.com/profile/01414524634664763786noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-37249631221929996732018-12-04T22:31:12.023-08:002018-12-04T22:31:12.023-08:00private static String reverseRecursively2(String s...private static String reverseRecursively2(String str) {<br /> if(str.length()==1){<br /> return str;<br /> }<br /> str=str.charAt(str.length()-1)+reverseRecursively2(str.substring(0,str.length()-1));<br /> return str;<br /> }Anonymoushttps://www.blogger.com/profile/10635256998675432365noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-16708375731619548862018-07-03T06:45:49.046-07:002018-07-03T06:45:49.046-07:00I have made some changes to recursive method by us...I have made some changes to recursive method by using StringBuilder. In this way we can reduce the number of string objects being created to some extend. <br /><br />private static StringBuilder reverseRecursive(String str) {<br /> if(str.length() < 2) return new StringBuilder(str);<br /> <br /> return reverseRecursive(str.substring(1)).append(str.charAt(0));<br /> }Anonymoushttps://www.blogger.com/profile/08607537334825490690noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-56040257319815255112018-06-28T23:43:09.348-07:002018-06-28T23:43:09.348-07:00 public static string ReverseString(string str)
... public static string ReverseString(string str)<br /> {<br /> if (string.IsNullOrEmpty(str))<br /> return null;<br /> return str[str.Length - 1] + ReverseString(str.Substring(0, str.Length - 1));<br /> }EthZenahttps://www.blogger.com/profile/15981405554043877042noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-27232461115152623382018-06-28T23:39:29.442-07:002018-06-28T23:39:29.442-07:00public static string ReverseString(string str )
... public static string ReverseString(string str )<br /> {<br /> if (str.Length == 0)<br /> return "";<br /> string reversed = str[str.Length - 1] + ReverseString(str.Substring(0, str.Length - 1));<br /> return reversed;<br /> }EthZenahttps://www.blogger.com/profile/15981405554043877042noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-43869654967014547872018-06-28T02:24:00.602-07:002018-06-28T02:24:00.602-07:00 public static String reverse(String input) {
... public static String reverse(String input) {<br /> if (input.length() == 1) {<br /> return input;<br /> } else {<br /> String first = String.valueOf(input.charAt(0));<br /> return reverse(input.substring(1)) + first;<br /> }<br /> }Najstariji zanatlijahttps://www.blogger.com/profile/15419227034409122786noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-55225289615434598012017-11-21T21:15:49.555-08:002017-11-21T21:15:49.555-08:00Assume you have a string “this is test string”.
Yo...Assume you have a string “this is test string”.<br />You need to write a code to reverse the string. The output should be “string test is this”.<br />There is a reverse method by default available in Java, do not use that.can any slove this<br />Anonymoushttps://www.blogger.com/profile/05680577889245268434noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-45519478305572811962017-04-15T16:53:48.093-07:002017-04-15T16:53:48.093-07:00public class Reverse {
public static void main (...public class Reverse {<br /><br /> public static void main (String[] args) {<br /> System.out.println(reverse("Madam is adam"));<br /> }<br /> public static char[] reverse (String str) {<br /> char[] character = str.toCharArray();<br /> char temp;<br /> for(int i=0; i<character.length/2; i++) {<br /> temp = character[i]; <br /> character[i] = character[character.length -i -1]Samyakhttps://www.blogger.com/profile/07186090024269535615noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-4162778691741221622017-02-20T12:56:49.661-08:002017-02-20T12:56:49.661-08:00void reverseString(string str){
if(str.size() ...void reverseString(string str){<br /> if(str.size() >0)<br /> reverseString(str.substr(1,str.size()));<br /> cout<<str[0];<br />}Samhttps://www.blogger.com/profile/15791037253110309249noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-22650525264938558482016-10-26T10:03:36.615-07:002016-10-26T10:03:36.615-07:00I don't understand how recursion works. Can yo...I don't understand how recursion works. Can you please give more logic details?<br />Evyhttps://www.blogger.com/profile/09230351920160887350noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-67005077094855766242016-07-17T19:36:37.414-07:002016-07-17T19:36:37.414-07:00can someone explain where the + str.charAt(0); in ...can someone explain where the + str.charAt(0); in reverseRecursively(str.substring(1))+ str.charAt(0);<br /> returns to?<br />I also don't understand where it stores or how it stores the string till it returns it fully reversed to reverseStr up top.Anonymoushttps://www.blogger.com/profile/06921783156626310203noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-70198327838198740402016-04-16T12:05:48.085-07:002016-04-16T12:05:48.085-07:00Why cannot we do use charAt and append from (strin...Why cannot we do use charAt and append from (string.length -1) to 0 and create a new string?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-66784763629343896942016-03-20T15:37:24.240-07:002016-03-20T15:37:24.240-07:00// tail recursive approach
recursiveReverse("...// tail recursive approach<br />recursiveReverse("tail recursive approach","");<br />public static String recursiveReverse(String text, String newStr) {<br /> if (text.length() < 1) {<br /> return newStr;<br /> }<br /> return recursiveReverse(text.substring(1), text.charAt(0) + newStr);<br />}Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-11980052172157784642015-09-02T10:40:16.928-07:002015-09-02T10:40:16.928-07:00package Reverse;
import java.util.ArrayList;
impor...package Reverse;<br />import java.util.ArrayList;<br />import java.util.List;<br />import java.util.Scanner;<br /><br /> public class Reverse {<br /> public static void main(String args[]){<br /> <br /> ActionReverse ar = new ActionReverse();<br /> System.out.println("Enter a String");<br /> @SuppressWarnings("resource")<br /> Scanner scanner = new Scanner(System.in);<br Anonymoushttps://www.blogger.com/profile/03988397280504216461noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-16993518392578842732015-08-23T02:15:33.833-07:002015-08-23T02:15:33.833-07:00Another one to check String Palindrome...
package...Another one to check String Palindrome...<br /><br />package com.practice.java;<br /><br />import java.util.Scanner;<br /><br />public class PalindromeStringCheckSimple {<br /><br /> public static void main(String[] args) {<br /> System.out.println("Enter any String: ");<br /> Scanner sc = new Scanner(System.in);<br /> String text = sc.nextLine();<br /> char[] text1 = Sanjuktanoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-65750056099463819642015-08-15T08:50:22.771-07:002015-08-15T08:50:22.771-07:00@praashant, that's the base case if String has...@praashant, that's the base case if String has less than two character e.g. empty or just one character than reverse of the String is the String itself e.g. reverse of "a" is "a" itself, or reverse of empty String is "" itself, that's why we are return the same str there. Remember, every recursive algorithm needs a base case to solve the problem without it, javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-70382375468737738182015-08-07T07:08:59.508-07:002015-08-07T07:08:59.508-07:00please sir can u breifly explain this function plz...please sir can u breifly explain this function plz <br /><br />public static String reverseRecursively(String str) {<br /><br /> if (str.length() < 2) {<br /> return str;<br /> }<br /><br /> return reverseRecursively(str.substring(1)) + str.charAt(0);<br /><br /> }<br />}<br />praashantnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-77942728495764541302015-06-26T11:21:31.780-07:002015-06-26T11:21:31.780-07:00Mine way :
public static char[] revStr(char[] ar...Mine way : <br /><br />public static char[] revStr(char[] arr, int start, int end)<br /> {<br /> if(start == (arr.length/2))<br /> {<br /> return arr;<br /> }<br /> <br /> char temp = arr[start];<br /> arr[start] = arr[end];<br /> arr[end] = temp;<br /> <br /> return revStr(arr, ++start, --end);<br /> }Tarun Kumarhttps://www.blogger.com/profile/15606517331932842013noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-36986639753806738462015-05-08T04:49:24.200-07:002015-05-08T04:49:24.200-07:00import java.io.*;
class split
{
public static voi...import java.io.*;<br />class split<br />{<br /> public static void main(String args[])throws IOException<br /> {<br /> String str[];<br /> String finalstr="";<br /> BufferedReader br=new BufferedReader(new InputStreamReader(System.in));<br /> str=br.readLine().split(" ");<br /> for(int i=str.length-1;i>=0;i--)<br /> finalstr+=str[i]+" ";<br /> CROSSWINhttps://www.blogger.com/profile/02637630186505018297noreply@blogger.com