+ 6
What's the problem?
Hey Guys!! I am using at function of string to assign values. But I am getting an error. Here is code đđ #include <iostream> #include<string> using namespace std; int main() { string str1="hello"; string str2; str2.at(1)=str1.at(0); cout<<str2.at(1); return 0; } Can anyone tell me about this error. I have no Idea of it. link to code:- https://code.sololearn.com/caZ3as5bdJr6/?ref=app
4 Answers
+ 1
using the resize method
#include <iostream>
#include<string>
using namespace std;
int main()
{
string str1="hello";
string str2;
str2.resize(str1.size());
str2.at(1)=str1.at(0);
cout<<str2.at(1);
return 0;
}
+ 5
Okay!
So is there any other way to assign a string to a blank string using at function?
+ 4
Mohamed ELomari thanks!
+ 1
str2 is of length zero so you cant do at(1)
maybe line 8 should say str2 = str1; then it be of length 5 so you can do the at(1)