strings.cpp 906 B

1234567891011121314151617181920212223242526
  1. // strings.cpp -- storing strings in an array
  2. #include <iostream>
  3. #include <cstring> // for the strlen() function
  4. int main()
  5. {
  6. using namespace std;
  7. const int Size = 15;
  8. char name1[Size]; // empty array
  9. char name2[Size] = "C++owboy"; // initialized array
  10. // NOTE: some implementations may require the static keyword
  11. // to initialize the array name2
  12. cout << "Howdy! I'm " << name2;
  13. cout << "! What's your name?\n";
  14. cin >> name1;
  15. cout << "Well, " << name1 << ", your name has ";
  16. cout << strlen(name1) << " letters and is stored\n";
  17. cout << "in an array of " << sizeof(name1) << " bytes.\n";
  18. cout << "Your initial is " << name1[0] << ".\n";
  19. name2[3] = '\0'; // set to null character
  20. cout << "Here are the first 3 characters of my name: ";
  21. cout << name2 << endl;
  22. // cin.get();
  23. // cin.get();
  24. return 0;
  25. }