• Register
0 votes
145 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 709 views
709 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
2 answers 671 views
671 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 4 views
4 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 5.5k points
0 votes
1 answer 34 views
34 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
1 vote
1 answer 111 views
111 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
1 vote
1 answer 30 views
30 views
Problem: Hey good people, I hope you are doing good. I was trying to write a program where, given a string name, e.g. "bob", return a greeting of the form "hello bob!". I was surfing over the internet and find this question from one of the websites. This problem is beyond my understanding. I looking forward to you for a solution. Thanks.
asked Mar 22, 2020 Gavin 15.3k points
0 votes
1 answer 226 views
226 views
Problem : I have written following code so far: Scanner scanit = new Scanner(System.in); String strscan = scanit.nextLine(); int x = strscan.length(); int y = strscan.charAt(x/2); System.out.println(strscan.substring(y, x-1)); Because of some reason when ... arrays and other advanced stuff. If someone can tell me how to resolve this issue in the easiest way possible that would be the great help!
asked Nov 26, 2019 alecxe 7.5k points
0 votes
2 answers 116 views
116 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 1.7K views
1.7K views
Problem : Need solution on bellow problem there are 4 bytes in an ipv4 address. what is the highest decimal value you can have for one byte?
asked Nov 8, 2019 peterlaw 6.9k points
0 votes
0 answers 22 views
22 views
I have a text file that consists of similar lines and few are half similar to other lines in a text file. Input.txt I would like to play: Volleyball I would like to play: Volleyball I would like to play: TableTennis I would like to play: Baseball I do not know how ... collected[key]}\n') I can print only the second line. Don't know where I am going wrong. Any help would be appreciated. Thank you
asked Jul 16, 2020 anonymous