Strings - C-Style

Useful Links

http://www.juicystudio.com/tutorial/cpp/strings.html

What are Strings?

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.

Declaring Strings

Explicitly declare the \0 character

char cat[7] = {'f','l','u','f','f','y','\0'}; // a string

Use a quoted string, called a string constant or string literal

char elephant[] = "Dumbo"; // let the compiler count
char bird[10] = "Mr. Cheep"; // the \0 is understood.

When determining the minimum array size necessary to hold a string, remember to include the terminating null character in your count.
There is no harm, other than wasted space, in making an array larger than the string.
That's because functions that work with strings are guided by the location of the null character.

String Concatenation

Any two string constants seperated only by white space( spaces, tabs, and newlines) automatically are joined into one. Thus, all the following output statements are equal to one another.
  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";
  

String Input

Using 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.

REMEMBER: The minimum array size for holding a string (e.g. 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

Using 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:

For example, suppose you want to use getline() to read a name into a 20-element name array. You would use this call:

cin.getline(name,20)
The following program shows how to use getline().
//getline_input.cpp -- reading more than one word with getline
#include 
using 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.

Using get() to input strings

The istream class has another member function called get(), which comes in several variations.
One variant works much like getline(). But rather than read and discard the newline character, get() leaves that character in the input queue.
Fortunately, another variation of get() reads the single next character, even if it is a newline, so you can use it to dispose of the newline and prepare for the next line of input. That is, this sequence works:
   cin.get(name, ArraySize);      // read first line
   cin.get();                     // read newline
   cin.get(dessert, ArraySize);   // read second line
   
Another 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);

reads two consecutive input lines.