(C++)我似乎不止一次在运行for循环时遇到问题.我做错什么了吗
(C++) I seem to be having trouble running my for loop more than once. Am I doing something wrong?
我已经在下面发布了这个问题和我的代码。我基本上试着运行这个循环3次,打印单词的前2个字符。我已经花了一个小时在这个问题上了。我需要知道如何修复它。
问题:编写一个C++程序,使用给定字符串的前2个字符的3个副本创建一个新字符串。如果给定字符串的长度小于2,则使用整个字符串。
样本输入:"abc";"Python";"J";
样本输出:阿巴巴布PyPyPyJJJ
代码:
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <ctype.h>
#include <algorithm>
using namespace std;
string test(string s1)
{
int x = s1.length();
string y;
if (x >= 2)
{
for (int i = 0; i < 3; ++i)
{
y = s1.substr(0, 2);
return y;
}
}
else
{
for (int j = 0; j < 3; ++i)
{
y = s1;
return y;
}
}
}
int main()
{
cout << test("abc") << endl;
cout << test("Python") << endl;
cout << test("J") << endl;
return 0;
}
这里有一个可能的解决方案:
// Example program
#include <iostream>
#include <string>
std::string test(const std::string& input) {
if (input.length() <= 2) {
return input + input + input;
} else {
std::string tmp(6, '0');
for (int i = 0; i < 3; i++) {
tmp[i*2 + 0] = input[0];
tmp[i*2 + 1] = input[1];
}
return tmp;
}
}
int main()
{
std::string name;
std::cout << test("abc") << "!n";
std::cout << test("Python") << "!n";
std::cout << test("J") << "n";
}
你可以在这个链接上看到它在运行。
以下是如何修复第一个循环
for (int i = 0; i < 3; ++i)
{
y += s1.substr(0, 2); // append two more characters to what we've got so far
}
return y;
还有其他方法可以做到这一点,但这是对代码的最小更改,希望能说明您所犯的错误。
首先,由于一些小错误,您在这里发布的程序无法编译,例如:
for (int j = 0; j < 3; ++i)
{
y=s1;
return y;
}
显然++i〃;应替换为"++j":
for (int j = 0; j < 3; ++j)
{
y=s1;
return y;
}
此外,您的程序中存在一些逻辑错误。请记住,一旦函数返回,就无法再次恢复。所以在这里你不能用一个循环来";返回3次";,这绝对是错误的。
你可以在函数中完成一个字符串,然后将其打印到屏幕上,这将很容易地解决你的问题。这是我对你的程序的更正:
#include <iostream>
#include <string>
using namespace std;
string test(string s1)
{
int x = s1.length();
string y;
if (x >= 2)
{
string res=s1.substr(0,2);
y=res+res+res;
//Given that the number 3 is very small, repeat it manually may bring a higher efficiency(though it cannot be seen exactly)
return y;
}
else
{
return s1+s1+s1;
}
}
int main()
{
//I do not think this program need so many "#include", just include what we need, that is convenient
//for readers to understand your program quickly :)
cout << test("abc") << endl;
cout << test("Python") << endl;
cout << test("J") << endl;
/*Also, I recommend that you can always test some programs with a dynamic input, so that you can find some
small mistakes more easily. Below is a simple example:
cout<<"Please input a string:";
string str;
cin>>str;
cout<<test(str)<<endl;*/
return 0;
}
希望我的答案能帮助你解决问题:(
相关文章:
- 编译包含字符串的代码时遇到问题
- 如何在C++向量中奇数元素前面加上值-1,我在使用insert函数时遇到了问题
- 在 for 循环中查找问题时遇到困难
- 代码在我的计算机上运行良好,但是在将其提交给coursera时遇到未知的信号11问题
- 在顶点着色器中使用 OpenGl 的未声明标识符,我在顶点着色器中绘制三角形时遇到问题
- 从其他类访问类时遇到问题
- 打印问题.我在数组中打印值时遇到一些问题
- 使用静态成员声明类时遇到问题
- C++ 填充数组时遇到问题
- 遇到此问题时遇到困难:允许用户输入数组的值并使用 for,而循环也输出输入的最大数字
- 我在使用C++文件时遇到了一些问题
- 在将字符串放入字符串向量时遇到问题?
- 余数除法和不允许除以零 (c++) 时遇到问题
- 我在C++中使用 getline() 函数时遇到问题
- C++线程安全:如果只有一个线程可以写入非原子变量,但多个线程从中读取. 会遇到问题吗?
- 在我的四叉树实现中遇到问题
- 在 c++ 中循环遍历数组时遇到问题
- 我遇到了黑客排名中的问题"TWO STRINGS"的三个测试用例的分段错误。原因是什么?
- 使用 MinGW 在 Windows 上构建 GLEW 时遇到问题
- 编译 Cilk Plus 程序时遇到问题