Displaying all prefixes of a word in C++: 在C++中显示单词的所有前缀:

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

Displaying all prefixes of a word in C++

问题

我正在尝试显示单词的所有后缀,如下所示:

单词:house

打印输出:

h
ho
hou
hous
house

我的做法是:

#include <iostream>
#include <string.h>

using namespace std;

int main()
{
   char cuvant[100];
   int i, k;
   
   cin >> cuvant;
   
   for (i = 0; i < strlen(cuvant); i++)
   {
       for (k = 0; k < i; k++)
       {
           if (k == 0)
           {
               cout << cuvant[k] << endl;
           }
           else
           {
               for (k = 1; k <= i; k++)
               {
                   if (k == i) cout << endl;
                   cout << cuvant[k];
               }
           }
       }
   }
}

我做错了什么?

英文:

I am trying to do is display all the suffixes of a word as such:

word: house

print:

h
ho
hou
hous
house

What I did is:

#include &lt;iostream&gt;
#include &lt;string.h&gt;

using namespace std;

int main()
{
   char cuvant[100];
   int i,k;
   
   cin&gt;&gt;cuvant;
   
   for(i=0;i&lt;strlen(cuvant);i++)
   {
       for(k=0;k&lt;i;k++)
       {
           if(k==0)
           {
               cout&lt;&lt;cuvant[k]&lt;&lt;endl;
           }else
           {
             for(k=1;k&lt;=i;k++){
                 if(k==i) cout&lt;&lt;endl;
                 cout&lt;&lt;cuvant[k];
             }
           }
       }
   }
}

What am I doing wrong?

答案1

得分: 2

你过于复杂化了。这里有一种更简单的方法:

#include <iostream>
#include <string>
#include <string_view>

int main() {
  std::string s;
  std::cin >> s;
  for (std::string::size_type i = 0, size = s.size(); i != size; ++i)
    std::cout << std::string_view{s.c_str(), i + 1} << '\n';
}

如果你没有访问C++17编译器,你可以使用这个:

#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>

int main() {
  std::string s;
  std::cin >> s;
  for (auto const& ch : s) {
    std::copy(s.c_str(), (&ch + 1),
              std::ostream_iterator<decltype(ch)>(std::cout));
    std::cout << '\n';
  }
}
英文:

You're over-complicating it. Here's a simpler way:

#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;string_view&gt;

int main() {
  std::string s;
  std::cin &gt;&gt; s;
  for (std::string::size_type i = 0, size = s.size(); i != size; ++i)
    std::cout &lt;&lt; std::string_view{s.c_str(), i + 1} &lt;&lt; &#39;\n&#39;;
}

If you don't have access to a C++17 compiler, you can use this one:

#include &lt;algorithm&gt;
#include &lt;iostream&gt;
#include &lt;iterator&gt;
#include &lt;string&gt;

int main() {
  std::string s;
  std::cin &gt;&gt; s;
  for (auto const&amp; ch : s) {
    std::copy(s.c_str(), (&amp;ch + 1),
              std::ostream_iterator&lt;decltype(ch)&gt;(std::cout));
    std::cout &lt;&lt; &#39;\n&#39;;
  }
}

答案2

得分: 2

Here's the translated code:

尽管如此,我认为对于你的学习进度来说,使用调试器自己找出问题会更好。以下是你的代码存在的问题:

对于 `i=0`(外部循环的第一次迭代),`for(k=0;k&lt;i;k++)` 不会执行,因为 `k&lt;0` 计算结果为 `false`。

而且,在两个嵌套的 `for` 循环中更改运行变量 (`k`) 大多数情况下也表示有问题。

所以你想做的是:你想要创建每个可能的前缀,因此你想要创建长度为 `1` 到 `n` 的 `n` 个字符串。所以你的外部循环的第一个想法是正确的。但你在内部部分过于复杂了。

对于内部部分,你想要打印从索引 `0` 到 `i` 的所有字符。

```cpp
int main() {
    char cuvant[100];

    std::cin &gt;&gt; cuvant;

    // 循环遍历字符串的长度
    for (int i = 0, size = strlen(cuvant); i &lt; size; i++) {

        // 打印从 0 到 i 的所有字符 (k&lt;=0)
        for (int k = 0; k &lt;= i; k++) {
            std::cout &lt;&lt; cuvant[k];
        }
        // 打印换行符
        std::cout &lt;&lt; std::endl;
    }
}

但是,与其重新发明轮子,我建议使用标准库提供的函数:

int main() {
    std::string s;
    std::cin &gt;&gt; s;
    for (std::size_t i = 0, size = s.size(); i &lt; size; i++) {
        std::cout &lt;&lt; s.substr(0, i + 1) &lt;&lt; std::endl;
    }
}

<details>
<summary>英文:</summary>

Even so, I think it would be better for your learning progress to use a debugger to finger out the problem yourself. Here the problems with your code:

For the `i=0` (the first iteration of your outer loop)  the `for(k=0;k&lt;i;k++)` will not be executed at all, as `k&lt;0` evaluates to `false`.

And having a running variable (`k`) that you change in two `for` loops that are nested, is most of the time also an indication that something is wrong.

So what you want to do: You want to create each possible prefix, so you want to create `n` strings with the length of `1` to `n`. So your first idea with the outer loop is correct. But you overcomplicate the inner part.

For the inner part, you want to print all chars from the index `0` up to `i`.

int main() {
char cuvant[100];

std::cin &gt;&gt; cuvant;

// loop over the length of the string
for (int i = 0, size = strlen(cuvant); i &lt; size; i++) {

    // print all chars from 0 upto to i (k&lt;=0)
    for (int k = 0; k &lt;= i; k++) {
        std::cout &lt;&lt; cuvant[k];
    }
    // print a new line after that
    std::cout &lt;&lt; std::endl;
}

}


But instead of reinventing the wheel I would use the functions the std provides:

int main() {
std::string s;
std::cin >> s;
for (std::size_t i = 0, size = s.size(); i < size; i++) {
std::cout << s.substr(0, i + 1) << std::endl;
}
}


</details>



# 答案3
**得分**: 0

以下是翻译好的部分:

对于这个非常简单的字符串后缀任务,您可以使用以下代码:

```cpp
void main()
{
    std::string s = "house";
    std::string s2;
    for(char c : s) 
    {
        s2 += c;
        cout << s2 << endl;
    }
}

对于更复杂的问题,您可能会对后缀树感兴趣。

英文:

For this very simple string suffix task you can just use:

void main()
{
    std::string s = &quot;house&quot;;
    std::string s2;
    for(char c : s) 
    {
        s2 += c;
        cout &lt;&lt; s2 &lt;&lt; endl;
    }
}

For more complicated problems you may be interested to read about Suffix Tree

答案4

得分: -2

以下是您的代码翻译:

#include <iostream>
using namespace std;

int main()
{
   char cuvant[100];
   int i,k;

   cin >> cuvant;
   for(i=0; i<strlen(cuvant); i++)
   {
        for (k = 0; k <= i; ++k)
        {
            cout << cuvant[k];
        }
        cout << endl;
    }
}
英文:

Your code is wrong, the following code can fulfill your requirements

#include &lt;iostream&gt;
using namespace std;

int main()
{
   char cuvant[100];
   int i,k;

   cin&gt;&gt;cuvant;
   for(i=0;i&lt;strlen(cuvant);i++)
   {
        for (k = 0; k &lt;= i; ++k)
        {
            cout&lt;&lt;cuvant[k];
        }
        cout&lt;&lt;endl;
    }
}

huangapple
  • 本文由 发表于 2020年1月6日 16:14:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/59608706.html
匿名

发表评论

匿名网友

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

确定