• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

  • Home
  • Test Case Examples
  • Interview Questions
  • Interview Questions Asked
  • Java
  • Selenium
  • Manual Testing
  • SQL Tutorial For Beginners
  • Difference
  • Tools
  • Contact Us
  • Search
SoftwareTestingo » Java » Java Programs » WAP to Replace a Character Sequence With Increase Order

WAP to Replace a Character Sequence With Increase Order

Last Updated on: July 26, 2023 By Softwaretestingo Editorial Board

What We Are Learn On This Post

  • Replace a Character Sequence With Increase Order

WAP to Replace a Character Sequence With Increase Order: The given Java program manipulates a given input string “HELLO WORLD” to replace each occurrence of the letter ‘L’ with a sequence of ‘x’ characters. The output will be “HExxxO WORxxxD,” where the number of ‘x’ characters increases for each occurrence of ‘L.’

Replace a Character Sequence With Increase Order

package com.softwaretestingo.interviewprograms;
public class InterviewPrograms44 
{
	/*
	 * input: "HELLO WORLD" 
	 * Output: "HExxxO WORxxxD"
	 */
	public static void main(String[] args) 
	{
		String s="HELLO WORLD";
		String z="";
		int count=1;
		for(int i=0;i<s.length();i++)
		{
			if(s.charAt(i)=='L') 
			{
				for(int j=0;j<count;j++) 
				{
					z=z+'x';
				}
				count++;
			}
			else 
			{
				z=z+s.charAt(i);
			}
		}
		System.out.println(z);
	}
}

Output

HExxxO WORxxxD

Here’s a step-by-step explanation of the program:

  1. Define the main method:
    • The program starts executing from the main method.
  2. Initialize the input string:
    • The input string s is initialized with the value “HELLO WORLD.”
  3. Initialize variables:
    • A new string z is initialized to store the modified output.
    • An integer variable count is initialized to keep track of the number of ‘x’ characters to be added for each ‘L’ occurrence. It is initially set to 1.
  4. Iterate through the input string:
    • A loop runs from index 0 to the length of the string – 1.
  5. Check for ‘L’ occurrences:
    • In each iteration, it checks if the character at the current index is ‘L.’
    • If ‘L’ is found, it enters a nested loop.
  6. Add ‘x’ characters:
    • The nested loop runs count times, and in each iteration, it appends ‘x’ to the string z.
    • This will add ‘x’ characters based on the current value of count for each occurrence of ‘L.’
  7. Increment count:
    • After the nested loop, count is incremented to increase the number of ‘x’ characters for the next ‘L’ occurrence.
  8. Add other characters:
    • If the current character is not ‘L,’ it directly appends the character to the string z.
  9. Print the modified string:
    • After the loop, the modified string z is printed as the output.

The program replaces each occurrence of ‘L’ with an increasing number of ‘x’ characters based on the variable count and outputs the final modified string “HExxxO WORxxxD.”

    WAP To Remove Consecutive Duplicates from Array
    WAP To Remove Consecutive Duplicates from Array
    Mountain Array
    WAP to Validate Array Is a Mountain Array & Peak...
    Odd and Even Numbers In Java
    How to Find Odd and Even Numbers In Java?
    Simple Java Programs for Beginners
    Simple Java Programs for Beginners Example
    WAP to Print Duplicate Character Number Of Occurrence Times
    WAP to Print Duplicate Character Based on its Occurrence Times
    Print Character And Count Of a Word
    Write a Java Program[ abbcccdeee –>a1b2c3d1e3]
    WAP to Separate Two Characters With Digit
    WAP to Separate Two Characters With a Digit
    WAP to Reverse String Based on Commas
    WAP to Reverse String Based on Commas
    WAP to Count Occurrence of Characters in a String
    WAP to Count Occurrence of Characters in a String
    WAP to Print Highest ASCII Value Character From String
    WAP to Print Highest ASCII Value Character From String

    Filed Under: Java Programs

    Reader Interactions

    Leave a Reply Cancel reply

    Your email address will not be published. Required fields are marked *

    Primary Sidebar

    Join SoftwareTestingo Telegram Group

    Categories

    Footer

    Java Tutorial for Beginners | Selenium Tutorial | Manual Testing Tutorial | SQL Tutorial For Beginners | GitHub Tutorial For Beginners | Maven Tutorial

    Copyright © 2023 SoftwareTestingo.com ~ Contact Us ~ Sitemap ~ Privacy Policy ~ Testing Careers