Remove " b " and " ac " from a given string | A C ++ program to remove " b " and ' ac ' from input string ; The main function that removes occurrences of " a " and " bc " in input string ; previous character ; current character ; check if current and next character forms ac ; if current character is b ; if current char is ' c ▁ & & ▁ last ▁ char ▁ in ▁ output ▁ ▁ is ▁ ' a ' so delete both  ; else copy curr char to output string ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void stringFilter ( char * str ) { int n = strlen ( str ) ; int i = -1 ; int j = 0 ; while ( j < n ) { if ( j < n - 1 && str [ j ] == ' a ' && str [ j + 1 ] == ' c ' ) j += 2 ; else if ( str [ j ] == ' b ' ) j ++ ; else if ( i >= 0 && str [ j ] == ' c ' && str [ i ] == ' a ' ) i -- , j ++ ; else str [ ++ i ] = str [ j ++ ] ; } str [ ++ i ] = ' \0' ; } int main ( ) { char str1 [ ] = " ad " ; cout << " Input ▁ = > ▁ " << str1 << " Output = > " stringFilter ( str1 ) ; cout << str1 << endl << endl ; char str2 [ ] = " acbac " ; cout << " Input ▁ = > ▁ " << str2 << " Output = > " stringFilter ( str2 ) ; cout << str2 << endl << endl ; char str3 [ ] = " aaac " ; cout << " Input ▁ = > ▁ " << str3 << " Output = > " stringFilter ( str3 ) ; cout << str3 << endl << endl ; char str4 [ ] = " react " ; cout << " Input ▁ = > ▁ " << str4 << " Output = > " stringFilter ( str4 ) ; cout << str4 << endl << endl ; char str5 [ ] = " aa " ; cout << " Input ▁ = > ▁ " << str5 << " Output = > " stringFilter ( str5 ) ; cout << str5 << endl << endl ; char str6 [ ] = " ababaac " ; cout << " Input ▁ = > ▁ " << str6 << " Output = > " stringFilter ( str6 ) ; cout << str6 << endl << endl ; char str [ ] = " abc " ; cout << " Input ▁ = > ▁ " << str << " Output = > " stringFilter ( str ) ; cout << str << endl << endl ; return 0 ; }