Remove “b” and “ac” from a given string

PROBLEM :

Given a string, eliminate all “b” and “ac” in the string, replace them in-place and iterate over the string once.

Input:
The first line of input contains an integer T denoting the number of test cases. The description of T test cases follows.
Each test case contains a string of length N.

Output:
Print the resultant substring after removing 'b' and 'ac' from string.

Constraints:
1 = T = 200
1 = N = 200

Example:
Input:
2
acbac
aababc

Output:

aaac
     
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

#include<iostream>
using namespace std;
#include<string.h>
#include<malloc.h>
char* Remove_b_ac(char*) ;
int main()
{
int t ;
char *str;
str=(char*)malloc(100*sizeof(char)) ;
cin>>t ;
while(t--)
{
   cin>>str ;

   str=Remove_b_ac(str) ;
   cout<<str<<endl ;
}
return 0;
}

char* Remove_b_ac(char *str)
{
    int l,i,j ;
    l=strlen(str) ;
    char *strn ;
    strn=(char*)malloc(100*sizeof(char)) ;
    j=0 ;
   
    for(i=0;i<l;i++)
    {
        if(str[i]=='b')
            continue ;
        if(str[i]=='a'&&str[i+1]=='c')
        {
            i++ ;
            continue ;
        }
       
        strn[j]=str[i] ;
        j++ ;
    }
   
    strn[j]='\0' ;
    return strn ;
}

---------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )