Why the code below gives as a result s2=0111 instead of s2=000111?
<code>#include <iostream>
using namespace std;
int main()
{
string s2="111";
for(int j=0;j<(s2.size()%4);j++){
s2='0'+s2;}
cout<<s2<<endl;
system("PAUSE");
return 0;
}
</code>
<code>#include <iostream>
using namespace std;
int main()
{
string s2="111";
for(int j=0;j<(s2.size()%4);j++){
s2='0'+s2;}
cout<<s2<<endl;
system("PAUSE");
return 0;
}
</code>
#include <iostream>
using namespace std;
int main()
{
string s2="111";
for(int j=0;j<(s2.size()%4);j++){
s2='0'+s2;}
cout<<s2<<endl;
system("PAUSE");
return 0;
}
The size of s2 is 3, so is s2.size()%4, but there is only one zero added in front of s2.
New contributor
Rado is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.