I wrote this code at the bottom and I want it to insert a random string r into my string str and multiple set intervals or at multiple random intervals. It is very clunky and I will need to increase it to say 1000 chars otherwise it won't really fix my problem. Is there anyone who can help me to put this into a loop? I want to use the length of my string to determine the size of the loop and a set 15 char increase in my random insert function. I have tried a loop and it was unsuccessful.
int main() {
string str;
int len;
int randNum;
char x;
char x1;
char x2;
char x3;
char x4;
char x5;
char x6;
string str1;
string str2;
string str3;
string str4;
string str5;
string str6;
str = "This is just a random string of text! I have extended it just to test my code.";
//works great
char letters[] = "abcdefghijklmnopqrstuvwxyz";
srand(time(0));
len = str.length();
randNum = len/2;
cout << str << endl;
cout << len << endl;
cout << randNum << endl;
if (randNum >= 100){ // Need help with using a loop for this if function
char x = letters[rand() % 26];
char y = letters[rand() % 26];
char z = letters[rand() % 26];
string r;
r = ".";
r += x;
r += y;
r += z;
str.insert(randNum % 10 + 100, r);
str1 = str;
}
else{
str1 = str;
}
if (randNum >= 85){
char x1 = letters[rand() % 26];
char y1 = letters[rand() % 26];
char z1 = letters[rand() % 26];
string r2;
r2 = ".";
r2 += x1;
r2 += y1;
r2 += z1;
str1.insert(randNum % 10 + 85,r2);
str2 = str1;
}
else{
str2 = str1;
}
if (randNum >= 60){
char x2 = letters[rand() % 26];
char y2 = letters[rand() % 26];
char z2 = letters[rand() % 26];
string r3;
r3 = ".";
r3 += x2;
r3 += y2;
r3 += z2;
str2.insert(randNum % 10 + 60,r3);
str3 = str2;
}
else{
str3 = str2;
}
if (randNum >= 45){
char x3 = letters[rand() % 26];
char y3 = letters[rand() % 26];
char z3 = letters[rand() % 26];
string r4;
r4 = ".";
r4 += x3;
r4 += y3;
r4 += z3;
str3.insert(randNum % 10 + 45,r4);
str4 = str3;
}
else{
str4 = str3;
}
if (randNum >= 30){
char x4 = letters[rand() % 26];
char y4 = letters[rand() % 26];
char z4 = letters[rand() % 26];
string r5;
r5 = ".";
r5 += x4;
r5 += y4;
r5 += z4;
str4.insert(rand() % 10 + 30,r5);
str5 = str4;
}
else{
str5 = str4;
}
if (randNum >= 15){
char x5 = letters[rand() % 26];
char y5 = letters[rand() % 26];
char z5 = letters[rand() % 26];
string r6;
r6 = ".";
r6 += x5;
r6 += y5;
r6 += z5;
str5.insert(rand() % 10 + 15,r6);
str6 = str5;
}
else{
str6 = str5;
}
if (randNum >= 0){
char x6 = letters[rand() % 26];
char y6 = letters[rand() % 26];
char z6 = letters[rand() % 26];
string r7;
r7 = ".";
r7 += x6;
r7 += y6;
r7 += z6;
str6.insert(rand() % 10,r7);
}
else{
return 0;
}
cout << str6 <<endl;
Aucun commentaire:
Enregistrer un commentaire