C++ 中的暴力字符生成
所以我试图制作一个强力字符串生成器来匹配和比较 CUDA 中的字符串。在我开始尝试使用一门语言之前,我不知道我是否想让它在 C++ 中工作。我目前有这段代码。
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
int sLength = 0;
int count = 0;
int charReset = 0;
int stop = 0;
int maxValue = 0;
string inString = "";
static const char charSet[] = //define character set to draw from
"0123456789"
"!@#$%^&*"
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int stringLength = sizeof(charSet) - 1;
char genChars()
{
return charSet[count]; //Get character and send to genChars()
}
int main()
{
cout << "Length of string to match?" << endl;
cin >> sLength;
cout << "What string do you want to match?" << endl;
cin >> inString;
string sMatch(sLength, ' ');
while(true)
{
for (int y = 0; y < sLength; y++)
{
sMatch[y] = genChars(); //get the characters
cout << sMatch[y];
if (count == 74)
{
charReset + 1;
count = 0;
}
if (count == 2147000000)
{
count == 0;
maxValue++;
}
}
count++;
if (sMatch == inString) //check for string match
{
cout << endl;
cout << "It took " << count + (charReset * 74) + (maxValue*2147000000) << " randomly generated characters to match the strings." << endl;
cin >> stop;
}
cout << endl;
}
}
现在这段代码可以运行并编译,但它并不完全符合我的要求。它会做 4 个相同的角色,EX。 aaaa 或 1111,然后继续下一个而不像 aaab 或 1112 那样递增。我尝试过乱搞这样的事情,
for (int x = 0; x < sLength; x++)
{
return charSet[count-sLength+x];
}
在我看来应该有效,但无济于事。
So I'm trying to make a brute force string generator to match and compare strings in CUDA. Before I start trying to mess around with a language I don't know I wanted to get one working in C++. I currently have this code.
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
int sLength = 0;
int count = 0;
int charReset = 0;
int stop = 0;
int maxValue = 0;
string inString = "";
static const char charSet[] = //define character set to draw from
"0123456789"
"!@#$%^&*"
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int stringLength = sizeof(charSet) - 1;
char genChars()
{
return charSet[count]; //Get character and send to genChars()
}
int main()
{
cout << "Length of string to match?" << endl;
cin >> sLength;
cout << "What string do you want to match?" << endl;
cin >> inString;
string sMatch(sLength, ' ');
while(true)
{
for (int y = 0; y < sLength; y++)
{
sMatch[y] = genChars(); //get the characters
cout << sMatch[y];
if (count == 74)
{
charReset + 1;
count = 0;
}
if (count == 2147000000)
{
count == 0;
maxValue++;
}
}
count++;
if (sMatch == inString) //check for string match
{
cout << endl;
cout << "It took " << count + (charReset * 74) + (maxValue*2147000000) << " randomly generated characters to match the strings." << endl;
cin >> stop;
}
cout << endl;
}
}
Now this code runs and compiles but it doesn't exactly do what I want it to. It will do 4 of the same character, EX. aaaa or 1111 and then go onto the next without incrementing like aaab or 1112. I've tried messing around with things like this
for (int x = 0; x < sLength; x++)
{
return charSet[count-sLength+x];
}
Which in my mind should work but to no avail.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您基本上只需要增加一个计数器,然后将计数转换为基数(字符数组的大小)。
下面是一个示例,它执行以 16 为基数的普通数字。
http://www.daniweb.com/code/snippet217243.html
您应该能够替换
为您的字符集并从那里找出它。这可能无法使用 uint 生成足够大的字符串,但您应该能够从那里将其分成块。
假设您的字符数组是“BAR”,因此您需要使用自己的符号而不是 0 1 和 2 转换为以 3 为基数的数字。
它的作用是执行模数来确定字符,然后除以基数,直到数变为零。相反,您要做的是重复“B”直到达到字符串长度,而不是在达到零时停止。
例如:由数字 13 生成的四个字符的字符串:
最终输出:BAAR
对于可以生成更大字符串的方法,您可以使用字符串大小的整数数组(称为位置),将所有整数初始化为零并执行类似的操作每次迭代时都会这样做:
然后您将遍历整个数组,并使用 charSet[positions[i]] 构建字符串以确定每个字符是什么。
You basically just need to increment a counter, than convert the count number to base (size of char array)
Here's an example which does normal numbers up to base 16.
http://www.daniweb.com/code/snippet217243.html
You should be able to replace
with your set of characters and figure it out from there. This might not generate a large enough string using a uint, but you should be able to break it up into chunks from there.
Imagine your character array was "BAR", so you would want to convert to a base 3 number using your own symbols instead of 0 1 and 2.
What this does is perform a modulus to determine the character, then divide by the base until the number becomes zero. What you would do instead is repeat 'B' until your string length was reached instead of stopping when you hit zero.
Eg: A four character string generated from the number 13:
Final output: BAAR
For an approach which could generate much larger strings, you could use an array of ints the size of your string, (call it
positions
), initialize all the ints to zero and do something like this on each iteration:Then you would go through the whole array, and build the string up using charSet[positions[i]] to determine what each character is.