Why new length is not updated after inserting a char
Hello @guptanikhil898
When you are executing the following statement:
string s;
You are just creating an object of class string as s.
And initially the size of string is set to 0 as you have not initialized it.
So, the constructor of string class will initialize it with “”.
Now, when you access the index as s[0], you essentially index outside the bounds of the “used” string (i.e. its size).
Thus, while printing s, the compiler will print “” i.e. empty string because assignment s[0]=‘a’ would not update it’s size.
Solution:
s+=‘a’;
or
s=“a”;
Hope, this would help.
Give a like if you are satisfied.
if i want to add one more after ‘a’ then how should i do that
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.