An Array Type for Strings • C-strings can be used to represent strings of characters – C-strings are stored as arrays of characters – C-strings use the null character '\0' to end a stri
Trang 1C and C++ Strings
Trang 2An Array Type for Strings
• C-strings can be used to represent strings of characters
– C-strings are stored as arrays of characters
– C-strings use the null character '\0' to end a string
• The Null character is a single character
– To declare a C-string variable, declare an array of characters:
Trang 3• Declaring a C-string as char s[10] creates space for only nine characters
– The null character terminator requires one space
• A C-string variable does not need a size
variable
– The null character immediately follows the last
character of the string
• Example:
s[0] H i s[1] s[2] M s[3] s[4] o m s[5] ! s[6] \0 s[7] s[8] ? s[9] ?
C-string Details
Trang 4Initializing a C-string
• To initialize a C-string during declaration:
char my_message[20] = "Hi there.";
– The null character '\0' is added for you
• Another alternative:
char short_string[ ] = "abc";
but not this:
char short_string[ ] = {'a', 'b', 'c'};
– because null character '\0' will NOT be added
Is called a string constant
or string literal
Trang 5Length of C Strings
To calculate the length of a string use the strlen() function – returns the
number of characters between the start of the string to the terminating
Trang 6Assignment With C-strings
• This statement is illegal:
char my_name[20], your_name[20]; my_name = "Thao"; ERROR!
– This is an assignment statement, not an
Trang 8A Problem With strcpy
• strcpy can create problems if not used
strcpy (my_name, "Thao Thi Nguyen");
this will overwrite memory!
Trang 9A Solution for strcpy
• A safer version of strcpy is named strncpy
– strncpy uses a third argument representing the maximum number of characters to copy
– Example: char another_string[10];
strncpy(another_string,
another_string[9] = ‘\0’;
This code copies up to 9 characters into
another_string, leaving one space for '\0'
Trang 10Comparing C-strings
• The = = operator does not work with C-strings
– The predefined function strcmp is used to compare C-string variables
– Example: #include <cstring>
Trang 13More C-string Functions
– The second argument is added to the end of the first
– The result is placed in the first argument
– Example:
char string_var[20] = "The rain";
strcat(string_var, "in Spain");
Now string_var contains "The rainin Spain "
– A third parameter specifies a limit for the number
of characters to concatenate
Trang 14C-string Output
• C-strings can be output with the insertion
operator <<
char news[] = "is good.";
cout << "The news " << news << endl;
Output: The news is good
Trang 15
Example
Trang 16C-string Input
• The extraction operator >> can fill a C-string
– Whitespace ends reading of data
– Example: char a[80], b[80];
cout << "Enter input: " << endl;
Trang 17Issues with cin >> for strings
cin >> (extraction operator) uses whitespace (spaces, tab,
newline) to delineate a string Therefore it will only read one word when it extracts input for a string (skipping all the
whitespace characters before reading each word)
char name[20], address[50];
cout << "Please enter your name:\n";
cin >> name;
user types "John Smith" and hits enter key
cout << "Please enter your address:\n";
Trang 18Reading an Entire Line
• Predefined member function getline can read an entire line, including spaces
– getline is a member of all input streams (like cin)
– getline has two arguments
• The first is a C-string variable to receive input
• The second is an integer, specifying the size of the string variable
Trang 19Using getline
spaces into a single C-string variable
– char a[80];
cout << "Enter input:\n";
cin.getline(a, 80);
cout << a << End Of Output\n";
and could produce:
Enter some input:
Do be do to you!
Do be do to you!End of Output
Trang 20Mixing cin >> and getline
• Recall cin >> n skips whitespace (spaces, tabs, newline) to find what it needs to read, then stops reading when whitespace is found
• cin >> leaves the '\n' character in the input
Trang 21getline after cin >>
double value;
cin >> value;
char str[80];
cin.getline(str, 80);
And you type: 3.14<return>
• 3.14 is read into value
• The newline following the 3.14 is still sitting in the input buffer
• cin.getline(str)immediately processes the newline that
is still on the input buffer
str becomes an empty string (" \0")
• It looks like that the application "skipped" the cin.getline()
statement
• Solution?
• Add a call to cin.get()/cin.ignore() after calling cin >> x
3 1 4 \n
Trang 22cin.ignore
characters, including '\n' that remain in a line
– Ignore takes two arguments
• First, the maximum number of characters to discard
• Second, the character that stops reading and discarding – Example:
• cin.ignore(1000, '\n');
discards up to 1000 characters until it finds a '\n' cin.ignore();
discards the next character
• discard:sự chui bài, sự dập bài
Trang 24The Standard string Class
• The string class allows the programmer to
treat strings as a basic data type
– No need to deal with the implementation as with C-strings (array of chars)
– No need to worry about size of string!
• The string class is defined in the string library and the names are in the standard namespace
– To use the string class you need these lines:
#include <string>
using namespace std;
Trang 26Using + With strings
• Variables of type string can be concatenated with the + operator
Trang 27string Constructors
• The default string constructor initializes a
string object to the empty string
• Another string constructor takes a C-string
Trang 28Comparing strings
For C Strings use strcmp
if (str1 == "Danang"){ …
Trang 29
Member Function length
• The string class member function length return the number of characters in the string object:
string str1 = "ABC ";
cout << str1.length(); // prints 3
• Is exactly the same as member function size()
• string.empty() is the same as string.length() == 0
str1 is an object (or instance) of class string
length() is a member function of class string
Trang 30• If you use int you will get compiler warnings:
for (int i = 0; i < str.length(); i++)
• Solution – use size_t instead:
for (size_t i = 0; i < str.length(); i++)
Trang 31Operator[] and the string Class
Operator[] works the same as for C-strings char arrays
Trang 32I/O With Class string
• The insertion operator << is used to output objects of type string
– Example: string s = "Hello Mom!";
cout << s;
• The extraction operator >> can be used to
input data for objects of type string
Example: string s1;
cin >> s1;
>> skips leading whitespaces and stops on
encountering more whitespace (reads one word)
Trang 33getline and Type string
• A getline function exists to read entire lines into a string variable
– This version of getline is not a member of the istream class, it is a non-member function
– Syntax for using this getline is different than that used with cin: cin.getline(…)
getline(Istream_Object, String_Object);
Trang 34cout << line << "END OF OUTPUT\n";
Output could be:
Enter some input:
Do be do to you!
Do be do to you!END OF OUTPUT
Trang 35Another Version of getline
• The versions of getline we have seen, stop reading at the end of line marker '\n'
• getline can stop reading at any character specified in the argument list
• This code stops reading when a '?' is read
Trang 36C++ string vs C-string
Old-style C-string
• pointer to character array, null-terminated ('\0')
• string functions are dangerous (strcpy, strncpy, )
• String literals are actually C-strings ("hello\n");
C++ string
• are objects of class string
• are much easier and safer to use than C-Strings
• have member functions such as:
length(); find(…); insert( ); replace( );
Trang 37Converting C++ string to C-string
Converting C-string to C++ string
• Happens automatically in most cases
string s = "abc";
• Can force using string("abc")
string s = "abc" + "def"; // compile error
string s = string("abc") + "def";
Converting C++ string to C-string
• Some older functionality requires use of C-string
• Use member function c_str() :
printf("%s", last_name.c_str());
For more information on string class see:
http://www.cplusplus.com/reference/string/string/
Trang 38Strings and Unicode
To store Unicode strings C++ defines the wstring type, which uses wchar_t as its character type
All wide string literals need to be prefixed with 'L'
Trang 39string::find()
string class implements a find function:
size_t find(string pattern, size_t start_position);
returns position where pattern found,
or the constant string::npos if not found
string str = "My name is Pam";
size_t found;
found = str.find("name"); // returns 3
found = str.find("am", 4); // returns 12
found = str.find("john"); // returns string::npos
There is also a similar string::rfind() function which searches
from the end of the string (reverse find)
:: means member function of class
Trang 40string::substr()
string substr(size_t pos, size_t n);
Returns the substring that starts at character position pos and has a length of n characters
If you omit the n parameter it means ‘to then end of the string’