×
FREE ASSISTANCE FOR THE INQUISITIVE PEOPLE
Tutorial Topics
X
softetechnologies
Java String Programs - Word Count in java Java String Programs - Replace word in java
Java String Programs - Find word in java - Java
3832    Arnab De    14/03/2018

Write a program in java to find out the frequency of specified words in a sentance.

It is a simple java string program. Just split the sentence into a string array by split() method and search the specified word in the array by using the equals() method of the java string class. Here we use the for each loop which is introduce in jdk1.5 version.

import java.util.*;
class FindWord
{
    public static void main(String []args)
    {
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter a String:");
        String str=sc.nextLine();
        System.out.print("Enter the word you want to search : ");
        String searchword=sc.nextLine();
        String []words=str.split(" "); //split the sentence into words
        int c=0;
        for (String word : words) {
            if(word.equals(searchword)){
                c++;
            }
        }
        System.out.println("The word " + searchword + " Found " + c + " Times.");
    }
}

softetechnologies

Write a program in java to find out the frequency of specified words in a sentance by ignoring the case of the charecter.

 

import java.util.*;
class FindWord
{
    public static void main(String []args)
    {
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter a String:");
        String str=sc.nextLine();
        System.out.print("Enter the word you want to search : ");
        String searchword=sc.nextLine();
        String []words=str.split(" "); //split the sentence into words
        int c=0;
        for (String word : words) {
            if(word.toLowerCase().equals(searchword.toLowerCase())){
                c++;
            }
        }
        System.out.println("The word " + searchword + " Found " + c + " Times.");
    }
}

softetechnologies

Alternate Way of find word in a sentence by ignoring the case.



import java.util.*;
class FindWord
{
    public static void main(String []args)
    {
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter a String:");
        String str=sc.nextLine();
        System.out.print("Enter the word you want to search : ");
        String searchword=sc.nextLine();
        String []words=str.split(" "); //split the sentence into words
        int c=0;
        for (String word : words) {
            if(word.equalsIgnoreCase(searchword)){
                c++;
            }
        }
        System.out.println("The word " + searchword + " Found " + c + " Times.");
    }
}

Java String Programs - Word Count in java Java String Programs - Replace word in java
softetechnologies
Author Details
Arnab De
I have over 16 years of experience working as an IT professional, ranging from teaching at my own institute to being a computer faculty at different leading institute across Kolkata. I also work as a web developer and designer, having worked for renowned companies and brand. Through tutorialathome, I wish to share my years of knowledge with the readers.
Enter New Comment
Comment History
No Comment Found Yet.
Leo Tolstoy
However diffcult life may seem, there is always something you can do, and succeed at. It matters that you don not just give up.
Leo Tolstoy
9583
72.01
Today So Far
Total View (Lakh)
softetechnologies
26/05/2018     51817
25/06/2018     43664
01/01/2018     42781
28/06/2017     40509
02/08/2017     39329
01/08/2017     33541
06/07/2017     33294
15/05/2017     32656
14/07/2017     28867
11/09/2018     28672
softetechnologies