• 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 Last Two Special Character With Dots

WAP to Replace Last Two Special Character With Dots

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

What We Are Learn On This Post

  • Replace Last Two Special Character With Dots

Replace Last Two Special Character With Dots: This Java program aims to remove the last alphanumeric character from a given input string and replace it with dots.

Replace Last Two Special Character With Dots

package com.softwaretestingo.interviewprograms;
public class InterviewPrograms40 
{
	/*
	 * Input string : Aut@oma#tion@# 
	 * Output: Aut@oma#tion.. 
	 * Input maybe any string, but only the last alphanumeric character should be removed
	 */
	public static void main(String[] args) 
	{
		String s = "Aut@oma#tion@#";
		String [ ] newstring = s.split("");
		int count = 0 ;
		String last = "";
		for ( int i = s.length()-1; i>=0;i--)
		{
			if (!newstring[i].matches("[a-zA-Z]"))
			{
				count++;
				last=last+".";
			}
			else
			{
				break;
			}
		}
		System.out.print(s.substring(0, s.length()-count)+last);	
	}
}

Output

Aut@oma#tion..

Here’s a breakdown of the program:

  1. Define the main method:
    • The entry point of the program is the main method.
  2. Initialize the input string and split it into an array of characters:
    • The string s is initialized with the value “Aut@oma#tion@#”.
    • The split(“”) method is used to convert the input string into an array of characters named newstring.
  3. Find the last non-alphanumeric character:
    • A variable count is initialized to zero to keep track of the number of non-alphanumeric characters at the end of the string.
    • A variable last is initialized as an empty string to store the dots that will replace the removed characters.
    • The program iterates through the characters in reverse order (from the end of the string to the beginning).
    • If a character is not a letter (uppercase or lowercase) using the regex [a-zA-Z], it means it is non-alphanumeric.
    • If the character is non-alphanumeric, the count is incremented, and a dot is appended to the last string.
    • If a letter is encountered, the loop breaks since we have reached the last alphanumeric character.
  4. Build the output string:
    • The program constructs the output string by removing the last count characters from the original string s and appending the last string (dots) at the end.
  5. Print the output:
    • The program prints the final s string, which has the last alphanumeric character replaced with dots.

For the given input “Aut@oma#tion@#”, the program removes the last alphanumeric character “n” and replaces it with dots, resulting in “Aut@oma#tion..”.

    WAP to Reverse Digits Only From a String
    WAP to Reverse Digits Only From a String
    Reverse Words without Changing Order & Split
    WAP to Reverse Words without Changing Order & Split
    WAP to Print Character and Occurrences Count
    WAP to Print Character and Occurrences Count
    Print Character And Count Of a Word
    Write a Java Program[ abbcccdeee –>a1b2c3d1e3]
    WAP to Print Highest ASCII Value Character From String
    WAP to Print Highest ASCII Value Character From String
    WAP to Cyclically Shifting the Input String
    WAP to Cyclically Shifting the Input String
    Frequency Of Words In A String
    WAP To Print Frequency Of Words In A String
    Swap Two Numbers In Java
    Swap Two Numbers In Java
    Factorial Number In Java
    Factorial Number In Java
    Product of Array Except Itself Program
    Product of Array Except Itself

    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