This part alone is crashing my program. Why is that? This is to add a ‘u’ after each ‘p’ in the string.
for(int i=0;i<s.size();i++)
{
if((s[i]=='p'))
{
s.insert(i,1,'u');
}
}
cout<<"after adding u after each p: "<<s;
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
(You’ve written ‘u’ instead of ‘h’ in your code.)
Let’s assume you have the string that is just “p”. So s[0] == ‘p’. Now you insert ‘h’ at 0 so the string is now “hp” (the h is before the p, not after it as you intended). In the next iteration i is 1 and there is a p (it just moved there) so another h will be inserted. This goes on until you run out of memory.
Try: