I'm new to programming , when I cout << str outside the loop , it prints nothing and when cout << str[0] << str[1] it prints the char in this position and also length of str outside of the loop is 0, what is the reason for that behavior.
Note: I tried to use concatenate str+= s[i]; and it's working in this case , I can cout << str;
#include <iostream>
#include <string>
using namespace std;
int main()
{
int n,k;
cin >> n >> k;
int z = k;
string s = "abcdefghijklmnopqrstuvwxyz";
string str;
int repeated = n-k;
for(int i{};i<n;++i){
if(k > 0){ //distinct
str[i] = s[i];
k--;
//cout << str[i];
}else if(k==0 && repeated >0){ //repeated
str[i] = str[i-z];
repeated--;
//cout << str[i];
}
}
cout << str;
//cout << str[0] << str[1] << str[2] << str[3] << str[4] ;
return 0;
}