Can anyone guide me in solving this problem? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can anyone guide me in solving this problem?

Write a program that reads text from the input, prints all matches to the following pattern (1) <= n in the output: a𝑏 𝑏…...𝑏c ⏟ n repetition ( b repeated n times) Input example: abbbcabababxyz abaxavabaabcabbc Output example: abbbbc abc abbc

22nd Nov 2020, 12:41 PM
Faezeh Alinejad
Faezeh Alinejad - avatar
9 Answers
+ 2
#include <iostream> using namespace std; void printpatt(string); int main() { string str1,str2; getline(cin,str1); getline(cin,str2); printpatt(str1); printpatt(str2); return 0; } void printpatt(string str) { int i=0; while(i<(str.length()-1)) { string temp=""; if(str[i]=='a' && str[i+1]=='b') { temp+=str[i]; temp+=str[i+1]; i+=2; while(str[i]=='b' && i<str.length()) { temp+=str[i]; i++; } if(i<str.length()) if(str[i]=='c') { temp+=str[i]; cout<<temp<<"\n"; } } i++; } } //------------------------------------------------------------ //try this
23rd Nov 2020, 2:54 AM
Manish Yadav
Manish Yadav - avatar
+ 2
Good question. In that case, we can use a linked list. Declare a constant term which when entered as input, we will stop taking input, we can implement this using while loop, and break out of loop when the stopping constant is entered by the user. By doing this we create a linked list of all inputs, then we just traverse through the list and check for the substrings. Hope this clears you doubt.
27th Nov 2020, 2:15 PM
Manish Yadav
Manish Yadav - avatar
+ 1
Manish Yadav thank you very much for your help,that's a big help to me!Now a question! If I want to make the same program for multiple inputs on multiple lines whose The number of input lines is not given in advance.What changes should I make to it?
27th Nov 2020, 1:22 PM
Faezeh Alinejad
Faezeh Alinejad - avatar
+ 1
May I see that problem? Please post the exact problem statement. How does the computer know when to stop taking input? It's either a specific character/constant is given to type when user don't want to input more or the input will be timed i.e. if user doesn't enter input in given time frame the program will automatically stop taking input.
28th Nov 2020, 12:39 AM
Manish Yadav
Manish Yadav - avatar
0
how many input strings are allowed?
22nd Nov 2020, 4:22 PM
Manish Yadav
Manish Yadav - avatar
0
Manish Yadav two string
22nd Nov 2020, 5:21 PM
Faezeh Alinejad
Faezeh Alinejad - avatar
0
Manish Yadav How to solve this problem without entering a specified constant from the input? The problem is that this constant is not necessarily entered by the input.
27th Nov 2020, 6:01 PM
Faezeh Alinejad
Faezeh Alinejad - avatar
0
Manish Yadav Suppose, for example, a user wants to enter three lines After entering the third line, hit enter Then ctrl + z and enter again Here the program must understand that it must start processing So how do we understand each other? while (getline (cin, line)); When eof is entered, the end of the line is actually poured into the getline (which is then poured into the line) and the phrase inside while while becomes wrong (becomes zero) and the iteration loop ends. I wrote the following code to implement it in main, but apparently there is a problem in implementing it, because nothing is displayed in the output! Do you know what the problem is? Int main() { string a[20]; int i=0; while (getline(cin,a[i]))i++; for(int j=0;j<i;j++) { printpatt(a[j]); } return 0; }
28th Nov 2020, 7:23 PM
Faezeh Alinejad
Faezeh Alinejad - avatar
0
Manish Yadav There seems to be a problem with your program. It does not accept all input values.
29th Nov 2020, 5:16 AM
Faezeh Alinejad
Faezeh Alinejad - avatar