Posted By

marwac6 on 02/21/11


Tagged

array output strings initialisation


Versions (?)

Strings and arrays


 / Published in: C++
 

  1. #include <iostream> #include <cstring> // for the strlen() function int main() {
  2. using namespace std; const int Size = 15; char name1[Size];
  3. // empty array
  4. char name2[Size] = �C++owboy�; // initialized array // NOTE: some implementations may require the static keyword // to initialize the array name2
  5. cout << �Howdy! I�m � << name2; cout << �! What�s your name?\n�; cin >> name1;
  6. cout << �Well, � << name1 << �, your name has �; cout << strlen(name1) << � letters and is stored\n�; cout << �in an array of � << sizeof(name1) << � bytes.\n�; cout << �Your initial is � << name1[0] << �.\n�; name2[3] = �\0�;
  7. // null character
  8. cout << �Here are the first 3 characters of my name: �; cout << name2 << endl; return 0;
  9. }

Report this snippet  

You need to login to post a comment.