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

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

Displaying all prefixes of a word in C++

问题

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

单词:house

打印输出:

  1. h
  2. ho
  3. hou
  4. hous
  5. house

我的做法是:

  1. #include <iostream>
  2. #include <string.h>
  3. using namespace std;
  4. int main()
  5. {
  6. char cuvant[100];
  7. int i, k;
  8. cin >> cuvant;
  9. for (i = 0; i < strlen(cuvant); i++)
  10. {
  11. for (k = 0; k < i; k++)
  12. {
  13. if (k == 0)
  14. {
  15. cout << cuvant[k] << endl;
  16. }
  17. else
  18. {
  19. for (k = 1; k <= i; k++)
  20. {
  21. if (k == i) cout << endl;
  22. cout << cuvant[k];
  23. }
  24. }
  25. }
  26. }
  27. }

我做错了什么?

英文:

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

word: house

print:

  1. h
  2. ho
  3. hou
  4. hous
  5. house

What I did is:

  1. #include &lt;iostream&gt;
  2. #include &lt;string.h&gt;
  3. using namespace std;
  4. int main()
  5. {
  6. char cuvant[100];
  7. int i,k;
  8. cin&gt;&gt;cuvant;
  9. for(i=0;i&lt;strlen(cuvant);i++)
  10. {
  11. for(k=0;k&lt;i;k++)
  12. {
  13. if(k==0)
  14. {
  15. cout&lt;&lt;cuvant[k]&lt;&lt;endl;
  16. }else
  17. {
  18. for(k=1;k&lt;=i;k++){
  19. if(k==i) cout&lt;&lt;endl;
  20. cout&lt;&lt;cuvant[k];
  21. }
  22. }
  23. }
  24. }
  25. }

What am I doing wrong?

答案1

得分: 2

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

  1. #include <iostream>
  2. #include <string>
  3. #include <string_view>
  4. int main() {
  5. std::string s;
  6. std::cin >> s;
  7. for (std::string::size_type i = 0, size = s.size(); i != size; ++i)
  8. std::cout << std::string_view{s.c_str(), i + 1} << '\n';
  9. }

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

  1. #include <algorithm>
  2. #include <iostream>
  3. #include <iterator>
  4. #include <string>
  5. int main() {
  6. std::string s;
  7. std::cin >> s;
  8. for (auto const& ch : s) {
  9. std::copy(s.c_str(), (&ch + 1),
  10. std::ostream_iterator<decltype(ch)>(std::cout));
  11. std::cout << '\n';
  12. }
  13. }
英文:

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

  1. #include &lt;iostream&gt;
  2. #include &lt;string&gt;
  3. #include &lt;string_view&gt;
  4. int main() {
  5. std::string s;
  6. std::cin &gt;&gt; s;
  7. for (std::string::size_type i = 0, size = s.size(); i != size; ++i)
  8. std::cout &lt;&lt; std::string_view{s.c_str(), i + 1} &lt;&lt; &#39;\n&#39;;
  9. }

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

  1. #include &lt;algorithm&gt;
  2. #include &lt;iostream&gt;
  3. #include &lt;iterator&gt;
  4. #include &lt;string&gt;
  5. int main() {
  6. std::string s;
  7. std::cin &gt;&gt; s;
  8. for (auto const&amp; ch : s) {
  9. std::copy(s.c_str(), (&amp;ch + 1),
  10. std::ostream_iterator&lt;decltype(ch)&gt;(std::cout));
  11. std::cout &lt;&lt; &#39;\n&#39;;
  12. }
  13. }

答案2

得分: 2

Here's the translated code:

  1. 尽管如此,我认为对于你的学习进度来说,使用调试器自己找出问题会更好。以下是你的代码存在的问题:
  2. 对于 `i=0`(外部循环的第一次迭代),`for(k=0;k&lt;i;k++)` 不会执行,因为 `k&lt;0` 计算结果为 `false`
  3. 而且,在两个嵌套的 `for` 循环中更改运行变量 (`k`) 大多数情况下也表示有问题。
  4. 所以你想做的是:你想要创建每个可能的前缀,因此你想要创建长度为 `1` `n` `n` 个字符串。所以你的外部循环的第一个想法是正确的。但你在内部部分过于复杂了。
  5. 对于内部部分,你想要打印从索引 `0` `i` 的所有字符。
  6. ```cpp
  7. int main() {
  8. char cuvant[100];
  9. std::cin &gt;&gt; cuvant;
  10. // 循环遍历字符串的长度
  11. for (int i = 0, size = strlen(cuvant); i &lt; size; i++) {
  12. // 打印从 0 到 i 的所有字符 (k&lt;=0)
  13. for (int k = 0; k &lt;= i; k++) {
  14. std::cout &lt;&lt; cuvant[k];
  15. }
  16. // 打印换行符
  17. std::cout &lt;&lt; std::endl;
  18. }
  19. }

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

  1. int main() {
  2. std::string s;
  3. std::cin &gt;&gt; s;
  4. for (std::size_t i = 0, size = s.size(); i &lt; size; i++) {
  5. std::cout &lt;&lt; s.substr(0, i + 1) &lt;&lt; std::endl;
  6. }
  7. }
  1. <details>
  2. <summary>英文:</summary>
  3. 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:
  4. 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`.
  5. 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.
  6. 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.
  7. For the inner part, you want to print all chars from the index `0` up to `i`.

int main() {
char cuvant[100];

  1. std::cin &gt;&gt; cuvant;
  2. // loop over the length of the string
  3. for (int i = 0, size = strlen(cuvant); i &lt; size; i++) {
  4. // print all chars from 0 upto to i (k&lt;=0)
  5. for (int k = 0; k &lt;= i; k++) {
  6. std::cout &lt;&lt; cuvant[k];
  7. }
  8. // print a new line after that
  9. std::cout &lt;&lt; std::endl;
  10. }

}

  1. 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;
}
}

  1. </details>
  2. # 答案3
  3. **得分**: 0
  4. 以下是翻译好的部分:
  5. 对于这个非常简单的字符串后缀任务,您可以使用以下代码:
  6. ```cpp
  7. void main()
  8. {
  9. std::string s = "house";
  10. std::string s2;
  11. for(char c : s)
  12. {
  13. s2 += c;
  14. cout << s2 << endl;
  15. }
  16. }

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

英文:

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

  1. void main()
  2. {
  3. std::string s = &quot;house&quot;;
  4. std::string s2;
  5. for(char c : s)
  6. {
  7. s2 += c;
  8. cout &lt;&lt; s2 &lt;&lt; endl;
  9. }
  10. }

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

答案4

得分: -2

以下是您的代码翻译:

  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. char cuvant[100];
  6. int i,k;
  7. cin >> cuvant;
  8. for(i=0; i<strlen(cuvant); i++)
  9. {
  10. for (k = 0; k <= i; ++k)
  11. {
  12. cout << cuvant[k];
  13. }
  14. cout << endl;
  15. }
  16. }
英文:

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

  1. #include &lt;iostream&gt;
  2. using namespace std;
  3. int main()
  4. {
  5. char cuvant[100];
  6. int i,k;
  7. cin&gt;&gt;cuvant;
  8. for(i=0;i&lt;strlen(cuvant);i++)
  9. {
  10. for (k = 0; k &lt;= i; ++k)
  11. {
  12. cout&lt;&lt;cuvant[k];
  13. }
  14. cout&lt;&lt;endl;
  15. }
  16. }

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:

确定