Coding &
programming

Installing CodeBlocs compiler

C language

C++ language

© The scientific sentence. 1998-2016

# Programming with C/C++ Language Pointers, Arrays and Strings Strings in C++

### 1. Definitions

In C++ a string is a string of characters. represented by an array. Then a string is an array of characters.

Strings are arrays of chars. They are words surrounded by double quotation marks. Example: "we are coding in cpp" is a string.

To declare a string of 20 letters, we will need an array of 21 dimension, and we write:

char a_string[21];

This would declare a string with a length of 21 characters.

Note that arrays begin at zero, not 1 for the index number.

In addition, a string ends with a null character, literally a '\0' character. However, just remember that there will be an extra character on the end on a string.

It is like a period at the end of a sentence, it is not counted as a letter, but it still takes up a space. Technically, in a 21 char array we could only hold 20 letters and one null character at the end to terminate the string.

Instead of an array, we can use a pointer, and make the following declaration:

char *an_array;

And write :

an_array = new char[21];

which allows us to access an_array just as if it were an array.

To delete the string, we must put [] between delete and an_array to free all the 21 bytes of memory allocated. For example:

delete [] an_array;

### 2. Using cin.get()

Strings are useful for holding all types of long input. We use cin>> to input a string, but it terminates the string after it reads the first space.

The best way to handle this situation is to use the function cin.getline () .

The prototype for that function is:

istream& getline(char *feca, int length, char terminal_char);

The char *feca 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 how long the string to be input can be at its maximum. That is how big the array is.

The char terminal_char means that the string will terminate if the user inputs whatever that character is. Note that it will discard whatever the terminal character is.

It is possible to make a function call of cin.getline(an_array, 21); 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.

Example:

Note that we are actually passing the address of the array when we pass a_string because arrays do not require an address operator (&) to be used to pass their address.

We can also replace by '\n' any character within the string, just we might make sure to enclose it with single quotes to inform the compiler of its character status, to have the getline terminate on that character.

### 3. String related functions

cstring is a header file that contains many functions for manipulating strings.

1. The string comparison function: strcmp :

int strcmp ( const char *s1, const char *s2 );

const is to declare a named constant

strcmp will accept two strings. It will return an integer. This integer will either be negative if s1 is less than s2, zero if s1 and s2 are equal, and positive if s1 is greater than s2.

strcmp is case sensitive. It also passes the address of the character array to the function to allow it to be accessed.

2. The string concatenate function: strcat :

char *strcat ( char *dest, const char *src );

strcat is short for string concatenate, that is to add to the end, or append.

strcat adds the second string to the first string. It returns a pointer to the concatenated string. Beware this function, it assumes that dest is large enough to hold the entire contents of src as well as its own contents.

3. The string copy function: strcpy :

char *strcpy ( char *dest, const char *src );

strcpy is short for string copy, which means it copies the entire contents of src into dest. The contents of dest after strcpy will be exactly the same as src such that strcmp (dest, src) will return 0.

4. The length of a string function: strlen :

size_t strlen ( const char *s );

strlen will return the length of a string, minus the terminating character ('\0'). The size_t is an integer type that cannot be negative.

### 4. Program Example



#include  //for  cin and cout
#include   //for the string functions

using namespace std;

int main()
{
char firstname[50];
char lastname[50];
char fullname[100]; // big enough to hold both firstname and lastname

cout<<"Please enter a first name: ";
cin.getline ( firstname, 50 );
if (strcmp ( firstname, "Golden mouth" ) == 0 ) // compare strings
cout<<"This first name is Golden mouth.\n";
else                                    // if not equal
cout<<"This first name is not Golden mouth.\n";
// Find the length of your name
cout<<"This firstname is "<< strlen ( firstname ) <<" letters long. \n";
cout<<"Enter a last name: ";
cin.getline ( lastname, 50 );
fullname[0] = '\0';            /* strcat searches for the terminating
character '\0' to cat after*/
strcat (fullname, firstname);     // write firstname into fullname
strcat ( fullname, " " );      // to separate the names by a space
strcat ( fullname, lastname ); // write lastname onto the end of fullname
cout<<"The full name is "<< fullname <<"\n";
cin.get();
}



With codeBlocs, we obtain:

There are string functions that take an additional argument to indicate the length of the destination buffer. For instance, the strcpy function has an analogous strncpy function:

char *strncpy ( char *dest, const char *src, size_t len );

which will only copy len bytes from src to dest.

Note that len should be less than the size of dest or the write could still go beyond the bounds of the array.

Unfortunately, strncpy can't guarantee that dest will have a null terminator attached to it. This might happen if the string src is longer than dest. We might avoid this problem by using strlen to get the length of src and make sure it will fit in dest.

 Web ScientificSentence

 chimie labs | scientific sentence | java | php | green cat | contact |