C++ Character sequence using arrays

In this tutorial we take a look at character sequences using arrays.

We already know that the C++ Standard Library (STL) implements a powerful string class. The string class can be used to manipulate strings of characters. But it is also possible to use plain arrays of char elements to represent a string. (Strings are in fact a sequence of characters.)

We can do the following:


	char my_string[10];

In this array (my_string) we can store up to ten elements of the type char. So we can store a character sequence of up to ten characters.

It is also possible to store less than ten characters in this array. So we need a special character to mark the end of the sequence. The special character used is the null character (it can be written like ‘\0′). Let’s say we want to store something like this:

Example array of char

We can store it in the array my_string because it has less then ten characters. (This is actually not the right way because we waste four elements and therefor memory).

Initialization

Let’s say we want to store the character sequence “HELLO” in an array called my_string.

We can do the following:


	char my_string[] = { 'H', 'E', 'L', 'L', 'O', '\0'};

We now declared an array of six elements of the type char. (The word “HELLO” plus the null character.)

There is also a different method to initialize an array of char elements with a value, using what is called: string literals. String literals are words between double quotes (“HELLO” or “Hello World”). The advantage of a string literal is that the null terminate character (‘\0′) is appended automatically at the end.

So we also can say:


	char my_string[] = "HELLO";

An example

Ok, now that we know how to initialize a character sequence (array of chars), let’s look at an example:


	#include<iostream>
	using namespace std;

	int main ()
	{
		char ask[] = "Enter your first name: ";
		char answer[] = "So your name is ";
		char name [100];

		cout << ask;
		cin >> name;
		cout << answer << name << ".";

		return 0;
	}

Note: The answer can only be a hundred characters long.

First we declare three arrays. The first array is used to ask the question. The second is used in the response. The third array is used to store the answer on the question. Using cout to print and cin to get the input we then ask the question, receive the answer en print the response.

Converting

It is possible to convert an array of chars to a string.

This can be done like so:


	#include<iostream>
	#include<string>
	using namespace std;

	int main ()
	{
		string my_string;
		char my_array[20]= "HELLO THERE !";

		my_string = my_array;
		cout << my_string << '\n';

		return 0;
	}

That’s all for this tutorial.

This entry was posted in C++ Tutorials. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site. Tweet This! Tweet This! or use to share this post with others.

There is currently one response to “C++ Character sequence using arrays”

Why not let us know what you think by adding your own comment!

  1. Fareena on April 13th, 2010:

    This is nicely written and very well understandable, although, i dont know what is string because i’m studing intro to cs; easy language not so much jargon has been used.

Leave a Reply: