Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Show program in C++ Write a program that asks the user to input 1 sequence of ch

ID: 3774283 • Letter: S

Question

Show program in C++ Write a program that asks the user to input 1 sequence of characters. Then it will ask the user for a character to search for and will output the maximum number of times that the character occurred in the sequence. Example: Sequence 1: aabccccccccc, Sequence 2: cchhcchcaaaa, If the user chooses to search for character 'a', the program will output "Character a occurred a maximum of 4 times" because it occurred 4 times in the second sequence, and only twice in the first sequence.

Explanation / Answer

I had made the code generic. you can add as many sequence as you want. below is the code :

#include <iostream>
#include <stdio.h>

#define size 20
using namespace std;

int main() {
  
   char ch;
   int n;
   int count;
   int maxCount=0,index=0;
   cout<<" how many sequence you want to enter ? :";
   cin>>n;
  
char sequence[n][size];

   for(int i=0;i<n;i++)
   {
   cout<<" enter the sequence number "<<i+1;
   gets(sequence[i]);
   }
  
   cout<<" enter the character you want to search :";
   cin>>ch;

   for(int i=0;i<n;i++)
   {
   count=0;
   for(int j=0;j<size;j++)
   {
   if(sequence[i][j]==ch)
   {
   count++;
   }
   }
   if(count>maxCount)
   {
   maxCount=count;
   index=i+1;
   }
   }
  
   cout<<" the character '"<<ch<<"' occured a maximum of "<<maxCount<<"number of times in Sequence no."<<index;
   return 0;
}

feel free to ask if you have any doubt :)