Question

In: Computer Science

C++ Write a program that lets the user enter a two letters which are f and...

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.

Solutions

Expert Solution

#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
  
  
}

  
}


Related Solutions

Write a C++ function that lets the user enter alphabet letters into a static char array...
Write a C++ function that lets the user enter alphabet letters into a static char array until either the user enters a non-alphabet letter or, it has reached the MAXSIZE. You can use the isalpha([Char]) function to check if the input is an alphabet letter or not. void fillArray (char ar[], size_t& size){ // this is the function prototype }
How to write a C++ program that lets the user enter a string and checks if...
How to write a C++ program that lets the user enter a string and checks if it is an accepted polynomial. Accepted polynomials need to have one term per degree, no parentheses, spaces ignored.
Write a C++ program that lets the user enter the total rainfall for each of 12...
Write a C++ program that lets the user enter the total rainfall for each of 12 months (starting with January) into an array of doubles. The program should calculate and display (in this order): the total rainfall for the year,     the average monthly rainfall,     and the months with the highest and lowest amounts. Months should be expressed as English names for months in the Gregorian calendar, i.e.: January, February, March, April, May, June, July, August, September, October, November,...
Write a program in C that lets the user enter a message using SMS Language(e.g. lol,...
Write a program in C that lets the user enter a message using SMS Language(e.g. lol, omg, omw etc.), then translates it into English (e.g. laughing out loud, oh my god, on my way etc.). Also provide a mechanism to translate text written in English into SMS Language. needs to be able to translate at least 10 sms words
Time Calculator Create a C++ program that lets the user enter a number of seconds and...
Time Calculator Create a C++ program that lets the user enter a number of seconds and produces output according to the following criteria: • There are 60 seconds in a minute. If the number of seconds entered by the user is greater than or equal to 60, the program should display the number of minutes in that many seconds. • There are 3,600 seconds in an hour. If the number of seconds entered by the user is greater than or...
IN C This assignment is to write a program that will prompt the user to enter...
IN C This assignment is to write a program that will prompt the user to enter a character, e.g., a percent sign (%), and then the number of percent signs (%) they want on a line. Your program should first read a character from the keyboard, excluding whitespaces; and then print a message indicating that the number must be in the range 1 to 79 (including both ends) if the user enters a number outside of that range. Your program...
Write a C++ program which prompts the user to enter an integer value, stores it into...
Write a C++ program which prompts the user to enter an integer value, stores it into a variable called ‘num’, evaluates the following expressions and displays results on screen. num+5, num-3, (num+3) – 2, ((num+5)*2 / (num+3)) For performing addition and subtraction, you are allowed to use ONLY the increment and decrement operators (both prefixing and postfixing are allowed). You must remember that using increment/decrement operators changes the original value of a number. Indent your code and include comments for...
Write a C program that asks the user to enter any two integernumbers, and each...
Write a C program that asks the user to enter any two integer numbers, and each number consists of four-digits. Your program should check whether the numbers are four digits or not and in case they are not a four digit number, the program should print a message and exit, otherwise it should do the following:Print a menu as follows:Select what you want to do with the number 1-3:1- Print Greatest Common Divisor (GCD) of the two numbers.2- Print sum...
IN C++ Write a program that prompts the user to enter the number of students and...
IN C++ Write a program that prompts the user to enter the number of students and each student’s name and score, and finally displays the student with the highest score (display the student’s name and score). Also calculate the average score and indicate by how much the highest score differs from the average. Use a while loop. Sample Output Please enter the number of students: 4 Enter the student name: Ben Simmons Enter the score: 70 Enter the student name:...
Question Write a C program that asks the user to enter two integers x and n....
Question Write a C program that asks the user to enter two integers x and n. Then the program computes xn (=x * x * x …… (n times)) using for loop. and give me an output please use printf and scanf #include int main(void) {     //Declare required variables             //read two integers x , n from the keyboard                 //compute xn using for loop                     printf("< Your name >\n");...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT