×
FREE ASSISTANCE FOR THE INQUISITIVE PEOPLE
Tutorial Topics
X
softetechnologies
Java String Programs - Find word in java SMITH NUMBERS JAVA
Java String Programs - Replace word in java - Java
6998    Arnab De    14/03/2018

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


It is a simple java string program. Just split the sentence into a string array by split() method and create a resultent empty string. Now travel the string array and rejoin the stentence by replaceing the specefied word. Here we use the for each loop which is introduce in jdk1.5 version.

softetechnologies


import java.util.*;
class ReplaceWord
{
    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();
        System.out.print("Enter the new word : ");
        String newword=sc.nextLine();
        String res="";
        String []words=str.split(" "); //split the sentence into words
        for (String word : words) {
            if(word.equalsIgnoreCase(searchword))
            {
                res += newword + " ";
            }
            else
            {
                res += word + " ";
            }
        }
        System.out.println("New Sentence will be " + res.trim());
    }
}

softetechnologies
Java String Programs - Find word in java SMITH NUMBERS 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
07/02/2023: where wil java after 10 years
By: kjn [hoh]
Sri Sri Ramakrishna Paramahamsa
All religions are true. God can be reached by different religions. Many rivers flow by many ways but they fall into the sea. They all are one.
Sri Sri Ramakrishna Paramahamsa
1065
90.53
Today So Far
Total View (Lakh)
softetechnologies
26/05/2018     55463
25/06/2018     46556
01/01/2018     44521
28/06/2017     42248
02/08/2017     41382
01/08/2017     35225
06/07/2017     34989
15/05/2017     34124
11/09/2018     32233
14/07/2017     31250
softetechnologies