Hello every one! Can anyone tell me the diff between strings and arrays?
Here is sample:
------------------------------------------------------------------------ By String:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <iostream>
#include <string>
usingnamespace std;
int main () {
string mystr;
cout << "What's your name? ";
getline (cin, mystr);
cout << "Hello " << mystr << ".\n";
cout << "What is your favorite team? ";
getline (cin, mystr);
cout << "I like " << mystr << " too!\n";
return 0;
}
---------------------------------------------------------------------------- By arrays:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
usingnamespace std;
int main () {
char name[256], title[256];
cout << "Enter your name: ";
cin.getline (name,256);
cout << "Enter your favourite movie: ";
cin.getline (title,256);
cout << name << "'s favourite movie is " << title;
return 0;
}
----------------------------------------------------------------------
which is preferable and can i pick characters one by one in string?
1) You don't have to manage the size yourself. 2) They can be resized. 3) They can be used elegantly with algorithms (with begin() and end() iterators).