简体   繁体   中英

Given a string, return a string where for every char is the original, there are two chars?

I want to print out every string twice backwards for example (string -> ggnniirrttss)

import java.util.Scanner;
public class ReverseDoubleChar {
public static void main(String[] args) {

    Scanner input = new Scanner(System.in);
    System.out.println("Enter a string ");
    String str = input.nextLine();
    String new_str = "";

             String result = "";
    for (int i = 0; i < str.length(); i++) {
        result += str.substring(i, i + 1) + str.substring(i, i + 1);
        String result2 = null;
        result2 = result;
        System.out.println(result2);
        }
    }
}

Yet when I enter this code all I get is

ss
tt
rr
ii
nn
gg

all on new lines. Anybody know how to solve this? Thanks

Make the for loop iterate backwards:

String result = "";
for (int i = str.length() - 1; i >= 0; i--) {
    String s = str.substring(i, i + 1);
    result += s + s;
}

System.out.println(result);

or with a forwards loop:

String result = "";
for (int i = 0; i < str.length(); i++) {
    String s = str.substring(i, i + 1);
    result = s + s + result;
}

System.out.println(result);

See the comments on the lines I changed:

  1. Iterate the string backwards
  2. Don't print each double char in each loop iteration since you are concatenating the final result
  3. Print the final result after the loop

    import java.util.Scanner; public class ReverseDoubleChar { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Enter a string "); String str = input.nextLine(); String new_str = ""; String result = ""; for (int i = str.length() - 1; i >= 0; i--) { //Iterate through string backwards result += str.substring(i, i + 1) + str.substring(i, i + 1); //Concatenation String result2 = null; result2 = result; // System.out.println(result2); //Don't print each double char since you're concatenating above } System.out.println(result); //Print full double-charred string after loop } }

Output:

Enter a string 
string
ggnniirrttss

Maybe use a StringBuilder instead of doing your own substring and concatenations.

public class ReverseDoubleChar {
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        System.out.println("Enter a string ");
        StringBuilder str = new StringBuilder(input.nextLine());        
        StringBuilder result = new StringBuilder();

        for (int i = str.length() - 1; i >= 0; i--) {
            result.append(str.charAt(i));
            result.append(str.charAt(i));
        }
        System.out.println(result.toString());
    }
}
String result="";
for(int i=0;i<str.length();i++){
result=result+str.charAt(i)+ str.charAt(i);
}
return result;

This code works fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM