Code:
// Read in a string and add four random letters
// Every four letters in that string
#include <iostream>
#include <string>
int GetRand(int min, int max);
using namespace std;
int main()
{
char array[100] = "Hello everyone this is my string unblemished";
char letters[30]= "abcdefghijklmnopqrstuvwxyz";
int size = strlen(array);
for (int i=0; i<size; i++)
{
if(i%4==0)
{
for (int j=0; j<4; j++)
{
int r;
r = GetRand(0, 25);
cout<<letters[r];
}
}
cout<<array[i];
}
cin.get();
return 0;
}
/*=============================
Random function definition
=============================*/
int GetRand(int min, int max)
{
static int Init = 0;
int rc;
if (Init == 0)
{
/*
* As Init is static, it will remember it's value between
* function calls. We only want srand() run once, so this
* is a simple way to ensure that happens.
*/
srand(time(NULL));
Init = 1;
}
/*
* Formula:
* rand() % N <- To get a number between 0 - N-1
* Then add the result to min, giving you
* a random number between min - max.
*/
rc = (rand() % (max - min + 1) + min);
return (rc);
}
This is c++ but you get the idea. I hope this isn't h/w btw