+ 3
How to do an array of the same size of a string?
I tried this way : string a; cin >> a ; char b [a.lenght ()]; including string libraries, but i cant' make an array not knowin is size before. Can someone help me?
3 Antworten
+ 18
#include <iostream>
#include <cstring>
using namespace std;
int main ()
{
string a;
cin >> a;
char b[strlen(a.c_str())];
return 0;
}
// tested and working.
// you may find array size using
// cout << sizeof(b)/sizeof(b[0]);
+ 16
char b[a.length()];
// fix the spelling of length
+ 4
char b [a.length () + 1]; // +1 for null character '\0' in case of char array.