w3resource

Java: Check whether the character immediately before and after a specified character is same in a given string


80. Check Char Surrounded by Same Char

Write a Java program to check whether the character immediately before and after a specified character is the same in a given string.

Visual Presentation:

Java String Exercises: Check whether the character immediately before and after a specified character is same in a given string.


Sample Solution:

Java Code:

import java.util.*;

// Define a class named Main
public class Main {

  // Method to check if characters before and after '#' are the same
  public boolean leftAndRightSame(String stng) {
    int l = stng.length(); // Get the length of the given string 'stng'
    boolean found = true; // Initialize a boolean variable 'found' as true

    // Loop through each character in the given string 'stng'
    for (int i = 0; i < l; i++) {
      String tmpString = stng.substring(i, i + 1); // Extract each character of 'stng' individually

      // Check if the character is '#', and it's not the first or last character
      if (tmpString.equals("#") && i > 0 && i < l - 1) {
        // Check if the character before '#' is equal to the character after '#'
        if (stng.charAt(i - 1) == stng.charAt(i + 1)) {
          found = true; // Set 'found' to true if characters before and after '#' are the same
        } else {
          found = false; // Set 'found' to false if characters before and after '#' are different
        }
      }
    }
    return found; // Return the result whether characters before and after '#' are the same or not
  }

  // Main method to execute the program
  public static void main(String[] args) {
    Main m = new Main(); // Create an instance of the Main class

    String str1 = "moon#night"; // Given string

    // Display the given string and the result of checking characters before and after '#'
    System.out.println("The given string is: " + str1);
    System.out.println("The before and after character are same: " + m.leftAndRightSame(str1));
  }
}

Sample Output:

The given string is: moon#night
The before and after character are same: true

The given string is: bat##ball
The before and after character are same: false

The given string is: #moon#night
The before and after character are same: true

Flowchart:

Flowchart: Java String Exercises - Check whether the character immediately before and after a specified character is same in a given string.



For more Practice: Solve these Related Problems:

  • Write a Java program to check if the characters adjacent to a specified character in a string are identical.
  • Write a Java program to verify if for every occurrence of a given character, the preceding and succeeding characters match.
  • Write a Java program to determine if a character in a string is flanked by the same character on both sides.
  • Write a Java program to scan a string and return true if the character immediately before and after a given target are equal.

Go to:


PREV : Remove Middle Char in 'z?g' Pattern.
NEXT : Compare 3-char and 4-char Substrings.

Java Code Editor:

Improve this sample solution and post your code through Disqus

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.