All data in is represented in a computer as binary. Characters are encoded into binary using
character sets such as ASCII, Extended ASCII or Unicode.
ASCII
Uses 7 bits to store all English characters (upper and lower case) as well as numbers, punctuation
and some control codes, which are mostly obsolete. Because ASCII uses 7 bits, it can easily be stored
within one byte.
Strings
Strings are an abstract data type that represent words. In reality, they are arrays of the characters
that make up the words. It is possible to compare strings by showing whether they are greater of
lesser than another string based on alphabetical order.
All strings end with a special byte, called the null byte. This signifies the end of a string and so means
that the array of characters assigned to a string will always be one greater in size than it seems it
should be. For example, “Hello World!” has 12 characters, but the character array would have 13
elements in.
Strings in C
In C, the library <string.h> provides functions for dealing with strings. In C, the compiler always
assumes that a string ends with a null byte (‘\0’).
strcpy(dest, txt) – copies the text in ‘txt’ to the variable in the ‘dest’ position.
strcat(txt1, txt2) – concatenates txt2 onto the end of txt1.
strlen(txt) – returns the number of characters in ‘txt’ excluding the null byte.
In C, comparing strings is not done by ‘string1 == string2’, instead the following can be used.
strcmp(txt1, txt2) – returns negative if txt1 is alphabetically smaller than txt2, zero if they are the
same and positive if txt1 is bigger than txt2.
strncmp(txt1, txt2, n) – compares the first ‘n’ characters of txt1 and txt2 in the same manner as
above.