Programming
Fundamentals
Lecture 2b
Character Arrays
• Character arrays are of special interest, and you process them differently than
you process other arrays.
• C++ provides many (predefined) functions that you can use with character
arrays.
• An array whose components are of type char.
• The statement ch = '\0’; stores the null character in ch, wherein ch is a char
variable.
• Null character plays an important role in processing character arrays.
• Two kinds of strings are commonly used in C++: C-strings and strings that are
objects of the string class.
• C-Strings may also be called char* strings, because they can be represented as
pointers to type char.
Character Arrays
• The most used term for character arrays is C-strings. However, there is a subtle
difference between character arrays and C-strings.
• string is a sequence of zero or more characters, and strings are enclosed in
double quotation marks.
• C-strings are arrays of type char.
Character Arrays
• In C++, C-strings are null terminated; that is, the last character in a C-string is
always the null character.
• A character array might not contain the null character, but the last character in
a C-string is always the null character.
• C-strings are stored in (one-dimensional) character arrays.
• Example of C-string
"John L. Johnson"
"Hello there.“
• There is a difference between 'A' and "A". The first one is character A; the
second is C-string A.
• Because C-strings are null terminated, "A" represents two characters: 'A' and '\
0'.
Character Arrays
• Similarly, the C-string "Hello" represents six characters: 'H', 'e', 'l', 'l', 'o', and '\
0’.
• To store 'A', we need only one memory cell of type char; to store "A", we need
two memory cells of type char—one for 'A' and one for '\0’.
• Consider the following statement:
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.
• If you store a C-string of length 10 in name, the first 11 components of name
are used and the last 5 are left unused.
Character Arrays
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.
• During char array variable declaration, C++ allows the C-string notation to be
used in the initialization statement.
• The above statement is, therefore, equivalent to:
char name[16] = "John";
char name[ ] = "John";
• Above statement declares a C-string variable name of a length large enough—in
this case, 5—and stores "John" in it.
• There is a difference between the last two statements: Both statements store
"John" in name, but the size of name in the statement 1 is 16, and the size of
Character Arrays
• Here’s an example that defines a single string variable.
#include <iostream>
using namespace std;
int main()
{
const int MAX = 80; //max characters in string
char str[MAX]; //string variable str
cout << “Enter a string: “;
cin >> str; //put string in str
cout << “You entered: “ << str << endl; //display string
from str
return 0;
}
If the user enters the string “Amanuensis”
C-String Operations
• C++ provides a set of functions that can be used for C-
string manipulation.
• The header file cstring describes these functions.
• Strcpy
• Strcmp
• strlen
• To use these functions, the program must include the header file cstring via the
include statement.
#include <cstring>
String Comparison
• In C++, C-strings are compared character by character.
• 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".
• 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".
Example
char studentName[21];
char myname[16];
char yourname[16];
String Input
char name[31];
• cin >> name;
• Cin will accept the input string but as soon as it checks the white
space , cin will skip the remaining characters to be inputted.
• It is due to extraction operator >>
• For example, if a first name and last name are separated by blanks,
they cannot be read into name.
• As the function get can be used to read a single character, it can also
be used to read in the strings.
String Input
• To read C-strings, you use the form of the function get that has two
parameters.
• The first parameter is a C-string variable.
• The second parameter specifies how many characters to read into
the string variable.
cin.get(str, m + 1);
• This statement stores the next m characters, or all characters until
the newline character '\n' is found, into str.
• The newline character is not stored in str.
• If the input C-string has fewer than m characters, then the reading
stops at the newline character.
Example
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.
Then, because str can store, at most, 30 characters, the C-string "Hello there. My
name is Mickey" is stored in str.
Example
#include <iostream>
using namespace std;
int main()
{
const int MAX = 80; //max characters in string
char str[MAX]; //string variable str
cout << “\nEnter a string: “;
cin.get(str, MAX); //put string in str
cout << “You entered: “ << str << endl;
return 0;
}
Example
Now, suppose that we have the statements:
char str1[26];
char str2[26];
char discard;
and the 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’.
• You must read and discard the newline character at the end of the first line to store the second
line into str2.
cin.get(str1, 26);
cin.get(discard);
cin.get(str2, 26);
String Input
• To read and store a line of input, including whitespace
characters, you can also use the stream function getline.
• Suppose that you have the following declaration:
char textLine[100];
• The following statement will read and store the next 99
characters, or until the newline character, into textLine.
• The null character will be automatically appended as the last
character of textLine.
cin.getline(textLine, 100);
Reading Multiple lines
• Cin.get() function can take a third argument to help in
this situation.
• This argument specifies the character that tells the
function to stop reading.
• The default value for this argument is the newline (‘\
n’) character, but if you call the function with some
other character for this argument, the default will be
overridden by the specified character.
Example
#include <iostream>
using namespace std;
const int MAX = 2000; //max characters in string
char str[MAX]; //string variable str
int main()
{
cout << “\nEnter a string:\n”;
cin.get(str, MAX, ‘$’); //terminate with $
cout << “You entered:\n” << str << endl;
return 0;
}
Copying String – Hard Way
#include <iostream>
#include <cstring> //for strlen()
using namespace std;
int main()
{ //initialized string
char str1[ ] = “Oh, Captain, my Captain! “
“our fearful trip is done”;
const int MAX = 80; //size of str2 buffer
char str2[MAX]; //empty string
for(int j=0; j<strlen(str1); j++) //copy strlen characters
str2[j] = str1[j]; // from str1 to str2
str2[j] = ‘\0’; //insert NULL at end
cout << str2 << endl; //display str2
return 0;
Copying String – Easy Way
#include <iostream>
#include <cstring> //for strcpy()
using namespace std;
int main()
{
char str1[] = “Tiger, tiger, burning bright\n”
“In the forests of the night”;
const int MAX = 80; //size of str2 buffer
char str2[MAX]; //empty string
strcpy(str2, str1); //copy str1 to str2
cout << str2 << endl; //display str2
return 0;
cin.get vs cin.getline
• cin is used to take a character as input. If multiple characters are used, it will accept only last
character.
• cin.get is also used to input a single character. A second parameter in cin.get arguments is used
to specify the number of characters thus it can be used to enter lines or character arrays.
char str[20];
cin.get(str,20)
• cin.getline has the same format as cin.get and is used to take lines of text as input.
• cin.getline always append a NULL character at the end of input steam of text while cin.get does
not put NULL character therefore cin.getline is usually used to take character arrays as input.
• cin doesn't perform any bounds checking on the array. When you use cin to read a string into a
character array, it will continue to read characters until it encounters a whitespace character
(such as a space, tab, or newline).
• If the array is not large enough to hold the entire string, cin will overflow the buffer, writing
characters beyond the end of the array. This can lead to undefined behavior such as buffer
overflow
The input Buffer
• The input buffer is a crucial component of the cin statement in C++.
• The input buffer is a temporary storage area where the input data is held before it's
processed by the program.
• Here's a step-by-step explanation of how the input buffer works with cin:
1. User Input: The user enters a line of text and presses the Enter key.
2. Input Buffer Fills: The input text is stored in the input buffer.
3. *cin Extracts Data*: When cin is executed, it extracts the required data from the input
buffer.
4. Data Conversion: If necessary, cin converts the extracted data to the required data type.
5. Data Assignment: The converted data is assigned to the specified variable.
6. Input Buffer Clears: After the data is extracted and assigned, the input buffer is cleared.
The input Buffer (Issues)
• Here are some common issues related to the input buffer:
1. Buffer Overflow: If the user enters more data than the buffer can hold, it can cause a
buffer overflow, leading to undefined behavior.
2. Leftover Characters: If cin doesn't consume all the characters in the input buffer, the
leftover characters can cause issues with subsequent input operations.
Best Practices
*Use cin.ignore()*: Use cin.ignore() to ignore any leftover characters in the input buffer.
*Use cin.clear()*: Use cin.clear() to clear the input buffer and reset the error flags.