How to repeat each of the individual letters in a piece of text? in Java

后端 未结 4 1248
悲&欢浪女
悲&欢浪女 2021-01-17 00:47

As in a stutter the number of times specified by the provided multiplier if the text was \"dean\" and the multiplier 3, the result would be \"ffffdeeeaaannn\".



        
相关标签:
4条回答
  • 2021-01-17 00:56

    You are just appending the string itself n times for each character in it. You need to iterate through the string and append each character n times.

    public static void repeatLetters()
    {
        String text = "dean";
        int n = 3;
        StringBuilder repeat = new StringBuilder();
        for (int i = 0; i < text.length(); i++) {
            for (int j = 0; j < n; j++) {
                repeat.append(text.charAt(i));
            }
        }
        System.out.println(repeat);
    }
    

    Also, another solution would be to use regular expressions.

    public static void repeatLetters()
    {
        String text = "dean", replace = "";
        int n = 3;
        for (int i = 0; i < n; i++) replace += "$1";
        System.out.println(text.replaceAll("(.)", replace));
    }
    
    0 讨论(0)
  • 2021-01-17 01:02

    You are simply appending the word "dean" to the end of your string three times.

    Try instead looping through each char in the word dean and appending that char three times.

    Something like this (this is pseudocode):

    StringBuilder repeat = new StringBuilder();
    
    ForEach( letter in "dean" )
        For( int i = 0 ; i < 3 ; i++ )
            repeat.add(letter);
    
    Return repeat
    
    0 讨论(0)
  • 2021-01-17 01:08

    You are repeating the whole word here .. a possible fix is

    public static void repeatLetters()
    {
       String text = "dean";
       int n = 3;
       StringBuilder repeat = new  StringBuilder();
    
       for (int i = 0; i < text.length() ; i++)
       {
            for (int j= 0; j< n; j++)
                repeat.append(text.charAt(i));
       }
    
      System.out.println(repeat);
    }
    
    0 讨论(0)
  • 2021-01-17 01:09

    Two problems:

    1. You are not printing the String that you have manipulated, you are printing the original String you started with, ie "dean". To print the string from the StringBuilder you can use

      System.out.println(repeat);

    2. You are adding the whole word "dean" to your original word instead of adding individual letters/chars. You need to iterate through every letter in your original word and add those letters to an empty StringBuilder. Here is the basic logic you should use to get you going:

      • Get original word ("dean")
      • Create an empty StringBuilder
      • Parse through each letter of your original word ("dean") either by using a for loop and getting each char in the string or using String.split and parsing the Array.
      • For each letter in your original word, append that letter with that letter n times to your StringBuilder.
      • Print the string from StringBuilder once you have parsed through all the letters in the original word.
    0 讨论(0)
提交回复
热议问题