if i input a##s…it is removing only 1 # character…not the both…why?
String code doubt
since u use the erase function so the next ‘#’ is shifted a position back so the next element encountered becomes ‘s’ instead of ‘#’
so but if i write a###s then the code in not running…
Ohh Sorry…now i get it…i have corrected my code…thanks!
okay great the code was running for triple #
no… i did i–; after every time the if statement is executed…so it was working
#include
using namespace std;
int main()
{
string s;
cin>>s;
int len = s.length();
for(int i=0;i<len;i++)
{
if(s[i]==35)
{
s.erase(s.begin()+i);
i–;
}
}
cout<<"New string is "<<s<<endl;
}
but if i have to erase the element at ith position and and the previous postion also like:
if input is a##s
then output should be s …as one **#**countered so with # , a should also be deleted…
but my code is not working
what is the moto of the above stmt i cant understand what u are trying to do!
please tell the i/p and desired o/p and the one u are getting
this is more of a stack based question if u need the code i can provide u one
can you just tell the stack approach…i will code it myself
iterate from the right side of botth strings
consider 2 stacks 1 for each string
telling for 1 string do it for both
in case u encounter #
pop only if stack is not empty
else
enter any other character that is encountered
do this for both the strings
now start popping elements from both the stacks until both or either is empty
if the top of stack is not same
return false;
return true if both the stack are empty
false if either is not empty
please send the code…i am not able to understand the concept clearly…i will dry run and understand!
bool backspaceCompare(string S, string T) {
stack<char> s1 , t1;
for(char c : S){
if( c == '#' ){
if(!s1.empty())
s1.pop();
continue;
}
s1.push(c);
}
for(char x : T){
if(x =='#') {
if(!t1.empty())
t1.pop();
continue;
}
t1.push(x);
}
while(!s1.empty() and !t1.empty()){
if(s1.top() != t1.top())
return false;
s1.pop(); t1.pop();
}
return s1.empty() == t1.empty();
}
Thanku so much Ma’am…just please tell the problem in my code
for(int i=0;i<len;i++)
{
if(S[i]==35)
{
S.erase(S.begin()+i);
i--;
}
if(S.empty())
{
break;
}
}
isee kya kia h
?
basically # ki ascii value dekh…uss element ko erase krrha…and usse previous one bh…maine S.erase(S.begin()+i);
i–;
ek aur baar likha…jisse previous element bh # se phele wla errase hojae
and what is previous koi element hua he nhi to
take this question as pressing backspace in notepad editor
okay…
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.