Showing posts with label Strings. Show all posts
Showing posts with label Strings. Show all posts

Monday, June 17, 2013

C++ Strings Introduction | C++ Tutorial

A String is a sequence of characters, such as "Singh". In C++ strings are enclosed in quotation marks. To declare a variable of type string you have to include the header file for strings: #include <string> and the syntax to declare a variable that hold strings: string fruit = "apple";.

To read a string from user input:
cout << "What is your favorite Fruit?"
cin >> fruit;

When a string is read by the compiler, only one word is placed in the string variable. (Words separate by white spaces) If user input is Honeydew melon, then only Honeydew is placed into the string variable fruit. To read the entire name (more than one word) of the fruit you can use: getline(cin, fruit);, which will read all letters including white spaces until Enter key.