Introduction
This question is about finding the distance between two specific words in a String. This can be done by using a combination of the String.indexOf() and String.substring() methods. The indexOf() method will return the index of the first occurrence of the specified word in the String, and the substring() method will return the part of the String between the two specified indices. With these two methods, it is possible to calculate the distance between two words in a String.
Solution
public class DistanceBetweenWords {
public static int getDistance(String str, String word1, String word2) {
int index1 = str.indexOf(word1);
int index2 = str.indexOf(word2);
if (index1 == -1 || index2 == -1) {
return -1;
}
return Math.abs(index1 – index2);
}
public static void main(String[] args) {
String str = “Hello World, this is a test string”;
String word1 = “World”;
String word2 = “test”;
System.out.println(getDistance(str, word1, word2));
}
}
Just a pointer, you can optimize the code:
public static void main(String[] args) {
String str = "The picture quality is great of this camera";
StringTokenizer st = new StringTokenizer(str);
int numberOfWords = 0;
boolean start = false;
while(st.hasMoreTokens()){
String token = st.nextToken();
if(token.equals("quality")){
start = true;
continue;
}
if(start) {
if(token.equals("great")){
start = false;
}
else {
numberOfWords++;
}
}
}
System.out.println(numberOfWords);
}
4
solved Find out the distance between two specific words in a String [closed]
Finding the distance between two specific words in a String can be a tricky task. Fortunately, there are a few methods that can be used to accomplish this. The first method is to use the String.indexOf() method. This method takes two parameters, the first being the word you are looking for and the second being the starting index. The method will then return the index of the first occurrence of the word. Once you have the index of the first word, you can subtract it from the index of the second word to get the distance between them.
Another method is to use the String.split() method. This method takes a String and splits it into an array of Strings based on a delimiter. You can then loop through the array and find the index of the two words you are looking for. Once you have the indexes, you can subtract them to get the distance between the two words.
Finally, you can use regular expressions to find the distance between two words. Regular expressions are powerful tools that allow you to search for patterns in Strings. You can use a regular expression to search for the two words you are looking for and then use the String.indexOf() method to get the index of each word. Once you have the indexes, you can subtract them to get the distance between the two words.
Finding the distance between two specific words in a String can be a tricky task, but with the right methods, it can be done. Using the String.indexOf() method, the String.split() method, or regular expressions can all be used to find the distance between two words in a String.