Está en la página 1de 22

More on C++ strings

CSIS1117 Computer Programming

Contents

Comparing C++ strings


Simple data type char
Member functions provided by C++ strings
Build-in function getline(), getchar()

c1117 lecture 5

C++ String

Recall that: C++ has a string library that provides


more convenient ways to handle texts.

To use the string, we need to include the header:


#include <string>
We can declare string variables to store string values
We can assign a string constant, or the value of
another string to a string.
We can concatenate two strings by the operator "+"

string str1 = "I loves";


II loves
loves HKU
HKU :->
:->
string str2 = HKU";
cout << str1 + str2 + " :->" << endl;
c1117 lecture 5

C++ strings can also be compared


lexicographically (dictionary order) by relational
operators. See name.cc & monthdays.cc
string str1 = "Apple"; string str2 = "apple";
string str3 = "apples"; string str4 = "orange";
bool t1 = (str1 == str2);
bool t2 = (str1 < str2);
bool t3 = (str2 < str3);
t1
t1 :: false
false
bool t4 = (str3 != str4);
t2
:: true
t2
true
bool t5 = (str4 > str3);
t3
t3 :: true
true
t4
t4 :: true
true
t5
t5 :: true
true

c1117 lecture 5

Never compare two string literals directly!

At least one of the operands must be a C++ string.


See string-cmp.cc
if("zebra" < "ant")
cout << "Yes" << endl;
else
cout << "No" << endl;

It is a valid expression, no error message is given, but


unexpected result is gained, we cant explain it at this moment.

c1117 lecture 5

In fact, a string is composed of a sequence of


characters.
Characters in a string can be specified by their
positions.

For a string with length x, the positions of its


characters are ranging from 0 to x-1

Given a string variable, we can access an


individual character of it using the subscript
operator[] (square bracket).

c1117 lecture 5

string myMsg = "How are you doing?";


cout << myMsg[4]<< " " << myMsg[12]
aa dog
dog
<< myMsg[13] << myMsg[16] << endl;

The string consists of 18 characters ranging from


position 0 to 17.

cout << myMsg[18] << endl;

Error message may be given if the program


accesses a character outside the range of a string.
c1117 lecture 5

Simple data type (char)

Can we change the characters in a string?

string myMsg = "How are you doing?";


myMsg[4] = "d";

It is wrong, you will get a type mismatch error.

myMsg is of type string but myMsg[4] is just a


character!
A character is a single letter, digit, punctuation, or
symbol (mostly) can be seen on the keyboard.

c1117 lecture 5

Simple data type (char)

In C++ language, we use data type char to


represent characters.
char : representing value of one character

Character constants are enclosed by a pair of single


quotes, e.g. 'A', 'a', '+', '!', '9',
Non-printable characters, e.g. '\n', '\t',
Uppercase and lowercase of letters are considered to be
different.

c1117 lecture 5

Simple data type (char)


int Ival = 100;
bool Bval = true;
string Sval = "100";
char Cval = '1';

//
//
//
//

any integers
or false
use double quotes
use single quotes

How to represent the following in program?


Constant value 4

Constant string 4

"4"

Constant char 4 '4'


Print a new line

cont << "\n";

cont << endl;

cont << '\n';

c1117 lecture 5

10

Simple data type (char)

Input/output operations are similar to int and


double.
It can be compared using relational operators.

cout
cout <<
<< "continue?
"continue? [y/n]
[y/n] "" <<
<< endl;
endl;
char
char choice;
choice;
cin
cin >>
>> choice;
choice;
cout
cout <<
<< "your
"your choice
choice is
is "" <<
<< choice
choice <<endl;
<<endl;
if(choice
if(choice ==
== 'n'){
'n'){
cout
cout <<
<< "The
"The process
process is
is terminated!"
terminated!" <<
<< endl;
endl;
}}

See greeting.cc as an example

c1117 lecture 5

11

We can change the characters in a string as follow:


string myMsg = "How are you doing?";
myMsg[0] = 'h';
cout << myMsg << endl;
myMsg[9] = '3';
cout << myMsg << endl;
how
how
how
how

c1117 lecture 5

are
are
are
are

you
you
y3u
y3u

doing?
doing?
doing?
doing?

output

12

Member functions of string

The string library is very flexible for handing


operations related to string/text manipulation.

e.g. finding the length of a string, comparing two strings,


searching/extracting of sub-string, concatenation,

Each string variable is associated with a number


of member functions, e.g. s.length(), s.substr()

Member functions are applied to individual variable


using dot operator.
Unlike the predefined functions provided in library, e.g.
sqrt(), pow(), member functions cannot be invoked
without a variable.

c1117 lecture 5

13

Member functions of string

s.length() return the no. of characters in the


string

string s = "Hello World!";


unsigned int len_s = s.length();
cout << "length of string s: " << len_s
<< endl;
length
length of
of string
string s:
s: 12
12
What is unsigned int ?
The member function is associated to a variable, we can
use the dot operator to invoke it.
c1117 lecture 5

14

unsigned int

There are two types of int: signed and unsigned.


If neither is specified the int is signed.

signed int: ranging from -231 to 231-1 inclusively (in


most 32-bit CPU).
unsigned int: only consider non-negative integers,
ranging from 0 to 232-1 inclusively.

As the length of a string must be a non-negative


number, we should use an unsigned int to
represent it.
unsigned int len_s = s.length();

c1117 lecture 5

15

unsigned int

An overflow occurs when the result of an


arithmetic operation exceeds the representable
range.

In C++, no warning or error message will be given for


integer overflow, but incorrect result may obtained.
unsigned int x = pow(2,31) + 2;
int y = -1;
What result you will get?
if(x > y)
cout << "Yes" << endl;
else
No
cout << "No" << endl;
No

c1117 lecture 5

16

What is the shortest length of a string?

An empty string is a string containing zero no. of


character.
e.g. string s = ""

How about string r = " " ? Is it still an empty string?


No, r is a string containing one character the space

See string-ex.cc as an example

c1117 lecture 5

17

Member functions of string

A substring is part of a string. Substrings can be


extracted using the member function s.substr().

s.substr(x, y) extracting a substring with length


y starting at position x.
If the second argument y is omitted, a substring up to
the end of the string is extracted.
string s (attending lecture");
string r = s.substr(0,6);
string t = s.substr(11);
string u = s.substr(11,99);
cout << r << endl << t << endl
<< u << endl;

attend
attend
lecture
lecture
lecture
lecture

See substr.cc as an example

c1117 lecture 5

18

Member functions of string

The member function s.find() can be used to


look for the occurrence of one string in another.

s.find(r) checking if string r occurs in the string s.


The starting position of the first occurrence is reported.
If there is no such occurrence, the constant
string::npos defined in <string> is returned.
a=
a= 33
not
not aa substring!
substring!

string r = "programming";
int a = r.find("gram");
cout << "a= " << a << endl;
if(r.find("job") == string::npos)
cout << "not a substring!" << endl;

See use-find.cc as an example.

c1117 lecture 5

19

Reading a line from input

We use cin >> v1 >> v2 >> >> vn to get data


from input.
cin makes use of whitespace characters as interdata separator. So cin will skip all the whitespaces
you typed. See cin-input.cc as an example.
As a result, we cannot read a string involving
space character into a string variable.

c1117 lecture 5

20

Instead, we can use the library function


getline() to read a line from the current
position till the end of the current line.
See read-a-line.cc for an example.
string s;
getline(cin, s);
cout << "the string is: " << s << endl;
Hello
Hello World!
World!
the
the string
string is:
is: Hello
Hello World!
World!

c1117 lecture 5

21

Reading characters from input

Similarly, if we want to read characters one by one


from input, including the whitespace characters,
we can use the library function getchar().

char a, b, c;
cout << "please input some characters: ";
a = getchar(); b = getchar(); c = getchar();
cout << "The first 3 characters are: "
<< a << "," << b << ", and " << c << endl;
please
please input
input some
some characters:
characters: II am
am
The
The first
first 33 characters
characters are:
are: I,
I, ,, and
and aa A space is
also treated
See read-a-char.cc as an example. as a character
c1117 lecture 5

22

También podría gustarte