-2
#include<bits/stdc++.h>
using namespace std;
int main ()
{
    string str="appleap",str1;
    for(int i=0;i<str.size();i++)
        str1[i]=str[i];
    cout<<"xxxxxx  "<<str1<<endl;
    cout<<str1[0]<<str1[1]<<str1[2]<<str1[3]<<str1[4]<<str1[5]<<str1[6]<<endl;
}

Output is :
xxxxxx
appleap

why is str1 not printing after "xxxxxx" but why it is printing when access str1 character by character?

can anyone help me?

sudhi
  • 1
  • 6
    Undefined behavior due to `str1[i]=str[i];`, when `str1` is empty. – Algirdas Preidžius Aug 24 '18 at 13:18
  • 2
    Unrelated to your problem, but please read [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – Some programmer dude Aug 24 '18 at 13:19
  • 2
    you can just replace the `for` loop with `str1 = str;` to copy the string, and avoid the issue entirely. – Sander De Dycker Aug 24 '18 at 13:21
  • @AlgirdasPreidžius: Please put that as an answer. Comments are to ask for clarification etc. – MSalters Aug 24 '18 at 13:23
  • 2
    @MSalters 1) There are plenty of answers already - no use of providing, yet another, exactly the same. 2) If the answer to the questions fits on a single line, I deem it to be too simple, to warrant an answer. – Algirdas Preidžius Aug 24 '18 at 13:26
  • @AlgirdasPreidžius: If you deem it too simple to answer, then don't answer it. There are now answers because the question remained on the list of unanswered questions. – MSalters Aug 24 '18 at 14:14

3 Answers3

1

May be you want to use str1.push_back(str[i]) instead.

Basically, your str1 works as an array of characters this way, but not as a C++ string object.

Edit1: when you access str1 using square brackets, C++ assumes the string already has an allocated size that covers this access, which is not in your case. So either you push_back, or reserve some size for str1 first.

Ahmed Hammad
  • 2,798
  • 4
  • 18
  • 35
1

why is str1 not printing after "xxxxxx"

Because that string is empty when created by default constructor.

why it is printing when access str1 character by character?

Because you are illegaly accessing memory that you assigned values illegaly before.

To use operator[] on std::string with index pos that string size must be at least pos+1 otherwise you get Undefined Behavior documenatation

Returns a reference to the character at specified location pos. No bounds checking is performed. If pos > size(), the behavior is undefined.

There is special case when pos == size() but that is irrelevant in your case.

Slava
  • 43,454
  • 1
  • 47
  • 90
0
for(int i=0;i<str.size();i++)
    str1[i]=str[i];

When using operator[] on std::string, you're not checking if the index that you're trying to access is correct. In this loop, you're trying to access elements of an empty string, which is undefined behavior, meaning anything can happen.

If you want to copy a std::string, just use operator=, like so

str1 = str;

str will copy the content of str, so you don't have to do that manually.

Kaldrr
  • 2,780
  • 8
  • 11