• Register
0 votes
192 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 5 3
6,930 points

Please log in or register to answer this question.

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

9 7 4
38,600 points

Related questions

1 vote
1 answer 1.1K views
1.1K 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, 2020 LizzyM 6.1k points
0 votes
1 answer 1 view
1 view
Question: I know that in Java I can extract a substring from a String object doing something like: String string= "Hello World"; String subString = string.substring(5); And in this wat the subString variable will contain only the Hello string and I know that I can also specify ... two initial characters. So for example I have: String value = "12345" and my substring have to be 345 How can I do it?
asked 3 days ago Atik03 10.1k points
0 votes
2 answers 6 views
6 views
Problem: Do you have any suggestions about how I can resolve this? Find maximum of minimum for every window size in a given array
asked Mar 29 tuhin1 48.9k points
0 votes
2 answers 890 views
890 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 65 views
65 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
1 answer 2 views
2 views
Problem: Does strcmp() work through each index of two arrays of characters and if the ASCII value is smaller at an identical index of two strings, that string is there alphabetically greater and therefore a 0 or 1 or 2 is returned?
asked 4 days ago ummesalma 25.2k points
0 votes
1 answer 5 views
5 views
Problem: I need someone&rsquo;s help to fix it... java for each char in string
asked Mar 25 Ifra 24.4k points
0 votes
1 answer 15 views
15 views
Problem: Hello Developers! I am new to the world of programming and I started learning Java at an academy. My teacher gave us an assingment in which we have to find the index of last occurrence of a character in a string. The program should return the ... those solutions. If someone has better solution then kindly explain or else please explain what substrings are and how I can use it. Thanks
asked Nov 29, 2020 Code Learner 9.2k points
0 votes
1 answer 2 views
2 views
Problem: I am stuck with this line for a long time.Someone please help me.how to delete a char from a string java
asked 5 days ago ashik 12.9k points
0 votes
1 answer 2 views
2 views
Problem: I need help for solving problem in java:delete a char from a string java
asked 5 days ago ashik 12.9k points