Monday, July 24, 2023

Write a program to accept a string from user and display the words that are having repeating characters.

 import java.util.Scanner;

import java.util.Random;

public class Main

{

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter a string: ");

String inputString = scanner.nextLine();

String[] words = inputString.split(" ");

System.out.println("Words with repeating characters:");

for (String word : words)

{

    if (hasRepeatingCharacters(word))

    {

        System.out.println(word);

    }

}

scanner.close();

}

private static boolean hasRepeatingCharacters(String word)

{

    for (int I = 0; I < word.length() - 1; I++)

    {

        char currentChar = word.charAt(I);

        for (int j = I + 1; j < word.length(); j++)

        {

            if (currentChar == word.charAt(j))

            {

                return true;

            }

        }

    }

        return false;

}

   

}



Enter a string: bbn bnb vbn xcdxx

Words with repeating characters:

bbn

bnb

xcdxx



No comments:

Post a Comment