Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For this exercise you are going to make a new method named expandString. This me

ID: 3689318 • Letter: F

Question

For this exercise you are going to make a new method named expandString. This method should take a String as input and return back a new String that has been expanded by repeating each character in the String a number of times equal to its position in the String. For example - the character at position zero should be repated 0 times, the character at position 1 one time, the character at position 2 two times and so on. This new method should use StringBuilder to create the new String in a fashion similar to how Exercise 1 used StringBuilder. Modify your main method to call this new method.


Your method should use the following method header and comments:

1

2

3

4

5

6

7

8

9

10

/**

* Expands a String by repeating characters in the String.

* The character at position 0 is repeated 0 times, position one

* is repeated once, position 2 is repeated twice and so on.

* @param input String to expand

* @return the input String expanded

*/

public static String expandString(String input) {

// your code goes here

}

1

2

3

4

5

6

7

8

9

10

/**

* Expands a String by repeating characters in the String.

* The character at position 0 is repeated 0 times, position one

* is repeated once, position 2 is repeated twice and so on.

* @param input String to expand

* @return the input String expanded

*/

public static String expandString(String input) {

// your code goes here

}

Exercise 2 Description Create a copy of your solution for Exercise 1 and name it Lab13b.java in the same ClosedLab13 folder. For this exercise you are going to make a new method named expandString. This method should take a String as input and return back a new String that has been expanded by repeating each character in the String a number of times equal to its position in the String. For example - the character at position zero should be repated 0 times, the character at position 1 one time, the character at position 2 two times and so on. This new method should use StringBuilder to create the new String in a fashion similar to how Exercise 1 used StringBuilder. Modify your main method to call this new method Your method should use the following method header and comments: 2 Expands a String by repeating characters in the String 3 The character at position e is repeated times, position one 4 is repeated once, position 2 is repeated twice and so on. 5 @param input String to expand 6 *@return the input String expanded 8 public static String expandString(String input) 9 // your code goes here 18 And your program should be able to produce the following transcript: Enter a string to expand: Howdy Expanded: owwdddyyyy Enter a string to expand: CSE1223 Expanded SEE111222222222333333 Goodbye! Note that the character at position zero appears 0 times, so it is not a bug if the first character you type does not appear in the output at all

Explanation / Answer

StringExpansion.java

package org.students;

import java.util.Scanner;

public class StringExpansion {
/* Method to expand the input string */
   public static String expandString(String input) {
       int len = input.length();
       char carr[] = new char[len];
       /* For loop which will convert the string into character array */
       for (int i = 0; i < len; i++) {

           carr[i] = input.charAt(i);
           /* For loop which displays the character that many number of times based on the position number in the character array */
           for (int j = 0; j < i; j++) {
               System.out.print(carr[i] + " ");
           }
       }
       return input;

   }

   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       /* Which will get the String Entered by the user */
       System.out.print("Enter the String ::");
       String str = sc.next();
       /* Calling the method by passing the string as argument */
       expandString(str);

   }

}

_________________________________________________________________________________________

output:

Enter the String ::harish123
a r r i i i s s s s h h h h h 1 1 1 1 1 1 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3

_________________________________________________________________________________________

output:

Enter the String ::hello
e l l l l l o o o o

_________________________________________________________________________________________

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote