
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to Generate a Sequence of N Characters for a Given Specific Case
This is a C++ program to generate a sequence of N characters for a given specific case.
Algorithms
Begin function GenerateSequence() generate a Sequence of N Characters for a Given Specific Case: Use rand() for generating random indexes. Store the first character directly into the sequence. If that sequence is used earlier, then it discards that and generates random index again. End
Example
#include<iostream> #include<stdlib.h> #include<string.h> using namespace std; void GenerateSequence(char string[], int n, int l, char *sequence) { int i, j=0, k,in; for(i = 0; i < n; i++) { //store first character directly into sequence if(j == 0) sequence[j++] = string[rand()%l]; else { h: in = rand()%l; for(k = 0; k < j; k++) { if(string[in] == sequence[k]) goto h; } sequence[j++] = string[in]; } } sequence[j] = '\0'; //end the sequence with null character } int main() { int n, m, l, i; char string[100]; cout<<"Enter the original string: "; cin>>string; 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; l = strlen(string); for(i = 0; i < n; i++) { char sequence[m]; GenerateSequence(string, m, l, sequence); cout<<"\nSequence "<<i+1<<": "<<sequence; } return 0; }
Output
Enter the original string: tutorialspoint Enter the number of strings to be generated from the Base string: 7 Enter the length of each string to be generated: 6 Sequence 1: tuanol Sequence 2: itlurp Sequence 3: tonaiu Sequence 4: untlri Sequence 5: liorpt Sequence 6: liusto Sequence 7: luisot
- Related Articles
- C++ Program to Generate a Graph for a Given Fixed Degree Sequence
- C++ Program to Solve a Matching Problem for a Given Specific Case
- Generate a sequence determined by the characters of a given string
- Program for converting Alternate characters of a string to Upper Case.\n
- C++ Program to Generate Randomized Sequence of Given Range of Numbers
- C++ Program to Perform integer Partition for a Specific Case
- C Program to delete n characters in a given string
- C++ Program to Generate Random Partition out of a Given Set of Numbers or Characters
- C++ Program to Implement a Binary Search Algorithm for a Specific Search Sequence
- C# program to count upper and lower case characters in a given string
- C++ Program to Generate a Random UnDirected Graph for a Given Number of Edges
- Program to find last digit of the given sequence for given n in Python
- Java program to count upper and lower case characters in a given string
- C++ Program to Generate a Random Directed Acyclic Graph DAC for a Given Number of Edges
- Java Program for Case Specific Sorting

Advertisements