Replace a Specific Character with # Increasing Number: This Java program takes an input string “tomorrow” and converts it into a modified version where each occurrence of the letter ‘o’ is replaced with an increasing number of ‘#’ symbols. The number of ‘#’ symbols for each ‘o’ is determined by its position in the original string.
Replace a Specific Character with # Increasing Number
package com.softwaretestingo.interviewprograms; public class InterviewPrograms12 { /* * Input: tomorrow * Output: t#m##rr###w */ public static void main(String[] args) { StringBuilder charactersToAppend = new StringBuilder("#"); String input = "tomorrow"; while (input.contains("o")) { input = input.replaceFirst("o", charactersToAppend.toString()); charactersToAppend.append("#"); } System.out.println(input); } }
Output:
t#m##rr###w
Explanation:
- We start by importing the required package and defining a class named InterviewPrograms12.
- Inside the class, we declare the main method, which serves as the entry point for the program.
- We create a StringBuilder called charactersToAppend initialized with the ‘#’ character. This StringBuilder will be used to store additional ‘#’ symbols as we iterate through the input string.
- The input string “tomorrow” is assigned to the variable input.
- The program enters a while loop that continues as long as the input contains the letter ‘o’.
- Inside the loop, we use the replaceFirst method of the String class to replace the first occurrence of ‘o’ with the contents of the charactersToAppend StringBuilder. Initially, only one ‘#’ is appended, but with each iteration, an additional ‘#’ is appended to the charactersToAppend StringBuilder.
- After the replacement, the loop continues to find the next occurrence of ‘o’ until all ‘o’s are replaced.
- Finally, the modified input string is printed, which will be “t#m##rr###w”.
In summary, this program demonstrates the use of a while loop and the replaceFirst method to manipulate strings, and it shows how to build a modified version of the input string based on the occurrences of a specific character.
Alternative 1:
package com.softwaretestingo.interviewprograms; public class InterviewPrograms12_1 { /* * Input: tomorrow * Output: t#m##rr###w */ public void getSolution(String input) { int count = 0; String output = ""; char ch[] = input.toCharArray(); for(int i=0; i<ch.length; i++) { if(ch[i] == 'o') { count++; for(int j=0; j<count; j++) { output = output+Character.toString(ch[i]); output = output.replace(ch[i], '#'); } } else { output = output+Character.toString(ch[i]); } } System.out.println(output); } public static void main(String[] args) { String s = "tomorrow"; InterviewPrograms12_1 soluSoultionTest = new InterviewPrograms12_1(); soluSoultionTest.getSolution(s); } }
Output:
t#m##rr###w
Alternative 2:
This Java program aims to take an input string and modify it to replace each occurrence of the letter ‘o’ with an increasing number of ‘#’ symbols. It demonstrates an alternative approach to achieving the same result as the previous program using a different logic.
package com.softwaretestingo.interviewprograms; public class InterviewPrograms12_2 { /* * Input: tomorrow * Output: t#m##rr###w */ public static String replaceWithString(String input,char present,String replace ) { int count =0; String s =""; while(input.contains(Character.toString(present))) { int n = input.indexOf(present)>0?(count++):(count=0); if(count == n) break; for(int i= count; i>1; i--) { s+=replace; } input= input.replaceFirst(Character.toString(present),s); } return input; } public static void main(String[] args) { String result=replaceWithString("tomorrow",'o',"#"); System.out.println(result); } }
Output:
t#m##rr###w
Here’s a step-by-step explanation of how the program works:
- The program defines a class named InterviewPrograms12_1.
- Inside the class, there is a method called getSolution, which takes a single parameter – the input string.
- The method initializes a variable count to keep track of the number of occurrences of ‘o’ and an empty string output to store the modified result.
- The input string is converted into a character array ch.
- The program iterates through each character of the input string using a for loop.
- If the current character is ‘o’, it increments the count and enters an inner loop to append the ‘o’ character and then replace it with ‘#’ count number of times in the output string.
- If the current character is not ‘o’, it simply appends the character to the output string.
- After processing all characters, the modified output string is printed as the final result.
In summary, the program showcases an alternate way to modify the input string, replacing ‘o’ characters with an increasing number of ‘#’ symbols based on the number of occurrences. It employs nested loops to achieve the desired result and displays the modified output as “t#m##rr###w” when the input is “tomorrow”.
Leave a Reply