You are on page 1of 40

CHARACTER ARRAY STRINGS – C

TYPE STRINGS
STRING CLASS – C++ STRINGS
COMSC 110
10.1
Character Testing
Character Testing

require cctype header fle
FUNCTION MEANING
isalpha
true if arg. is a letter, false otherwise
isalnum
true if arg. is a letter or digit, false otherwise
isdigit
true if arg. is a digit 0-9, false otherwise
islower
true if arg. is lowerase letter, false otherwise
isprint
true if arg. is a !ri"ta#le harater, false otherwise
ispunct
true if arg. is a !$"t$atio" harater, false otherwise
isupper
true if arg. is a" $!!erase letter, false otherwise
isspace
true if arg. is a whites!ae harater, false otherwise
From Program 10-1
10.2
Character Case Conversion
Character Case Conversion

equire cctype header fle

Functions!
toupper! i" char argument is lo#ercase letter$
return u%%ercase equivalent& other#ise$ return
in%ut unchanged
char ch1 = 'H';
char ch2 = 'e';
char ch3 = '!';
cout << toupper(ch1); displays 'H'
cout << toupper(ch2); displays '!'
cout << toupper(ch3); displays '!'
Character Case Conversion

Functions!
tolower! i" char argument is u%%ercase letter$
return lo#ercase equivalent& other#ise$ return
in%ut unchanged
char ch1 = 'H';
char ch2 = 'e';
char ch3 = '!';
cout << tolower(ch1); displays 'h'
cout << tolower(ch2); displays 'e'
cout << tolower(ch3); displays '!'
10.'
C-Strings
C-Strings

C-string! sequence o" characters
stored in ad(acent memor) locations
and terminated *) "#$$ character

String literal +string constant,!
sequence o" characters enclosed in
dou*le quotes - - ! %Hi
there!%
H i t h e r e ! &'
C-Strings

.rra) o" chars can *e used to defne
storage "or string!
const int ()*! = 2';
char city+()*!,;

/eave room "or "#$$ at end

Can enter a value using cin or --
%
0n%ut is #hites%ace-terminated
%
1o chec2 to see i" enough s%ace

For in%ut containing #hites%ace$ and to
control amount o" in%ut$ use
cin.getline()
10.3
/i*rar) Functions "or 4or2ing
#ith C-Strings
/i*rar) Functions "or 4or2ing #ith
C-Strings

equire the cstring header fle

Functions ta2e one or more C-strings
as arguments. Can use!
%
C-string name
%
%ointer to C-string
%
literal string
/i*rar) Functions "or
4or2ing #ith C-Strings
Functions!
%
strlen(str)! returns length o" C-string
str
char city+()*!, = %/issoula%;
cout << strlen(city); prints 0
%
strcat(str11 str2)! a%%ends str2 to
the end o" str1
char location+()*!, = %/issoula1 %;
char state+3, = %/2%;
strcat(location1 state);
location now has %/issoula1 /2%
/i*rar) Functions "or
4or2ing #ith C-Strings
Functions!
%
strcpy(str11 str2)! co%ies str2 to
str1
const int ()*! = 2';
char fname+()*!, = %/aureen%1 name+()*!,;
strcpy(name1 fname);
1ote! strcat and strcpy %er"orm no
*ounds chec2ing to determine i" there is
enough s%ace in receiving character arra)
to hold the string it is *eing assigned.
C-string 0nside a C-string
Function!
%
strstr(str11 str2)! fnds the frst
occurrence o" str2 in str1. eturns a
%ointer to match$ or "#$$ i" no match.
char ri3er+, = %4a5ash%;
char word+, = %a5a%;
cout << strstr(state1 word);
displays %a5ash%
10.5
C-String61umeric Conversion
Functions
String61umeric Conversion
Functions

require cstdli5 header fle
FUNCTION &A'AMETE' ACTION
atoi
C-stri"g o"(erts C-stri"g to a" int (al$e, ret$r"s the (al$e
atol
C-stri"g o"(erts C-stri"g to a long (al$e, ret$r"s the (al$e
atof
C-stri"g o"(erts C-stri"g to a dou5le (al$e, ret$r"s the (al$e
itoa
int1C-stri"g, int o"(erts )
st
int !ara*eter to a C-stri"g, stores it i" +
"d
!ara*eter. ,
rd

!ara*eter is #ase of o"(erted (al$e
String61umeric Conversion
Functions
int i"um;
long l"um;
dou5le d"um;
char int6har+1',;
i"um = atoi(%1237%); puts 1237 in i"um
l"um = atol(%89:0%); puts 89:0 in l"um
d"um = atof(%38.:%); puts 38.: in d"um
itoa(i"um1 int6har1 0); puts the string
%2322% (5ase 0 for 1237
1') in int6har
String61umeric Conversion
Functions - 1otes

i" C-string contains non-digits$ results
are undefned
%
"unction ma) return result u% to non-
digit
%
"unction ma) return 0

itoa does no *ounds chec2ing 7
ma2e sure there is enough s%ace to
store the result
10.8
4riting 9our O#n C-String
:andling Functions
4riting 9our O#n C-String
:andling Functions

;esigning C-String :andling
Functions
%
can %ass arra)s or %ointers to char
arra)s
%
Can %er"orm *ounds chec2ing to ensure
enough s%ace "or results
%
Can antici%ate une<%ected user in%ut
From Program 10-=
From Program 10-10
10.>
More .*out the C?? string
Class
The C?? string Class

S%ecial data t)%e su%%orts #or2ing #ith
strings

;include <string-

Can defne string varia*les in %rograms!
string first"ame1 last"ame;

Can receive values #ith assignment
o%erator!
first"ame = %<eorge%;
last"ame = %4ashington%;

Can *e dis%la)ed via cout
cout << first"ame << % % << last"ame;
0n%ut into a string O*(ect

@se cin -- to read an item into a
string!
string first"ame;
cout << %!nter your first name= %;
cin -- first"ame;
0n%ut into a string O*(ect

@se getline "unction to %ut a line o"
in%ut$ %ossi*l) including s%aces$ into
a string!
string address;
cout << %!nter your address= %;
getline(cin1address);
string Com%arison

Can use relational o%erators directl) to com%are
string o*(ects!
string str1 = %<eorge%1
str2 = %<eorgia%;
if (str1 < str2)
cout << str1 << % is less than %
<< str2;

Com%arison is %er"ormed similar to strcmp
"unction. esult is true or false
Other ;efnitions o" C?? strings
Definition Meaning
string name;
defi"es a" e*!t- stri"g o#.et
string myname(%6hris%);
defi"es a stri"g a"d i"itiali/es it
string yourname(myname);
defi"es a stri"g a"d i"itiali/es it
string aname(myname1 3);
defi"es a stri"g a"d i"itiali/es it with first , haraters of myname
string 3er5(myname1312);
defi"es a stri"g a"d i"itiali/es it with + haraters fro* myname starti"g at
!ositio" ,
string noname('>'1 8);
defi"es stri"g a"d i"itiali/es it to 0 '>'s
string O%erators
O&E'ATO' MEANING
--
e1trats haraters fro* strea* $! to whites!ae, i"sert i"to stri"g
<<
i"serts stri"g i"to strea*
=
assig"s stri"g o" right to stri"g o#.et o" left
?=
a!!e"ds stri"g o" right to e"d of o"te"ts o" left
?
o"ate"ates two stri"gs
+,
refere"es harater i" stri"g $si"g arra- "otatio"
-1 -=1 <1
<=1 ==1 !=
relatio"al o!erators for stri"g o*!ariso". 'et$r" true or false
string O%erators
string word11 phrase;
string word2 = % @og%;
cin -- word1; user enters %Hot 2amale%
word1 has %Hot%
phrase = word1 ? word2; phrase has
%Hot @og%
phrase ?= % on a 5un%;
for (int i = '; i < 19; i??)
cout << phrase+i,; displays
%Hot @og on a 5un%
string Mem*er Functions

.re *ehind man) overloaded o%erators

Categories!
%
assignment! assign1 copy1 data
%
modifcation! append1 clear1 erase1
insert1 replace1 swap
%
s%ace management! capacity1 empty1
length1 resiAe1 siAe
%
su*strings! find1 su5str
%
com%arison! compare

See Ta*le 10-> "or a list o" "unctions
string Mem*er Functions
string word11 word21 phrase;
cin -- word1; word1 is %Hot%
word2.assign(% @og%);
phrase.append(word1);
phrase.append(word2); phrase has %Hot @og%
phrase.append(% with mustard relish%1 13);
phrase has %Hot @og with mustard%
phrase.insert(01 %on a 5un %);
cout << phrase << endl; displays
%Hot @og on a 5un with mustard%
eading .ssignment

ead the entire cha%ter 10
2-30