C 使用for循环以反向打印字符串

C++ Printing a string in reverse using a for loop

本文关键字:打印 字符串 使用 for 循环      更新时间:2023-10-16

我有一个程序,可以使用for-loop打印字符串的字符。它还必须反向打印相同的字符,这是我遇到问题的地方。有人可以帮我弄清楚为什么第二循环不执行?

int main()
{
    string myAnimal;
    cout << "Please enter the name of your favorite animal.n";
    cin >> myAnimal;
    // This loop works fine
    int i;
    for(i = 0; i < myAnimal.length(); i++){
        cout << myAnimal.at(i) << endl;
    }
    // This one isn't executing
    for(i = myAnimal.length(); i > -1; i--){
        cout << myAnimal.at(i) << endl;
    }
    return 0;
}

您最初需要将我分配给长度为sionus One或数组中的最后一个索引值。

for(i = myAnimal.length()-1; i >= 0; i--){
    cout << myAnimal.at(i) << endl;
}

,因为字符位置在0开始,因此myAnimal的最后一个字符在位置(myAnimal.length()-1)不是myAnimal.length(),因此您想在此处启动第二个循环。

您可以使用反向迭代器

#include <iostream>
#include <string>
int main() {
  std::string myAnimal;
  std::cout << "Please enter the name of your favorite animal.n";
  std::cin >> myAnimal;
  // Iterate in reverse order
  for(auto c = myAnimal.rbegin(); c != myAnimal.rend(); ++c) {
    std::cout << *c << std::endl;
  }
}

请注意,您必须增加变量'c'(而不是降低),因为这是一个反向迭代器。

@lokno已经为您提供了正确的答案。但是,让我更多地将您的代码挑战以向您展示其他替代方案并纠正一些小错误。

首先,您实际上没有发布一个编译示例,因为您忘了显示随附的标题<iostream><string>,并且也没有显示代码中隐含的using namespace std;

第二,对于常规for循环,除非您实际需要将其用作返回值,否则更希望将循环变量保留在循环中。另外,更喜欢提前 ++i而不是提交后i++。此外,由于您确定了正确的循环索引,因此没有理由在未检查的[]版本上使用界限检查的元素访问at()

在C 11中,您具有范围循环,它允许更短,更宽写的代码,我还使用了auto,您可以在其中使用char。不幸的是,没有反向范围的循环。如果您使用i >= 0而不是i > -1,则正确的基于索引的反向可能更容易阅读。

然后,使用std::copy有一个基于算法的环路,其中您使用std::string的迭代界面(特别是反向迭代器rbegin()rend())通过绑定到标准输出的ostream_iterator来复制每个字符。

顺便说一句,我使用了分隔符"|",而不是新线,以更容易地看到您的口味。无论如何,使用std::endl可以具有性能含义,因为它每次都会冲洗输出缓冲区。

#include <algorithm>
#include <iterator>
#include <iostream> // you forgot this
#include <string>   // you forgot this
int main()
{
    using namespace std;    // you forgot this
    // let's pretend this is the string
    string myAnimal = "Please enter the name of your favorite animal.";
    // keep the loop variable local, prefer pre-increment
    for (int i = 0; i < myAnimal.length(); ++i)
        cout << myAnimal[i] << "|";    // prefer [] over at()
    std::cout << "n";
    // C++11 range-for
    for (auto c : myAnimal)
        std::cout << c << "|";
    std::cout << "n";
    // index-based reverse loop
    for (int i = myAnimal.length() - 1; i >= 0; --i)
        cout << myAnimal[i] << "|";
    std::cout << "n";
    // algorithm-based reverse loop
    std::copy(myAnimal.rbegin(), myAnimal.rend(), ostream_iterator<char>(cout, "|"));
    std::cout << "n";
    // main implicitly return 0
}

实时示例。PS:main()成功返回0

for(myAnimal.length(); i > -1; i--){
    ^

这无能为力。您获取值然后扔掉。

您的意思是i = myAnimal.length() - 1

而不是

int i;
for(i = 0; i < myAnimal.length(); i++){
    cout << myAnimal.at(i) << endl;
}
// This one isn't executing
for(i = myAnimal.length(); i > -1; i--){
    cout << myAnimal.at(i) << endl;
}

for ( string::size_type i = 0; i < myAnimal.length(); i++ ){
    cout << myAnimal.at(i) << endl;
}
// This one isn't executing
for ( string::size_type i = myAnimal.length(); i != 0; ){
    cout << myAnimal.at( --i) << endl;
}

在您的代码中,您尝试访问超出可接受范围的字符串元素,等于[0,length() - 1]

另外,而不是类型int,最好使用std :: String为返回类型的返回类型的成员函数长度的返回类型,即std :: string :: size_type。