C++ Strings

C++ Strings are character type array and string literals are words surrounded by double quotation marks.

String Declaration

C++ strings are declared as one-dimensional array of characters which is terminated by a null (‘\0’) character. The the size of the character array containing the string is one more than the number of characters it holds due to the null character at the end.

Example

char myString[6] = {‘G’, ‘e’, ‘n’, ‘l’, ‘e’, ‘\0’};

You can also declare and initialize myString as

 char myString[] = “Genie”;

This would declare a string with a length of 6 characters. Do not forget that arrays begin at zero. A string ends with a null character so remember that there will be an extra character at the end on a string. Technically, in a 6 character array you could only hold 5 letters and one null character at the end to terminate the string.

char *arr; can also be used as a string. If you have read the tutorial on pointers, you can do something such as:

arr = new char[256];

which allows you to access arr just as it is an array. Keep in mind that to use delete you must put [] between delete and arr to tell it to free all 256 bytes of memory allocated.

delete [] arr;

The getline() function

Strings are useful for holding all types of long input. If you want the user to input his or her name, you must use a string. Using cin>> to input a string works, but it will terminate the string after it reads the first space. The best way to handle this situation is to use the function cin.getline. Technically cin is a class (similar to a structure), and you are calling one of its member functions. The most important thing is to understand how to use the function however.

getline(char *buffer, int length, char terminal_char);

The char *buffer is a pointer to the first element of the character array, so that it can actually be used to access the array. The int length is simply how long the string to be input can be at its maximum (how big the array is). The char terminal_char means that the string will terminate if the user inputs whatever that character is. Keep in mind that it will discard whatever the terminal character is.

It is possible to make a function call of cin.getline(arry, 50); without the terminal character. Note that ‘\n’ is the way of actually telling the compiler you mean a new line, i.e. someone hitting the enter key.

getline() example

#include <iostream>

using namespace std;

int main()
{

char string[256];

cout<<“Please enter a long string: “;
cin.getline ( string, 256, ‘\n’ );
cout<<“Your long string was: “<< string;
cin.get();

}

Remember that you are actually passing the address of the array when you pass string because arrays do not require an address operator (&) to be used to pass their address. Other than that, you could make ‘\n’ any character you want (make sure to enclose it with single quotes to inform the compiler of its character status) to have the getline terminate on that character.

C++ String Functions

<string> is a header file that contains many functions for manipulating strings. See some of string function below.

strcmp() function

int strcmp (s1, s2);

strcmp accept two strings and return an integer. This integer will either be:

Negative if s1 is less than s2.
Zero if s1 and s2 are equal.
Positive if s1 is greater than s2.

strcat() function

char strcat (dest, src);

strcat is short for string concatenate. It appends the second string at the end of the first string. Beware this function, it assumes that dest is large enough to hold the entire contents of src as well as its own contents.

strcpt() function

char strcpy (dest, src);

strcpy is short for string copy, it copies the entire contents of src into dest.

strlen() function

size_t strlen (s);

strlen() will return the length of the string excluding the null character. The size_t is nothing to worry about. Just treat it as an integer that cannot be negative.

Leave a Reply

Your email address will not be published. Required fields are marked *