+ 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?

1st Apr 2017, 5:29 AM
Giovanni Del Gallo
Giovanni Del Gallo - avatar
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]);
1st Apr 2017, 6:09 AM
Hatsy Rei
Hatsy Rei - avatar
+ 16
char b[a.length()]; // fix the spelling of length
1st Apr 2017, 5:41 AM
Shamima Yasmin
Shamima Yasmin - avatar
+ 4
char b [a.length () + 1]; // +1 for null character '\0' in case of char array.
1st Apr 2017, 6:15 AM
देवेंद्र महाजन (Devender)
देवेंद्र महाजन (Devender) - avatar