Wednesday, 22 November 2017

C++ Program to Generate a Sequence of N Characters for a Given Specific Case


Code:

#include   iostream
#include   stdlib.h
#include   string.h

using namespace std;

// A function to generate a random sequence of the given length from a given string.
void GenSequence(char str[], int m, int len, char *seq)
{
int i, j=0, k, index;
for(i = 0; i < m; i++)
{
// Use rand() to generate random indexes.
// Store the very first character directly into the sequence.
if(j == 0)
seq[j++] = str[rand()%len];
// Check for the repetitions.
else
{
h:
index = rand()%len;
for(k = 0; k < j; k++)
{
if(str[index] == seq[k])
goto h;
}
seq[j++] = str[index];
}
}
// End the sequence with null character.
seq[j] = '\0';
}

int main()
{
int n, m, len, i;
char str[100];
cout<<"Enter the base string: ";
cin>>str;

cout<<"\nEnter the number of strings to be generated from the Base string: ";
cin>>n;

cout<<"\nEnter the length of each string to be generated: ";
cin>>m;

len = strlen(str);

// Print the generated sequence.
for(i = 0; i < n; i++)
{
char seq[m];
GenSequence(str, m, len, seq);
cout<<"\nSequence "<
}

return 0;
}


Output:

Case 1:
Enter the base string: abcedfghijklmnopq

Enter the number of strings to be generated from the Base string: 5

Enter the length of each string to be generated: 6

Sequence 1: hfkoqe
Sequence 2: qabkio
Sequence 3: qpeing
Sequence 4: bjaegn
Sequence 5: qpfham

Case 2:
Enter the base string: aabbccddeeffgghhiijj

Enter the number of strings to be generated from the Base string: 5

Enter the length of each string to be generated: 8

Sequence 1: ahaecjbc
Sequence 2: cdfhbicg
Sequence 3: gaijhddf
Sequence 4: jgdjhhbf
Sequence 5: bcafedbi



More C++ Programs:















100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...