The C - PowerPoint PPT Presentation

1 / 34
About This Presentation
Title:

The C

Description:

My name is Jim Anderson See: Example 1 Dr. Ahmad R. Hadaegh ... if str1 does not have numchars chars after. first, str2 just copies up to end of str1 ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 35
Provided by: ahad6
Category:
Tags: does | mean | my | name | what

less

Transcript and Presenter's Notes

Title: The C


1
The C String Class The contents of this
particular lecture prepared by the instructors at
the University of Manitoba in Canada and modified
by Dr. Ahmad Reza Hadaegh
2
The C String Class - Strings are defined in
the Standard Class Library - while not part of
the language, any standard compiler will
include this class in its library - This depends
on the compiler - writers - you have to rely on
the fact that they've properly implemented the
same string operations that everybody else
does - To obtain the String Class library
use include ltstringgt
3
The C String Class - To declare a variable of
type string include ltstringgt // declaring 3
variables of type string string play, author,
mainCharacter - We can assign to
strings play Hamlet author W.
Shakespeare mainCharacter play // assign to
string - Notice we dont worry about the
length!
4
String Class I/O - Do I/O on a string in a
natural way string name Jim
Anderson cout ltlt My name is ltlt name ltlt
endl - The output is My name is Jim
Anderson ltSee Example 1gt
5
String Class I/O - String class input cin gtgt
name // reads first word from input stream cout
ltlt name - Suppose user enter Marky Ramone
program prints Marky (not Marky Ramone) - gtgt
skips over whitespace characters, reads next
word (until next whitespace character) - Notice
the difference ltlt print out the entire
string gtgt read in the next (whitespace
delimited) word
6
String Class I/O - Often want to read the entire
line of text (up to and including the newline
character) - the string class has the getline
function getline(cin, name) // read next
line into name - if I type This is CS211
then string variable name will contain
This is CS211
7
String Class I/O - A simple program to copy one
file to another include ltstringgt include
ltfstreamgt int main() string line ifstream
infile ofstream outfile infile.open("infile.txt
") outfile.open("outfile.txt") getline(infile
,line) while (!infile.eof()) outfile ltlt
line ltlt endl // note need
endl getline(infile, line) infile.close()
outfile.close() ltSee Example 2gt
8
String Element Access - Access and modify
individual characters of a string, much like an
array of char - Example string line line
"This is a line of text" cout ltlt
line5 line2 'u' cout ltlt line ltlt endl -
Output iThus is a line of text
9
String Element Access - String bounds are
checked - access a string at an index beyond
the last character (or lt0) causes a run-time
error - Where is the end of the string? -
there is a size() function, which returns the
current length of the string
10
String Element Access - Example of "size"
function string nameAdam Smith" cout ltlt
name ltlt " has " ltlt name.size() ltlt " characters
in his name" ltlt endl - Output Adam Smith has
10 characters in his name - Characters are
stored at indices
11
Strings Dynamic Data - Strings are a dynamic
data type in that the length can be different
at different points in the program execution
string quote quote "I am a student at
CSUSM." cout ltlt quote.size() ltlt " " quote
"I am a student." cout ltlt quote.size() ltlt
endl - Output 24 15
12
Strings as Parameters - Strings behave
"normally" when used as parameters - can be
passed by value, or reference (with ) - unlike
C-style strings, or arrays of chars for that
matter, which are always passed by
reference - can still use size() inside a
function - the whole object gets passed, all
members are accessible - can be returned from
functions - unlike C-style strings which can't
be easily returned from functions -
Essentially, strings behave like ints when
used as parameters/return values
13
Strings as Value Parameters string
reverse(string line) int i char tmp for
(i 0 ilt line.size()/2 i) tmp
linei linei lineline.size()-1-i lin
eline.size()-1-i tmp return line
14
Strings as Value Parameters - Call this function
as follows string line1, line2 line1 This
is Adam Smith" line2 reverse( line1) cout ltlt
line1 ltlt endl cout ltlt line2 ltlt endl - The
output is This is Adam Smith htimS madA si
sihT - Note line1 does not change!
15
Strings as Reference Parameters - Use a
reference parameter void reverse2( string line)
int i char tmp for (i 0
iltline.size()/2 i) tmp linei linei
lineline.size()-1-i lineline.size()-1-i
tmp - line is now a reference parameter
16
Strings as Reference Parameters - Try out
reverse2 string line1, line2 line1"This is
Mr. Brown" cout ltlt line1 ltlt endl reverse2(line
1) cout ltlt line1 ltlt endl - Output This is
Mr. Brown nworB .rM si sihT ltSee Example 4gt
17
String Relational Operators - The string class
provides many operators and functions which
work with strings - Relational operators -
strings can be compared much like integers
using the operators , !, lt, lt, gt, gt - the
equality operator () and inequality operator
(!) work as you would expect.
18
String Operators Concatenation - Another
operator provided by the string class library
is concatenation using string word1, word2,
word3, line"I " word1"am " word2an "
word3"instructor in this college" line line
word1 word2 word3 cout ltlt line ltlt " " ltlt
line.size() ltlt endl - Output I am an
instructor in this college 34 - Notice the
string line grew dynamically! ltSee Example 5gt
19
String Operators Concatenation - This is an
example of operator overloading - operator
is used for ints, floats, and strings - has a
different meaning in each case - operator
for strings is actually not part of C - it is
part of the C class library ltstringgt - you
can (in the future) define your own meanings
for various operators
20
"Shortcut" operators - C provides a series of
shorthand operators for performing an operation
with the same variable you intend to assign
the result to - Instead of Try x x y
x y x x 2 x 2 - and so
on. - Feel free to use these!
21
Overloading Shortcuts - The reason I bring this
up is that is overloaded for strings as
well - you can write line word1 word2
word3 - same meaning as line line word1
word2 word3 - The others don't make sense for
strings, but you may see them overloaded for
other types - and as already stated possible
to overload them yourself
22
String Member Functions - The string class
provides a number of useful member functions -
Object-Oriented-Programming (OOP) - recall that
member functions of a class are functions
which operate on objects of that class (really
part of the object!) - sometimes called
"Methods - notation is VariableName.
FunctionName(...) - the function FunctionName
operates on the object (string)
VariableName - may have some other parameters
as well (e.g. fin.open() for file streams)
23
String Member Functions - Substrings - the
string class provides the substr member
function to extract substrings from a string -
Specification string str1, str2 str2
str1.substr( first, numchars) - str2 is
assigned the substring of str1 starting at
position first and containing numchars
characters - if str1 does not have numchars
chars after first, str2 just copies up to end
of str1
24
String Member Functions - Substrings
(continued) - Example string line"This
course is not that hard guys" string line2,
line3 line2 line.substr(5,6) line3
line.substr(15,8) cout ltlt "line 2 is \""ltlt
line2 ltlt"\""ltlt endl cout ltlt "line 3 is \"" ltlt
line3 ltlt "\""ltlt endl Output line 2 is
course line 3 is not that ltSee Example 6gt
25
String Member Functions - Substring
replacement - The substr function does not
modify its object - If you want to change a
substring, use the replace function -
Specification string str1, str2 str1.repla
ce( first, numchars, str2) - Action -
replace chars in str1, starting at index first
and extending numchars positions, with the
string str2.
26
String Member Functions - Example string
line"Let us now replace some text." line.replac
e( 4, 10, "it snow. P") cout ltlt line ltlt
endl - Output Let it snow. Place some
text. - Note that "it snow. P" contains 10 chars
and we replaced 10 chars. This is not always
necessary ltSee Example 7gt
27
String Member Functions - Can replace substrings
with shorter or longer substrings - the length
of the string will change - Example
string line"Let us now replace some text."
cout ltlt line.size() ltlt line.replace(4,
19, "it snow") cout ltlt line ltlt ltlt
line.size() ltlt endl Output 30 Let it
snow text. 18
28
String Member Functions - Two other member
functions closely related to replace are erase
and insert. - these functions can be done with
replace - more convenient and suggestive to use
erase and insert when appropriate
29
String Member Functions - Erase -
Specification string line line.erase(
start, numchars) - delete numchars chars
starting at index start - functionally same
as line.replace( start, numchars, "") -
"" is the null string (length 0)
30
String Member Functions - Insert -
Specification string line,
instr line.insert( start, instr) - insert
string instr into line, starting at index
start - don't overwrite any characters in line
just shift to the right - functionally same
as line.replace( start, 0, instr) -
replace 0 characters of line, starting at
position start, with instr
31
String Member Functions - Examples string
line"Walking on the sun is hot" line.erase(15,4
) cout ltlt line ltlt endl line.insert(15,"moon
") cout ltlt line ltlt endl - Output Walking on
the is hot Walking on the moon is hot ltSee
Example 8gt
32
String Search Functions - The string class also
offers a very useful searching function
find - Specification string line,
pattern int pos, x ... x line.find(
pattern, pos) - x will contain the first
location of pattern within line starting at or
after position pos or line.npos (a special
constant) if pattern not found in line at or
after position pos
33
String Search Functions - Example string line"
This and that and more and less" int p p
line.find( "and" ,0) // search, start at pos'n
0 while (p ! line.npos) cout ltlt "Found an
\" and\" at index " ltlt p ltlt endl //
continue looking at location p 1 p
line.find("and", p1) Output Found an and
at index 6 Found an and at index 15 Found an
and at index 24 ltSee Example 9gt
34
  • Makes Sure to Study all the examples for this
    lecture.
  • Specially, examples 10 and 11 are very important.
    Similar questions may appear in the labs,
    assignments and exams
Write a Comment
User Comments (0)
About PowerShow.com