我如何生成一个随机的小写字符串的长度的用户输入的数字
how would i generate a random lowercase string the length of the users entered number
你好,我试图写一个函数,将生成小写字母的随机字符串。随机字符串的长度将是用户输入的数字。到目前为止我已经讲了这么多,但我相信我把事情复杂化了
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <string>
using namespace std;
char randString(){
int number;
str::string Str; //str has not been declared error
for(unsigned int i = 0; i <8; i++){
Str += randString(); //str was not declared in this scope error
}
cout << Str << endl; // str was not declared in this scope error
}
int main() {
char c;
int number;
srand (time(0));
cout << "Enter a number.n"
"That number will generate a random string of lower case letters the length of the number" << endl;
cin >> number;
for (int i=0; i < number; i++){
number = rand() % 26;
c = 'a' + number;
cout << randString();
}
return 0;
}
您更改了for循环内的变量number
的值,该变量也是循环条件变量。这将导致一个未定义的行为,因为每次执行语句number = rand() % 26;
时,number
的值都会改变。然而,从我对你的问题陈述的理解来看,我认为这就是你想要达到的目标:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
int number;
char c;
srand(time(NULL));
cout << "Enter a number.n"
"That number will generate a random string of lower case letters the length of the number" << endl;
cin >> number;
for(int i=0;i<number;i++)
{
c = 'a' + rand()%26;
cout << c;
}
return 0;
}
希望这对你有帮助。好运!
相关文章:
- 如何检查一个c++字符串中有多少相同的字符/数字
- 在 c++ 中查找字符串中没有循环的数字总和
- 将数字转换为填充字符串
- 递归形成字符串中所有数字字符的中间和?
- std::regex:匹配由数字和空格组成的字符串,并提取数字.如何?
- C++ python 中的函数返回数字而不是字符串
- C++:从文件中读取字符串和整数,并获得最大数字
- 以C++格式将字符串转换为数字
- C++关于数字的字符串数组
- 使用非托管导出将字符串从 C# 返回到C++将返回数字
- 我想使用 std::stoi 函数在 cpp 中将字符串转换为整数,因为我想在字符串中找到不同数字的总和(在下级酶中)
- 将大数字(10-12 位数字)存储在无序映射中<字符串,整数>
- 用C++将浮点数字转换为本地化字符串
- 如何在c++中将字符串中的所有数字保存到多维数组中
- 列出所有 N 位数字,并带有 [字符串] 数字
- 如何将字符串数字转换为各个数字的整数数组
- 如何将字符串/数字排序添加到 QSortFilterProxyModel 派生类
- 将std::字符串数字转换为十六进制数字
- 在c++ /Qt中拆分Lua函数调用的混合字符串/数字参数列表
- 获取字符串数字的精度