In: Computer Science
C++ Write a program that lets the user enter a two letters which are f and s with a length of 5. And outputs how many times it was occurred and lists the 2 most repeating pattern with 5lengths of f and s. The output display should always start at three-f(s) .Include an option where user can retry the program.
Example:
Input: sssfsfsfssssfffsfsssssfffsffffsfsfssffffsfsfsfssssfffffsffffffffffffssssssssfffsffffsssfsfsfsfssssfffsssfsfsffffffssssssffffsssfsfsfsss
Output: The most repeating 5lengths of pattern is: fffsf and occurred 6times.
Output2: The second most repeating 5lengths of pattern is: fffss and occurred 5times.
#include <iostream>
using namespace std;
int main()
{
while(1)
{
cout<<"Enter the word to continue or q to quit: ";//taking
the choice from the user
string s;
cin>>s;
if(s.compare("q")==0)//until the user enters q the program will
repeat
{
break;
}
int max1=0,max2=0;
string result1,result2;
for(int p=0;p<s.length();p++)
{
string fs;
int c=0;
if(s[p]=='f' && s[p+1]=='f' && s[p+2]=='f'
&& s[p+3]=='s' && s[p+4])//generating the
substrings
{
fs=s.substr(p,5);
}
for (int i=0;i<=s.length()-5;i++) //to find the number of times
the given substring occured
{
int k;
for (k=0;k<5;k++)
{if (s[i+k]!=fs[k]) //if the characters are not equal
{break;}
}
if (k==5) //if the substring available in the string
incrementing the counter
{
c++;
k=0;
}
}
if(c>max1)//if the string occured count is greater than the
previous string
{
max2=max1;
max1=c;
result2=result1;
result1 = fs;
}
else if(c>max2 && c!= max1)
{
max2=c;
result2=fs;
}
}
cout<<"The first most repeating 5 lengths of pattern is:
"<<result1<<" and occurred "<<max1<<"
times\n";//printing the results
cout<<"The second most repeating 5 lengths of pattern is:
"<<result2<<" and occurred "<<max2<<"
times\n";//printing the results
}
}