Topic : Character Arrays and String: Declaration | Initialization | Using



Character Array Strings:


In C++ a Character variable can hold a single character value, such as 'A' or '#'. Single character values are always expressed within single Quotation (' ') Marks. When ever we will declare an variable for character having type "char", the "char" will hold enough Memory for one character. If we want to express Multiple Character values as a constant, such as a Your Name or any Program title, we Use double (" ") Quotation Marks. A C++ Value expressed within double Quotation Marks is commonly called A string, or a literal String.



character Array String: Infobrother:

Declaration of Character Arrays:

In all programs seen until now, we have used only Numerical variables, used to express Numbers exclusively. But in addition to numerical variables there also exist strings of characters, that allow us to represent successions of characters, like words, sentences, names, texts, etc. Until now we have only used them as constants, but we have Never considered variables able to contain them.

In C++ there is No Specific elemental variable type to store strings of characters. In order to fulfill this feature we can use Arrays of type char, which are successions of "Char" elements. Remember that this data type (char) is the one used to store a single character, for that reason arrays of them are generally used to make strings of single characters. The basic syntax to use Character Array is:


 char    Name  [ 12 ];


In the above Example, the Array "Name" can store 12 character long.


Initialization of Character Arrays:

In C++ an Array of character and a string can be declared in the same way. We might create an Array of characters if we need to store any Name or grades etc. We can store individual character of a string in an array. However, we do not defer to an array of characters as a string unless the last usable character in the string is the "NULL character":


The Null Character is represented by the combinatioin "\0" (backslash and zero), or by using the constant "NULL" that is available in <iostream> Library:



If we want to declare a string name Name and initialize it to "infobrother", each of the following statements provides the same result:



char Name[ ]    =   " InfoBrother ";
char Name[ ]    = { " InfoBrother " };
char Name[ 12 ] =   " InfoBrother ";
char Name[ 12 ] = { " InfoBrother " };
char Name[ 12 ] = { ' I ' , ' n ' , ' f ' , ' o ' , ' B ' , ' r ' , ' 0 '
                    , ' t ' , ' h ' , ' e ' , ' r ' , ' \0 ' };


Each Statement reserves 12 character positions, and stores an 'I' at Name[0], an 'n' at Name[1], an 'f' at Name[2] and so on.



When we store a string, the curly braces are Optional, but when we store individual characters, we must include them.


Following is the Memory Presentation of above defined string in C/C++


Memory Presenting of String: Infobrother:

Actually, we do not Place the Null character at the end of a string constant. The C++ compiler automatically places the '\0' at the end of the string when it initializes the Array. Let's have an simple example to print above-mentioned string:



/*String: InfoBrother*/
 
#include <iostream>
using namespace std;
int main ()
{
    char Name[12]= {'i','n','f','b','r','o','t','h','e','r'};
    cout<<Name;
    return 0;
}


Assigning Values to String Array:

Since the Value of an assignation can only be an element of an Array and not the entire array, it would be valid to assign a string of characters to an array of Char using Method like this:



Name[ 0 ] = ' I ';
Name[ 1 ] = ' n ';
Name[ 2 ] = ' f ';
Name[ 3 ] = ' o ';
...
...
...
Name[ 10 ] = ' r ';


But as we Know, this does not seem to be a very practical Method, Generally for assigning values to an Array, and more specifically to a string of characters, a series of function like Strcpy (string copy) is defined in the cstring (string.h) Library and can be called the following way:


strcpy (String1, String2);


This does copy the content of String2 into String1. String2 can be either an Array, a Pointer or a constant string, so the following line of code would be a valid way to assign the constant string "InfoBrother" to Name as shown in the given example


strcpy (Name, InfoBrother);

Let's have an simple example, by using strcpy() function to clear this concept:


We Need to Include <string.h> header file to use string functions.


/*Assigning Values to String Array: InfoBrother */
 
#include <iostream>
#include <string.h> 
using namespace std;
 
int main ()
{
    char name[12]; 
    strcpy (name, "Infobrother"); //string copy function:
    cout<<name;
    return 0;
}


The standard C++ Library Provides a string class that supports Multiple string function like "strcpy" function. we will discuss more on strings in our String tutorial.






I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement