C 使用for循环以反向打印字符串
C++ Printing a string in reverse using a for loop
我有一个程序,可以使用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。
- C++ |<<运算符重载和打印字符串
- 反向打印字符串
- C++ 用于在数组中打印字符串的随机数
- C 不打印字符串
- 如何在没有同步的情况下使用多个线程(2、4,8、16 个线程)在循环(10,100、1000 个周期)中打印字符串?
- 尝试打印字符串时"Python Exception <class 'gdb.error'> There is no member named _M_dataplus."
- 使用带有 std::cout 的单引号打印字符串实际上打印了数字
- 使用迭代从链表向后打印字符串
- 如何打印字符串而不是数字
- 使用 for 循环打印字符串数组无法正常工作 (C++)
- C++ 打印字符串的内容
- 如何在空格后打印字符串的其余部分
- 如何在C 中打印字符串向量的向量
- 当我打印字符串时,我不知道它是什么输出
- 尝试以随机字符(长读取)打印字符串返回结果
- 从左到右打印字符串的组合(不是排列)c++
- cout 不能一致地打印字符串和变量值,导致输出未对齐
- C++ cout 在擦除换行符后打印字符串不正确
- 从对象数组打印字符串对象
- C++ 粘贴值和打印字符串