Installing CodeBlocs compiler
© The scientific sentence. 1998-2016
Programming with C/C++ Language
Pointers, Arrays and Strings
Strings in C++
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:
This would declare a string with a length of 21 characters.
Note that arrays begin at zero, not 1 for the index number.
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
And write :
an_array = new char;
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:
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.
Note that we are actually passing the address of the array when we pass
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
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 *
strcat is short for string concatenate, that is to add to the end,
strcat adds the second string to the first string. It returns a pointer
to the concatenated string. Beware this function, it assumes that
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 *
strcpy is short for string copy, which means it copies the entire contents
dest. The contents of
dest after strcpy will be exactly
the same as
src such that strcmp (
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;
char fullname; // 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'; /* 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";
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 *
which will only copy
len bytes from
len should be less than the size of
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
dest. We might avoid this problem by using strlen to get the
src and make sure it will fit in