Introduction
In programming languages, we very often hear about the terms Strings and Characters. In programming, knowing the difference between strings and characters is crucial. Characters represent individual symbols like letters or digits, while strings are sequences of characters. Understanding these distinctions helps in effectively handling and manipulating text data in various applications.
Also see, Longest Common Substring and Rabin Karp Algorithm
What is Character?
Characters are simple alphabets such as a, b, c, d...., but with one exception. Any single-digit integer, such as 0, 1, 2, 3, 4,..., and special characters, such as $,%, +, -,*,..., etc., are similarly treated as characters in computer programming, and you can assign them to a character type variable by simply enclosing them in single quotes.
For example, we declare a character type variable ch and assign it the value 'a'.
char ch = 'a';
Here, 'a' is referred to as a character literal or a character constant, and 'ch' is a character type variable that can store a character from the implementation's character set. Not only a, b, c, d...., etc., but also numbers like 0, 1, 2, 3, 4..., etc., or special characters like $,%, +, -,*..., etc. will be considered as character literals and can be assigned to a variable of character type, making the following point valid.
char ch = '$';
A character data type takes up 8 bits of memory; therefore, you can store any 256 possible values in a character whose ASCII value is between -127 and 127.
Any character, including special characters like !, @, #, $,%, &, *,_, +..., etc., can be stored in a character data type.
Note that only one letter or one digit can be enclosed in single quotations; multiple letters or numbers cannot be enclosed in single quotes. As a result, the following statements are incorrect in C/C++ programming:-
char ch = 'abc';
char ch = '123';
Let’s learn about Strings.
Read More About, Data Structures and Algorithms and Data Structure.