英文:
Print the word that contains our pattern with the position
问题
I tried to print the word that contains our pattern with the position. I tried to use a boolean variable to check whether the text match if it matches then print but it's not working. How can I print the text with the position?
If the String pat is in the string text, then print the index which is already found and print the word that contains String pat. For example, est is inside testing, so we want to print the word testing.
Current Output
Pattern found at index 20
Pattern found at index 49
Pattern found at index 89
Want Output
Pattern found at index 20 in-word testing
Pattern found at index 49 in-word test
Pattern found at index 89 in-word est's
public class FindWord {
public static void search(String txt, String pat)
{
int M = pat.length();
int N = txt.length();
for (int i = 0; i <= N - M; i++) {
int j;
for (j = 0; j < M; j++)
if (txt.charAt(i + j) != pat.charAt(j))
break;
if (j == M)
System.out.println("Pattern found at index " + i + " in-word " + txt.substring(i, i + M));
}
}
public static void main(String[] args)
{
String txt = "This is only for a testing input. +\n" +
"This is for testing, example input.+\n" +
"We want to find est's index and print text";
String pat = "est";
search(txt, pat);
}
}
英文:
I tried to print the word that contains our pattern with the position. I tried to use a boolean variable to check whether the text match if it matches then print but it's not working. How can I print the text with the position?
If the String pat is in the string text, then print the index which is already found and print the word that contains String pat. For example, est is inside testing, so we want to print the word testing.
Current Output
Pattern found at index 20
Pattern found at index 49
Pattern found at index 89
Want Output
Pattern found at index 20 in-word testing
Pattern found at index 49 in-word test
Pattern found at index 89 in-word est's
public class FindWord {
public static void search(String txt, String pat)
{
int M = pat.length();
int N = txt.length();
//boolean found = false;
for (int i = 0; i <= N - M; i++) {
int j;
for (j = 0; j < M; j++)
if (txt.charAt(i + j) != pat.charAt(j))
break;
if (j == M)
// found = true;
System.out.println("Pattern found at index " + i);
// if(found)
// System.out.println(txt.charAt(i));
}
}
public static void main(String[] args)
{
String txt = "This is only for a testing input. +\n" +
"This is for testing, example input.+\n" +
"We want to find est's index and print text";
String pat = "est";
search(txt, pat);
}
}
</details>
# 答案1
**得分**: 1
根据我理解,您想要打印出包含子字符串的整个单词。您可以通过搜索找到的字符串左侧和右侧的空格字符来提取这个单词,类似以下方式:
```java
if (j == M) {
// 在字符串左侧搜索空格
int leftSpace = i;
while (leftSpace > 0 && txt.charAt(leftSpace) != ' ') {
leftSpace--;
}
// 在字符串右侧搜索空格
int rightSpace = i + j;
while (rightSpace < txt.length() && txt.charAt(rightSpace) != ' ') {
rightSpace++;
}
// 特殊情况,模式在文本开头的情况
if (leftSpace != 0) {
leftSpace++;
}
String word = txt.substring(leftSpace, rightSpace);
System.out.println("Pattern found at index " + i + " in-word " + word);
}
英文:
As I understand, you want to print the whole word where the substring was found. You could extract this word by searching for space characters to the left and to the right of the found string, something like the following:
if (j == M) {
// searching for a space to the left of the string
int leftSpace = i;
while (leftSpace > 0 && txt.charAt(leftSpace) != ' ') {
leftSpace--;
}
// searching for a space to the right of the string
int rightSpace = i + j;
while (rightSpace < txt.length() && txt.charAt(rightSpace) != ' ') {
rightSpace++;
}
// corner-case where the pattern is at the beginning of the text
if (leftSpace != 0) {
leftSpace++;
}
String word = txt.substring(leftSpace, rightSpace);
System.out.println("Pattern found at index " + i + " in-word " + word);
}
答案2
得分: 1
你可以使用 lastIndexOf
和 indexOf
来获取空格的索引并获取单词。
if (j == M){
int s = txt.lastIndexOf(' ', i) + 1;
int e = txt.indexOf(' ', i);
if(e == -1) e = N;
System.out.println("Pattern found at index " + i + " " + txt.substring(s, e));
}
英文:
You can use lastIndexOf
and indexOf
to get the index of space and get the word.
if (j == M){
int s = txt.lastIndexOf(' ', i) + 1;
int e = txt.indexOf(' ', i);
if(e == -1) e = N;
System.out.println("Pattern found at index " + i +" "+ txt.substring(s, e));
}
答案3
得分: 0
如果j等于M,那么会打印出字符串pat在字符串txt中的索引,如下所示:
if (j == M) {
System.out.printf("Pattern found at index %d in-word %s%n", i, pat);
}
此外,您还可以使用indexOf()方法来实现这个目标。查看Java String文档:https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#indexOf(java.lang.String)
英文:
Don't quite understand your question: do you want to print the index and the String pat if it is found in the String txt?
if (j == M) {
System.out.printf("Pattern found at index %d in-word %s%n", i, pat);
}
In addition, you could use indexOf() method to reach the goal. Take a look at Java String: https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#indexOf(java.lang.String)
答案4
得分: -1
Here's the translated code without any additional content:
if (j == M)
{
String s = "";
for(int k = i; txt.charAt(k) != ' ' && k >= 0; k--) s = txt.charAt(k) + s;
for(int k = i + 1; txt.charAt(k) != ' ' && k < txt.length(); k++) s += txt.charAt(k);
System.out.println("Pattern found at index " + i + ", " + s);
}
Please note that the code provided has been translated to Chinese without any additional information.
英文:
if (j == M)
{
String s = "";
for(int k = i; txt.charAt(k) != ' ' && k >= 0; k--) s = txt.charAt(k) + s;
for(int k = i + 1; txt.charAt(k) != ' ' && k < txt.length(); k++) s += txt.charAt(k);
System.out.println("Pattern found at index " + i + ", " + s);
}
Something like this but you need to modify some detail to make it bug-free.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论