This code is implemented and provided by Vasu.
About code : Using this program we can find out the minimum distance between two words inside a paragraph.
Note: Before using this code please create a Java project in Eclipse and save this class inside MinimumDistance.java file.
import java.util.Scanner;
public class MinimumDistance {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in) ;
String para = "We have the BST search and insert operations. "
+ "We also In the search post and delete operation is discussed. "
+ "When we delete a node, there possibilities arise." ;
para = para.toLowerCase();
System.out.println("Enter The two words : ");
String word1 = sc.next() , word2 = sc.next() ;
int index1 = -1 , index2 = -1 , min = Integer.MAX_VALUE;
for (int i = 0; i < para.length() ; i++) {
int count1 = 0 , count2 = 0 ;
for (int j = 0; j < word1.length(); j++) {
if(word1.charAt(j) != para.charAt(i+count1)) {
break ;
}
else {
if(count1 == word1.length()-1) {
index1 = i ;
count1 = 0 ;
break ;
}
else {
count1++ ;
}
}
}
for (int j = 0; j < word2.length(); j++) {
if(word2.charAt(j) != para.charAt(i+count2)) {
break ;
}
else {
if(count2 == word2.length()-1) {
index2 = i ;
count2 = 0 ;
break ;
}
else {
count2++ ;
}
}
}
if(index2 > index1) {
if(index2-index1 < min) {
min = index2-index1 ;
}
}
}
System.out.println("The Minimum distance between the two words "
+"in the para is : " + min);
}
}
Output of this program =>
Enter The two words :
We
is
The Minimum distance between the two words in the para is : 95
About code : Using this program we can find out the minimum distance between two words inside a paragraph.
Note: Before using this code please create a Java project in Eclipse and save this class inside MinimumDistance.java file.
import java.util.Scanner;
public class MinimumDistance {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in) ;
String para = "We have the BST search and insert operations. "
+ "We also In the search post and delete operation is discussed. "
+ "When we delete a node, there possibilities arise." ;
para = para.toLowerCase();
System.out.println("Enter The two words : ");
String word1 = sc.next() , word2 = sc.next() ;
int index1 = -1 , index2 = -1 , min = Integer.MAX_VALUE;
for (int i = 0; i < para.length() ; i++) {
int count1 = 0 , count2 = 0 ;
for (int j = 0; j < word1.length(); j++) {
if(word1.charAt(j) != para.charAt(i+count1)) {
break ;
}
else {
if(count1 == word1.length()-1) {
index1 = i ;
count1 = 0 ;
break ;
}
else {
count1++ ;
}
}
}
for (int j = 0; j < word2.length(); j++) {
if(word2.charAt(j) != para.charAt(i+count2)) {
break ;
}
else {
if(count2 == word2.length()-1) {
index2 = i ;
count2 = 0 ;
break ;
}
else {
count2++ ;
}
}
}
if(index2 > index1) {
if(index2-index1 < min) {
min = index2-index1 ;
}
}
}
System.out.println("The Minimum distance between the two words "
+"in the para is : " + min);
}
}
Output of this program =>
Enter The two words :
We
is
The Minimum distance between the two words in the para is : 95
No comments:
Post a Comment