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

Write a C++ program that reads a string from the standard string of number of A\

ID: 3821323 • Letter: W

Question

Write a C++ program that reads a string from the standard string of number of A's followed by the same number of 'B's stack. For example, if the user enters the following string: "AABB" then the output will be true. If the user enters "AAABB" or "ABB" then the output will be false. You can assume that the user will always enter valid strings and all the characters are uppercase.

write a program that reads a string from the standard string of number of A's followed by the same number of 'B's stack. For example, if the user enters the following string: "AABB" then the output will be true. If the user enters "AAABB" or "ABB" then the output will be false. You can assume that the user will always enter valid strings and all the characters are uppercase. points)

Explanation / Answer

#include <iostream>

using namespace std;
bool checkStrings(string s1) {
if(s1.length () % 2 == 0) {
int countA = 0;
int countB = 0;
bool isA = true;
for(int i=0; i<s1.length(); i++){
if(s1[i] == 'A' && isA){
countA++;
}
else if(s1[i] == 'B'){
isA=false;
countB++;
}
else{
return false;
}
}
if(countA == countB){
return true;
}
else{
return false;
}
}
else {
return false;   
}
}
int main()
{
string s1;
cout << "Enter the first string: ";
cin >> s1;
cout<<checkStrings(s1)<<endl;
return 0;
}

Output:

sh-4.2$ g++ -o main *.cpp                                                                                                                                                                                                                                              

sh-4.2$ main                                                                                                                                                                                                                                                           

Enter the first string: AABB                                                                                                                                                                                                                                           

1                                                                                                                                                                                                                                                                      

sh-4.2$ main                                                                                                                                                                                                                                                           

Enter the first string: AAABB                                                                                                                                                                                                                                          

0                                                                                                                                                                                                                                                                      

sh-4.2$ main                                                                                                                                                                                                                                                           

Enter the first string: ABB                                                                                                                                                                                                                                            

0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote