打印包含我们模式的单词及其位置。

huangapple go评论42阅读模式
英文:

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&#39;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 &lt;= N - M; i++) {

            int j;

            for (j = 0; j &lt; M; j++)
                if (txt.charAt(i + j) != pat.charAt(j))
                    break;

            if (j == M)
               // found = true;
                System.out.println(&quot;Pattern found at index &quot; + i);
//                if(found)
//                    System.out.println(txt.charAt(i));
        }
    }

    public static void main(String[] args)
    {
        String txt = &quot;This is only for a testing input. +\n&quot; +
                     &quot;This is for testing, example input.+\n&quot; +
                     &quot;We want to find est&#39;s index and print text&quot;;
        String pat = &quot;est&quot;;
        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 &gt; 0 &amp;&amp; txt.charAt(leftSpace) != &#39; &#39;) {
        leftSpace--;
    }

    // searching for a space to the right of the string
    int rightSpace = i + j;
    while (rightSpace &lt; txt.length() &amp;&amp; txt.charAt(rightSpace) != &#39; &#39;) {
        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(&quot;Pattern found at index &quot; + i + &quot; in-word &quot; + word);
}

答案2

得分: 1

你可以使用 lastIndexOfindexOf 来获取空格的索引并获取单词。

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(&#39; &#39;, i) + 1;
      int e = txt.indexOf(&#39; &#39;, i);
      if(e == -1) e = N;
      System.out.println(&quot;Pattern found at index &quot; + i +&quot; &quot;+ 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(&quot;Pattern found at index %d in-word %s%n&quot;, 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 = &quot;&quot;;
    for(int k = i; txt.charAt(k) != &#39; &#39; &amp;&amp; k &gt;= 0; k--) s = txt.charAt(k) + s;
              
    for(int k = i + 1; txt.charAt(k) != &#39; &#39; &amp;&amp; k &lt; txt.length(); k++) s += txt.charAt(k);
              
    System.out.println(&quot;Pattern found at index &quot; + i + &quot;, &quot; + s);
}

Something like this but you need to modify some detail to make it bug-free.

huangapple
  • 本文由 发表于 2020年7月29日 13:31:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/63146886.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定