A string is a series of characters stored in consecutive bytes of memory. C++ has two ways of dealing with strings. The first, taken from C and often called a C-style string is the method you'll learn here.
C-style strings have a special feature: The last character of every string is the null character. This character, writen \0
, is the character with ASCII code 0, and it serves to mark the string's end.
\0
character
char cat[7] = {'f','l','u','f','f','y','\0'}; // a string
char elephant[] = "Dumbo"; // let the compiler count
char bird[10] = "Mr. Cheep"; // the \0
is understood.
cout << "I am a little boy riding a horse " "on a monday morning\n"; cout << "I am a little boy riding a horse on a monday morning"; cout << "I am a little boy riding a horse " "on a m" "onday morning";
cin
to input strings
You can use cin
to place an input string into an array.
This program uses the standard library function strlen()
to get the length of a string. It does not take into account the \0
(null) character.
The standard cstring
header file (or string.h
for older implementations) provides declarations for this and many other string-related functions.
junior
) is strlen(junior) + 1
.// strings.cpp -- storing strings in an array #include#include // for the strlen() function using namespace std; int main() { const int Size = 15; char name1[Size]; // empty array char name2[Size] = "Andrew Lim"; // initialized array cout << "Yo!!! I'm " << name2; cout << "! What's your name?\n"; cin >> name1; 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'; // null character cout << "Here are the first 3 characters of my name: "; cout << name2 << "\n"; return 0; } OUTPUT: Yo!!! I'm Andrew Lim! What's your name? Alexander Well, Alexander, your name has 9 letters and is stored in an array of 15 bytes. Your initial is A. Here are the first 3 characters of my name: And
getline()
to input strings
The above program is flawed because by simply using cin
we cannot
handle names with more than two words.
The istream
class, of which cin
is an example,
has some line-oriented class member functions. The getline()
function
reads a whole line, using the newline character transmitted by the ENTER
to mark the end of input.
You invoke this method by using cin.getline()
as a function call.
The function takes two arguments:
getline()
to read a name into a
20-element name
array. You would use this call:
cin.getline(name,20)
getline()
.
//getline_input.cpp -- reading more than one word with getline #includeusing namespace std; int main() { const int ArraySize = 20; char name[ArraySize]; char dessert[ArraySize]; cout << "Enter your name:\n"; cin.getline(name, ArraySize); // reads through newline cout << "Enter your favourite dessert:\n"; cin.getline(dessert, ArraySize); cout << "I have some delicious " << dessert; cout << " for you, " << name << ".\n"; return 0; } OUTPUT: Enter your name: Bill Clinton Enter your favourite dessert: Monica Lewinsky I have some delicious Monica Lewinsky for you, Bill Clinton.
get()
to input stringsistream
class has another member function called get(), which comes in
several variations.
cin.get(name, ArraySize); // read first line cin.get(); // read newline cin.get(dessert, ArraySize); // read second lineAnother way to use
get()
is to concatenate the two class member functions
as follows.
cin.get(name, ArraySize).get();What makes this possible is that
cin.get(name, ArraySize)
returns the
cin
object, which then is used as the object that invokes the get()
function. Similarly, the statement
cin.getline(name1, ArraySize).getline(name2, ArraySize);