Skip to content

Commit

Permalink
Create string_condition_recursion.cpp
Browse files Browse the repository at this point in the history
Suppose you have a string, S, made up of only 'a's and 'b's. Write a recursive function that checks if the string was generated using the following rules:
a. The string begins with an 'a'
b. Each 'a' is followed by nothing or an 'a' or "bb"
c. Each "bb" is followed by nothing or an 'a'
If all the rules are followed by the given string, return true otherwise return false.
  • Loading branch information
Sustainability4 authored Apr 18, 2021
1 parent 98b2b62 commit 6fa4694
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions string_condition_recursion.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <iostream>
using namespace std;
int array_length ( char input[]){
int i = 0;
while(input[i]){
i++;
}
return i;
}


bool check_AB(char input[], int size){
if (size == 0){
return true;
}

if (input[0] == 'a'){
if(input[1] == 'a'){
check_AB(input+1,size-1);
}else if (input[1] == 'b' && input[2] == 'b'){
check_AB(input+3, size-3);
}else if (size == 1){
return true;
}else{
return false;
}
}else{
return false;
}
}


bool checkAB(char input[]) {
// Write your code here
int size = array_length(input);
if (size == 0){
return false;
}else{
bool val = check_AB(input, size);
}
}

int main() {
char input[100];
bool ans;
cin >> input;
ans=checkAB(input);
if(ans)
cout<< "true" << endl;
else
cout<< "false" << endl;
}

0 comments on commit 6fa4694

Please sign in to comment.