Title: Lecture 2 Strings in C
1Lecture 2Strings in C
A C-style string is an array of characters
(char), terminated with an extra character, the
null character \0.
char greet_msg How are you doing? char
greet_msg19 How are you doing? char
greet_msg40 How are you doing? char
greet_msg How are you doing? char
greet_msg19 H,o,w, , a,r,e,
,y,o,u, , d,o,I,n,g,?,\0
2C Style Strings
include ltstringgt string msg1string msg2
Bravostring msg3 msg2string msg4(10, z)
zzzzzzzzzz
In some context the old C-style strings might be
required!
string filename punts.datifstream
infileinfile.open( filename.c_str() )
3(1) String Length
string s Ed Woodcout ltlt Length ltlt
s.length() ltlt \n
(2) Output of Strings
string msg1string msg2 Bravostring msg3
msg2string msg4(10, z) cout ltlt msg1 ltlt
\n ltlt msg2 ltlt \n ltlt msg3 ltlt \n
ltlt msg4 ltlt \n
4(3) Input of Strings
string scout ltlt Enter a string cin gtgt s
s is the string Ed !
The default action of the input operator gtgt is to
skip white space and then read and store
characters until end-of-file or another
white-space character is read. No white space is
stored.
5getline(istream, string)
- getline reads characters from the input stream,
storing them - in the string variable until
- End-of-file is reached.
- A newline is read, in which case the newline is
removed from the stream but it is not stored in
the variable. - The maximum allowable size for a string is
reached (typically 4,294,967,295 232 1).
If getline reads no characters from the input
stream, it returns false. Otherwise it returns
true.
6include ltiostreamgt include ltfstreamgt include
ltstringgt using namespace std int main()
string buff ifstream infile ofstream
outfile cout ltlt Input file name
cin gtgt buff infile.open( buff.c_str() )
cout ltlt Output file name cin gtgt buff
outfile.open( buff.c_str() ) while (
getline(infile, buff) ) outfile ltlt buff
ltlt \n\n infile.close() outfile.close()
return 0
Example 4
7(4) Assignment of Strings
string str1, str2char cstyle_str3
Hi str1 assignment is easystr2
str1str2 cstyle_strstr2 z
(5) Concatenation of Strings
string str1 Ed , str2 Wood, str3str3
str1 str2str1 str2
str3 and str1 become Ed Wood
8(6) Modifying Strings
erase removes a substring from a stringinsert
inserts a string into another one at a
specified positionreplace replaces a substring
with a specified stringswap swaps two strings
string s The door is closed s.erase(9,3)cou
t ltlt s ltlt \n
The door closed
9string s1 The door is closed string s2
not s1.insert(12, s2)cout ltlt s1 ltlt
\n s1.erase(12, 4) s1.replace(12,
6,open)cout ltlt s1 ltlt \ns2.erase()
s2.insert(0, Hi!)s2.swap(s1) cout ltlt s1 ltlt
\n ltlt s2 ltlt \n
The door is not closed The door is open Hi! The
door is open
10string s The door is closed s4
rs6 as7 d cout ltlt s ltlt
\ncout ltlt s1 ltlt s9 ltlt \n
The road is closed hi
an operator used to reference a character in
a string at a specified index
11(7) Extracting Substring
string s1 The door is closed string s2s2
s1.substr(4,4)cout ltlt s1 ltlt \ncout ltlt s2 ltlt
\n
The door is closed door
- If the first argument is in-bounds, the length of
the substring is - the smaller of
- The value of the second argument
- The strings length minus the value of the first
argument
12(8) Searching
s1.find( s2, ind )
If s2 is a substring of s1 at index ind or
higher, find returns the smallest index ? ind
where s2 begins. Otherwise find returns plus
infinity (typically 232 1).
If a second argument is not supplied, it defaults
to 0.
string s1 The door is closed string s2
doorcout ltlt s1.find(s2) ltlt \ncout ltlt
s1.find(s2, 5) ltlt \n
4 4294967295
13s1.rfind( s2, ind )
Returns the largest index ? ind where s2 begins,
or plus infinity if s2 is not found.
If a second argument is not supplied, it defaults
to plus infinity.
string s1 the left the right string s2
thecout ltlt s1.find(s2) ltlt \ncout ltlt
s1.rfind(s2) ltlt \n cout ltlt s1.find(s2,5) ltlt
\n cout ltlt s1.rfind(s2,8) ltlt \n cout ltlt
s1.rfind(s2,9) ltlt \n
0 9 9 0 9
14(9) Comparing Strings
Alphabetical order
bus carfest fistgraph
grapecartoon carsix silence
lt
lt
gt
gt
gt
The operators , !, lt, lt, gt and gt can be used
for string comparison. The left- and right-hand
sides can both bestrings, or one can be string
and the other a C-style string.
15Exercise 5. Write a complete C program that
reads lines until end-of-file from the file
names.dat, replaces each occurrence of a letter
a, o, u, e, i by and writes the
names with at least two stars into the file
stars.dat.
HelenMichelleMarkKenBrianAndrewThomas
Hln MchllBrnndrwThms
stars.dat
names.dat