Get a string and a positive integer n as input. The last n characters should repeat the number of times given as second input. Write code to repeat the set of characters from the given string.
The return type of the output is a string with repeated n characters.
Input and Output Format
- Input consists of a string and a positive integer n.
- Output is a string with repeated characters.
Refer sample output for formatting specifications
Sample Input 1:
codebun
3
Sample Output 1:
codebunbunbunbun
Sample Input 2:
java
2
Sample Output 2:
javavava
Find the repeating set of characters in a string in Java
Following are the steps to find the repeating set of characters in a string:
- Input string and integer value from the user.
- Pass both of them to the lengthiestString() method.
- Inside the method, create an empty string buffer to hold the updated string.
- First, append the input string to the string buffer.
- Use for loop and traverse over the string and get the substring from the string and repeat them for the given ‘n’ integer values.
- At last, append it to the string buffer.
package com.demo2;
import java.util.Scanner;
public class RepeatingString {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("enter the String:");
String s1 = s.nextLine();
int n = s.nextInt();
System.out.println("the lengthiest string is:" + lengthiestString(s1, n));
}
public static String lengthiestString(String s1, int n) {
StringBuffer sb = new StringBuffer();
sb.append(s1);
for (int i = 0; i < n; i++) {
sb.append(s1.substring(s1.length() - n, s1.length()));
}
return sb.toString();
}
}
Output
Frequency of Repeating Character using HashMap
Get a string and a character as input. Write code to find the frequency of repeating characters from the given string.
Input and Output Format
- Input consists of a string.
- Output is an integer.
Refer sample output for formatting specifications
Sample Input :
old is gold
o
Sample Output :
2
Following are the steps to count the number of repeating characters:
- Input string and character from the user.
- Pass the character to the repeatingChars() method.
- Inside the method, create an empty HashMap.
- Iterate over each character of the input string and perform the following operation:
- If the character is not present yet in the HashMap, it means it is the first time the character appears in the String; then, set the count as one.
- If the character were already in the String, then increase the count and add it to the map.
- At last, return the map value.
package com.demo2;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class RepeatingString {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String str = scanner.nextLine();
Map<Character,Integer> m1 = repeatingChars(str);
System.out.println("Enter character");
char c = scanner.next().charAt(0);
System.out.println(c+" is repeated "+m1.get(c)+" times");
}
private static Map<Character, Integer> repeatingChars(String str) {
Map<Character,Integer> map = new HashMap<>();
for(Character character: str.toCharArray()){
if(map.get(character)!=null){
int count = map.get(character)+1;
map.put(character,count);
}
else {
map.put(character,1);
}
}
return map;
}
}
Output
Thus, in this way, we find the repeating sets of characters in Java.