w3resource

Java: Check whether a prefix string creates using the first specific characters in a given string appears somewhere else in the string

Java String: Exercise-74 with Solution

Write a Java program to check whether a prefix string created using the first specific character in a given string appears somewhere else in the string.

Visual Presentation:

Java String Exercises: Check whether a prefix string creates using the first specific characters in a given string appears somewhere else in the string.

Sample Solution:

Java Code:

import java.util.*;

// Define a class named Main
public class Main {

    // Method to check if a substring of length 'n_chr' appears elsewhere in the string
    public boolean nChrStringSomewhere(String stng, int n_chr) {
        int len = stng.length(); // Get the length of the input string

        // Extract the prefix substring of length 'n_chr' from the input string
        String pre_str = stng.substring(0, n_chr);

        for (int i = n_chr; i < len; i++) {
            // Check if 'n_chr' characters can be extracted starting from the current index 'i'
            if (n_chr + i <= len) {
                // Extract a substring of length 'n_chr' starting from index 'i'
                if (pre_str.equals(stng.substring(i, n_chr + i))) // Compare the extracted substring with the prefix
                    return true; // If a match is found, return true
            }
        }
        return false; // If no match is found, return false
    }

    // 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 = "MrsJemsMrsam"; // Input string to be checked
        int n = 3; // Define the length of the prefix to be checked
        String prechr = str1.substring(0, n); // Extract the prefix of length 'n' from the input string

        // Display the given string, prefix string length, and check if the prefix appears elsewhere in the string
        System.out.println("The given string is: " + str1);
        System.out.println("The prefix string length is: " + n);
        System.out.println("Does '" + prechr + "' appear elsewhere in the string? " + m.nChrStringSomewhere(str1, n));
    }
}

Sample Output:

The given strings is: MrsJemsmrsam
The prefix string length is: 3
Is 'Mrs' appear else where in the string? false

The given string is: MrsJemsMrsam
The prefix string length is: 3
Is 'Mrs' appear else where in the string? true

Flowchart:

Flowchart: Java String Exercises - Check whether a prefix string creates using the first specific characters in a given string appears somewhere else in the string.

Java Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a Java program to check whether a substring appears before a period(.) within a given string.
Next: Write a Java program to check whether a given substring presents in the middle of another given string. Here middle means difference between the number of characters to the left and right of the given substring not more than 1.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/java-exercises/string/java-string-exercise-74.php