20 Apr 2017

Difference Between Character Array and String


Character-Array-string-1C++ supports both, Character array and string, as C++ has considerable benefits in using both of them. But, inability to operate on character array raises the development of class string. Both a character array and string contain the sequence of characters. But the fundamental difference between character array and string is that the “character array” can not be operated with standard operators, whereas, the “string “objects can be operated with standard operators. Let’s study the other differences between a character array and string.

Content: Character Array Vs String

  1. Comparison Chart
  2. Definition
  3. Key Differences
  4. Conclusion

Comparison Chart

BASIS FOR COMPARISONCHARACTER ARRAYSTRING
BasicCharacter array is collection of variables, of character data type.String is class and variables of string are the object of class "string".
Syntaxchar array_name [size];string string_name;
IndexingAn individual character in a character array can be accessed by its index in array.In string the particular character can be accessed by the function "string_name.charAt(index)".
Data TypeA character array does not define a datatype.A string defines a datatype in C++.
OperatorsOperators in C++ can not be applied on character array.You may apply standard C++ operator on the string.
BoundaryArray boundaries are easily overrun.Boundaries will not overrun.
AccessFast accessing.Slow accessing.

Definition of Character Array
A character array is a collection of the variables of “char” datatype; it can be a one-dimensional array or a two-dimensional array. It is also called “null terminated string”. A Character array is a sequence of the characters that are stored in consecutive memory addresses. In a character array, a particular character can be accessed by its index. A “Null character” terminates the character array”.
Character-Array-1
Let’s take an example of character array:-
  1. char name[ ]={'A', 'j', 'a', 'y', '\0'};
  2. or
  3. char name[ ]="Ajay";
Here, “char” is a character data type, “name” is a variable name of the character array. I had shown two ways to initialize the character array. In the first method, the null is explicitly mentioned and in the second method, the compiler automatically inserts the null.
The end of the string is always a null character; it is the terminating character of the character array. A character array is not a built-in data type; we create character array by declaring it. You can not apply standard operators on a character array. To operate on character array there are some built-in function such as,(strlen( ), strlwr( ), strupr( ), strcat( )). As the standard operators can not be applied to a character array, they cannot take part in any expression.
The character pointer to a character array can also be created.
Let’s understand it with an example.
  1. char s1[ ]="Hello";
  2. char s2[ ]="Sir";
  3. s1= s1+s2; //error operators can not be applied
  4. s2=s1; //error
  5. Character pointer
  6. char *s= "Morning";
  7. char *p;
  8. p=s; //executes
In above example, we had declared two character array s1, s2 and two character pointers s and p. Character array s1 and s2 are initialized, we can see that neither the addition operator(+) nor the assignment operator works on the character array. But a character pointer can be assigned to another character pointer.
Remember once the character array is initialized it can not be initialized again to another set of character. The access to a character array or a null terminated string is fast as compared to string in C++.

Definition of String

A string is not a built-in data type of C++. It a class object of type “string”. As in C++ creating a class is just like creating a “type”. The class “string” is a part of C++ library. It holds the set of character or character array as a whole. There are three reasons behind the development of a standard string class.
  • First is “consistency”, the character arrays are not data types in their own right.
  • Second is “convenience”, you can not use standard operators on a character array.
  • Third is “safety”, array boundaries are easily overrun.
String-Explanation-1
Let us understand strings with an example.
  1. string s1;
  2. s1= "Hello";
  3. string s2("Good morning");
  4. string s3="Hennery";
  5. string s4;
In the above declaration, four string variable or objects (s1, s2, s3, s4) are declared.  In above declaration, I had shown three ways of initializing the string. The string s1 is declared and then separately initialized. The string s2 is initialized by the constructor of class “String”. The string s3 is initialized at the time of its declaration as normal data type do. We can apply the standard operator to the string variables.
  1. s4=s1;// assigning one string object to other
  2. s4=s1+s2; // adding two string and storing the result in third string
  3. if(s3 > s2) // comparing two strings
  4. strings s5(s1); creating a new string object using existing string object
In above code, various operators are applied on a string and various operations are performed. The first statement copies one string object to another string object. In the second statement, two string are concatenated and stored in a third string. In the third statement, two string are compared. In the fourth statement, a new string object is created using the already existing string object.
Access to the string is slow as compared to a character array or null terminated string.

Key Differences Between Character Array and String

  1. A character array is a collection of variables which are of character datatype. String is a class that is instantiated to declare strings.
  2. Using index value you can access a character from a character array. On the other hand, if you want to access a particular character in a string, you can access it by function string’s_name.charAt(index).
  3. As an array is not a datatype similarly a character also is not a datatype. On the other hand, String being a class act as a reference type hence, it can be said String is a data type.
  4. You can not apply any operator on a character array whereas, you can apply operators on String.
  5. Being an array character array has a fixed length and its boundaries can be easily overrun. Where String does not have any boundaries.
  6. Array elements are stored in a contiguous memory location hence that can be accessed faster than string variable.

Conclusion:

Inability to operate on character array raised the development of standard string class.

1 comment:

commnet here