Please open the menu to show more

Friday, August 11, 2017

Solution for the assignment "find occurrence of a word in string"

This code is implemented and provided by "ITI" From "Banasthali University"

import java.util.*;
public class StringIndex
{
public static void main(String[] args)
{
System.out.println("PLEASE ENTER A PARAGRAPGH : ");
Scanner sc=new Scanner(System.in);
System.out.println("PLEASE ENTER A WORD : ");
String p=sc.nextLine(); String w=sc.nextLine();
System.out.println("FIRST INDEX OF "+w+" = "+fi);
//finding first index of w in p int fi=p.indexOf(w);
System.out.println("LAST INDEX OF "+w+" = "+li);
//finding last index of w in p int li=p.lastIndexOf(w); int c=0;
//loop for upgrading the index in order to check
System.out.print("ALL THE INDEXES OF "+w+" = ");
//finding all the indexes of w in p
//it's further occurrence
while(fi>=0) {
//finding index of next occurrence until it returns -1
fi=p.indexOf(w,fi+1); System.out.print(fi+","); //calculating frequency
}
c++; } System.out.println("\n"+w+" OCCURRED "+c+" TIMES ");
}

No comments:

Post a Comment