String not getting displayed


Can someone please let me know why am I not able to see the output?

there were couple of mistakes in your code

  1. you were not considering test cases
  2. s3 is initially empty so s3[i] makes no sense, instead you can add ‘0’ or ‘1’ at the end
    Corrected code
    https://ide.codingblocks.com/s/236476
1 Like

Can you explain why char ch[i] makes sense but string s[i] does not? Also, something interesting to note- when you s[i]=‘0’ and simultaneously print cout<<s[i] it is indeed showing 0. Why so? if we cannot update string like that as you said?

character array’s size if pre defined but string’s size is not predefined.

But its elements are accessible individually and therefore writing them over shouldn’t be a problem?

yes writting over the elements shouldnt be a problem but you are trying to access the elements that doesnt exist, doing that wont create a new elements your list is initially empty, and when you do s[0] = ‘0’ please exaplain which element you are trying to write over.

Hi! I understand what you’re saying but again we are trying to not overwrite in that case but fill a blank bucket, as I told you if you cout s[0] right after updating it, it will show you 0. Which means we are able to manipulate and successfully store the value.

acc. to your logic, this code https://ide.codingblocks.com/s/245946, should display H but it is not displaying anything, you need to append to string, and you can only change existing index, as simple as that.

https://ide.codingblocks.com/s/246163 check now. I never said cout<<s is giving me an output. however cout<<s[0] is. Anyway, I understood the reason. The elements can be accessed as I told you above and indeed we can access the buckets. The only reason why the string does not get displayed is, because of the fact that the NULL character does not shift if we do not append the string. all these updates are being done after the NULL array hence they are insignificant.

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.