在 C++ 中将字符存储到字符串中

发布于 2024-10-18 05:59:45 字数 767 浏览 3 评论 0原文

所以现在我有这段代码,可以按照用户输入确定的设定增量生成随机字母。

#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

int sLength = 0;
static const char alphanum[] =
"0123456789"
"!@#$%^&*"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";

int stringLength = sizeof(alphanum) - 1;

char genRandom()
{
    return alphanum[rand() % stringLength];
}

int main()
{
    cout << "What is the length of the string you wish to match?" << endl;
    cin >> sLength;
    while(true)
    {
        for (int x = 0; x < sLength; x++)
        {
            cout << genRandom();
        }
        cout << endl;
    }

}

我正在寻找一种方法将第一个(用户定义的数量)字符存储到一个字符串中,我可以用它与另一个字符串进行比较。任何帮助将不胜感激。

So right now I have this code that generates random letters in set increments determined by user input.

#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

int sLength = 0;
static const char alphanum[] =
"0123456789"
"!@#$%^&*"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";

int stringLength = sizeof(alphanum) - 1;

char genRandom()
{
    return alphanum[rand() % stringLength];
}

int main()
{
    cout << "What is the length of the string you wish to match?" << endl;
    cin >> sLength;
    while(true)
    {
        for (int x = 0; x < sLength; x++)
        {
            cout << genRandom();
        }
        cout << endl;
    }

}

I'm looking for a way to store the first (user defined amount) of chars into a string that I can use to compare against another string. Any help would be much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

贱贱哒 2024-10-25 05:59:45

只需

string s(sLength, ' ');

while (true) 之前添加,在循环中更改

cout << genRandom();

s[x] = genRandom();

,然后删除 cout << endl; 语句。这将通过将字符放入 s 来替换所有打印。

Just add

string s(sLength, ' ');

before while (true), change

cout << genRandom();

to

s[x] = genRandom();

in your loop, and remove the cout << endl; statement. That will replace all of the printing by putting the characters into s.

ˇ宁静的妩媚 2024-10-25 05:59:45

嗯,这个怎么样?

    std::string s;
    for (int x = 0; x < sLength; x++)
    {
        s.push_back(genRandom());
    }

Well, how about this?

    std::string s;
    for (int x = 0; x < sLength; x++)
    {
        s.push_back(genRandom());
    }
離人涙 2024-10-25 05:59:45
#include<algorithm>
#include<string>
// ...

int main()
{
    srand(time(0));  // forget me not
    while(true) {
        cout << "What is the length of the string you wish to match?" << endl;
        cin >> sLength;
        string r(sLength, ' ');
        generate(r.begin(), r.end(), genRandom);
        cout << r << endl;
    }

}
#include<algorithm>
#include<string>
// ...

int main()
{
    srand(time(0));  // forget me not
    while(true) {
        cout << "What is the length of the string you wish to match?" << endl;
        cin >> sLength;
        string r(sLength, ' ');
        generate(r.begin(), r.end(), genRandom);
        cout << r << endl;
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文