Character Arrays and C-String
Character Arrays and C-String
Aamina Batool
Character Array
the C-string
"Hello" represents six characters: 'H', 'e', 'l',
'l', 'o', and '\0’
to store the C-string "Hello“ in computer memory, we
need six memory cells of type char.
More Examples
char name[16];
This statement declares an array name of 16
components of type char. Because C-strings are
null terminated and name has 16 components,
the largest string that can be stored in name is of
length 15, to leave room for the terminating
'\0’.
More Examples
char name[16] = {'J', 'o', 'h', 'n', '\0’};
declares an array name containing 16 components of type
char and stores the C-string "John" in it.
char name[16] = "John";
The statement:
char name[] = "John";
declares a C-string variable name of a length large enough—in
this case, 5—and stores "John" in it.
Illegal operations
char studentName[26];
studentName = "Lisa L. Johnson"; //illegal
Built-in functions of C-String
String Comparison
In C++, C-strings are compared character by character using
the system’s collating sequence.
The C-string "Air" is less than the C-string "Boat"
because the first character of "Air" is less than the first
character of "Boat".
The C-string "Air" is less than the C-string "An" because
the first characters of both strings are the same, but the
second character 'i’ of "Air" is less than the second
character 'n' of "An".
String Comparison
The C-string "Bill" is less than the C-string "Billy" because the first
four characters of "Bill" and "Billy" are the same, but the fifth
character of "Bill", which is '\0' (the null character), is less than the
fifth character of "Billy", which is 'y'. (Recall that C-strings in C11 are
null terminated.)
The C-string "Hello" is less than "hello" because the first character
'H' of the C-string "Hello" is less than the first character 'h' of the
C-string "hello".
the function strcmp compares its first C-string argument with its second
C-string argument character by character.
Use of built-in functions
char studentName[21];
char myname[16];
char yourname[16];
The following statements show how string functions work:
Use of built-in functions
Reading/Writing Strings
char name[31];
cin >> name;
stores the next input C-string into name
The length of the input C-string must be less than or equal to 30.
If the length of the input string is 4, the computer stores the four
characters that are input and the null character '\0’.
If the length of the input C-string is more than 30, then because there is
no check on the array index bounds, the computer continues storing the
string in whatever memory cells follow name.
This process can cause serious problems, because data in the adjacent
memory cells will be corrupted
get Function
the extraction operator, >>, skips all leading whitespace characters and stops reading
data into the current variable as soon as it finds the first whitespace character or invalid
data
C-strings that contain blanks cannot be read using the extraction operator, >>. For
example, if a first name and last name are separated by blanks, they cannot be read
into name.
char str[31];
cin.get(str, 31);
If the input is:
William T. Johnson
then "William T. Johnson" is stored in str. Suppose that the input is:
Hello there. My name is Mickey Blair.
which is a string of length 37. Because str can store, at most, 30 characters, the
C-string "Hello there. My name is Mickey" is stored in str.
String input cont’d
char str1[26];
char str2[26];
char discard;
two lines of input:
Summer is warm.
Winter will be cold.
suppose that we want to store the first C-string in str1 and the second
C-string in str2. Both str1 and str2 can store C-strings that are up
to 25 characters in length. Because the number of characters in the first
line is 15, the reading stops at '\n’.
String input cont’d
Now the newline character remains in the input buffer and must be manually
discarded.
Therefore, you must read and discard the newline character at the end of the
first line to store the second line into str2.
The following sequence of statements stores the first line into str1 and the
second line into str2:
cin.get(str1, 26);
cin.get(discard);
cin.get(str2, 26);
Study this for more information:
https://www.geeksforgeeks.org/clearing-the-input-buffer-in-cc/
getline function
#include <cstring>
For instance, the following code segment uses the strlen
function to determine the length of the string stored in the name
array:
char name[] = "Thomas Edison";
int length;
length = strlen(name);
The strcat Function
The function concatenates or appends one string to another.
const int SIZE = 13;
char string1[SIZE] = "Hello ";
char string2[] = "World!";
cout << string1 << endl;
cout << string2 << endl;
strcat(string1, string2);
cout << string1 << endl;
These statements will cause the following output:
Hello
World!
Hello World!
The strcat Function
The strncat and strncpy Functions
(to avoid the out of bound index)