String is basically a sequence of characters. As we know, we already have a character array so why do we need a separate data type for string . The reason that makes string special is the small set of characters. We typically encounter small set of characters in string. There are 128 standard ASCII values that cover most of the character that we generally see. For example English alphabets ‘a’ to ‘z’ and ‘A’ to ‘Z’.
Multiple languages encode the char data type in different ways. In c and c++, the char data type uses ASCII and stores character using 8 bits while in Java, char data type uses UTF 16 and is stored using 16 bits.
ASCII stands for American Standard Code for Information Interchange. ASCII value of some characters are shown below.

UTF-16 is a character encoding capable of encoding all 1,112,064 valid character code points of Unicode.
The first 128 characters and their encoding have same ASCII value and UTF-16 value.
In C++, if we assign a character to an integer and print it’s value, we will get the corresponding ASCII value of character as shown below.
int main()
{
char x = 'a';
cout<<(int)x;
return 0;
}
The output for the above code would be 97, as the ASCII value for character ‘a’ is 97, as shown in the table above.
Let us see an example to understand this concept.
Write a program to print frequencies of character in a string of lowercase alphabets in sorted order.
In C++:

In Java:

In the above code, we create an array of size 26 (i.e for each lowercase letter) and initialise this array as 0. Thereafter, we traverse through the string and use individual character of string to locate its index and then increment the corresponding count. Index 0 corresponds to a, index 1 corresponds to b and so on.
When you use character in a context where we should be using integer, they are automatically converted into integer corresponding to ASCII value in c++ and UTF-16 in java.
In order to obtain the frequency of the characters, we then traverse the count array and print characters having count greater than 0.
This Article Was Contributed By Ritika Semwal