• Register
0 votes
78 views

Problem :

I am trying to print every string in Reverse Double Character way (e.g. string -> ggnniirrttss)

import java.util.Scanner;
public class ReverseDoubleCharecters {
public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("Enter a string to be reversed");
    String str = input.nextLine();
    String new_str = "";
    String result = "";
    for (int j = 0; j < str.length(); i++) {
        result += str.substring(i, j + 1) + str.substring(j, j + 1);
        String result2 = null;
        result2 = result;
        System.out.println(result2);
        }
    }
}

Still when I execute this code all I get as follows:

ss
tt
rr
ii
nn
gg

All double characters are printed on new lines. Anybody has the solution on this?

6.9k points

1 Answer

0 votes

Solution :

Please have a look at the comments on the lines which I have updated:

1.      Please Iterate the string in backward way

2.      Do not try to print each double char in each loop iteration since you are need to print only the final result

3.      So Print the final result only after the loop

import java.util.Scanner;

public class ReverseDoubleCharecters {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.println("Enter a string to be reversed ");

String str = input.nextLine();

String new_str = "";

String result = "";

for (int j = str.length() - 1; j >= 0;j--) { // Please Iterate the string in backward way

 result += str.substring(j, j + 1) + str.substring(j j + 1); //Concatenate

String result2 = null;

 result2 = result;

// System.out.println(result2); // Do not try to print each double char in each loop iteration since you are need to print only the final result

}

System.out.println(result); // So Print the final result only after the loop

}

}

Output:

Enter a string to be reversed  string

ggnniirrttss

36.1k points

Related questions

1 vote
1 answer 197 views
197 views
Problem: I am new in python programming and I have been doing a course on it as a new comer I tried to repeat some char in a string. I wrote a code : def Work(str): for i in range(len(str)): return i * 2 But it does not work . how can I do this?
asked Apr 3 LizzyM 5.7k points
0 votes
2 answers 234 views
234 views
Problem : I am facing following issue need solution return true if the string "cat" and "dog" appear the same number of times in the given string
asked Nov 13, 2019 peterlaw 6.9k points
0 votes
1 answer 25 views
25 views
Problem : I am very new to Java Programming and currently I am stuck with the following doubt related to Strings. How to determine if two strings are permutations of each other? Any solution on my above doubt will be appreciated.
asked Dec 25, 2019 alecxe 7.5k points
0 votes
2 answers 97 views
97 views
Problem : How to count the number of occurrences of a character in a string in java
asked Oct 23, 2019 peterlaw 6.9k points
1 vote
1 answer 87 views
87 views
Problem : I am trying to find solution for following error replace any space ' ' by '_' in 2-character string passcode. sample output for the given program:
asked Nov 11, 2019 peterlaw 6.9k points