0% found this document useful (0 votes)
5 views44 pages

7 CPP String

Presentation for C++ Strings

Uploaded by

drocsit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views44 pages

7 CPP String

Presentation for C++ Strings

Uploaded by

drocsit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 44

Lecture 7: String

Harbin Institute of Technology ( Shenzhen )


School of Computer Science and Technology
Zhang Meishan ([email protected])
Copyright : Harbin Institute of Technology , Su Xiaohong , [email protected]
High-Level Language Programming ( 高级语言程序设计 )
Overview

C-strings

C-string input and output

C-string functions

C++ strings

Character classification

High-Level Language Programming ( 高级语言程序设计 )


7.1 C-strings

C-string : a string is an array of characters (elements of type char) with
the null character '\0' in the last element of the array

string literal
• A sequence of characters enclosed
in double quotation marks
• It is not necessary to specify the
number of characters
The string literal "Hello“
actually contains 6, • The compiler automatically inserts
rather than the null character '\0' after the last
5,characters. character of a string literal

High-Level Language Programming ( 高级语言程序设计 )


7.1 C-strings

If specify the size of the array and the string is shorter than this size, the
remaining elements of the array are initialized with the null character '\
0'.


To include a double quote inside a string precede the quote with a back
slash (\).

High-Level Language Programming ( 高级语言程序设计 )


7.1 C-strings

The newline ('\n') escape sequence can be used in place of endl to
advance to a new line.


If a string is too long to fit onto a single program line, it can be broken
up into smaller segments.

High-Level Language Programming ( 高级语言程序设计 )


7.2 C-string input and
output

Remember that the number of elements in the char array must be one
more than the number of characters in the string.

Program Example: a simple demonstration of C-string input and output.

• The extraction operator >> read the


characters up to ( but not including)
the space character after John.
High-Level Language Programming ( 高级语言程序设计 )
7.2 C-string input and
output

If the user does types in more than 10 characters, what will
happen?

The array first_name will overflow, the excess characters will
overwrite other areas of memory, and the program will probably
malfunction.

getline() function

To allow for this possibility and also to allow for whitespace
characters in the input.

High-Level Language Programming ( 高级语言程序设计 )


7.2 C-string input and
output

Program Example getline()
• reads characters from cin until
either the user presses the key '\n' or
10 characters have been read
• '\n' is called the delimiter, when the
delimiter is omitted, it is assumed to
be '\n‘.
• '\0' is automatically added to the end
of a string

High-Level Language Programming ( 高级语言程序设计 )


7.2 C-string input and
output

Program Example

This line seems to be skipped and no


name is read in. Why?
• Line 13 stops reading into the
numeric variable student_number as
soon as a non-numeric character is
read.
• Enter key is pressed after typing
12345 is left in the input stream,
High-Level Language Programming ( 高级语言程序设计 ) which is then read by getline() on line
7.2 C-string input and
output

Program Example: How to correctly read data into student_name
Solution
Solution
2: 1:
discard
Read ortheignore
newline
all
characters
characterin'\n‘
theinto
input
a ‘dummy’
stream up to
andcharacter
including the
newline
variable.
character '\
n'.However, if an
extra space is
entered after the
student number,
the space is read
into the variable
dummy, and '\n' is
left in the input
High-Level Language Programming ( 高级语言程序设计 )
stream.
7.2 C-string input and
output

Program Example: display each character of the string "Hello" on
separate lines
Solution:
• Each character of a C-
string can be accessed
using an index

High-Level Language Programming ( 高级语言程序设计 )


7.3 C-string functions

C++ has inherited a library of C-string functions

How to use these functions?


Finding the length of a C-string
strlen(): returns the number of characters in a C-string, excluding the
null character '\0'.

High-Level Language Programming ( 高级语言程序设计 )


7.3 C-string functions

Copying a C-string

strcpy( destination, source )

copies the contents of a C-string source to another C-string destination.

Remark:
•A '\0' must be at the end
of the string.
•‘destination’ string is
assumed big enough to
hold the string being
copied to it.

High-Level Language Programming ( 高级语言程序设计 )


7.3 C-string functions

C-string concatenation

strcat( str1, str2 )

concatenates a C-string str2 to the end of the C-string str1


Both str1 and str2 must be null-terminated

Enough memory must be allocated to str1 to hold the result of the
concatenation

High-Level Language Programming ( 高级语言程序设计 )


7.3 C-string functions

Comparing C-strings

strcmp( str1, str2 )

compares two null-terminated C-strings str1 and str2.

This function returns:

a negative value, if str1 < str2,

0 , if str1 == str2,

a positive value, if str1 > str2.

High-Level Language Programming ( 高级语言程序设计 )


7.3 C-string functions

Program Example

High-Level Language Programming ( 高级语言程序设计 )


7.3 C-string functions

strncat( str1, str2, n )

Appends the first n characters of the C-string str2 to the C-string
str1.

strncmp( str1, str2, n )

Identical to strcmp( str1, str2 ), except that at most, n characters are
compared.

strncpy( str1, str2, n )

Copies the first n characters of str2 into str1.

High-Level Language Programming ( 高级语言程序设计 )


7.3 C-string functions

Converting numeric C-strings to numbers

The storage of a numeric C-string: in the ASCII representation

"123“


The storage of an integer value: in binary

123

High-Level Language Programming ( 高级语言程序设计 )


7.3 C-string functions

atoi() : C-string to an integer

atol() : C-string to a long integer

atof() : C-string to a double float.
Remark:
• Ignore any leading whitespace
characters
• Stop converting when a character
that cannot be part of the number
is reached.
• atoi() will stop when it reaches a
decimal point
• atof() will accept a decimal point,
High-Level Language Programming ( 高级语言程序设计 )
because it can be part of a decimal
7.3 C-string functions
Function Remark

strlen(str) Finding the length of a C-string str

strcpy(str1, str2) copies the contents of a C-string str2 to str1

strcat( str1, str2 ) concatenates a C-string str2 to the end of the C-string str1

strcmp( str1, str2 ) compares two null-terminated C-strings str1 and str2.

strncat( str1, str2, n ) Appends the first n characters of the C-string str2 to str1.

strncmp( str1, str2, n ) Identical to strcmp, except that at most n characters are compared.

strncpy( str1, str2, n ) Copies the first n characters of str2 into str1.
High-Level Language Programming ( 高级语言程序设计 )
7.4 C++ strings

Common errors in C-strings:

Attempting to access elements outside the array bounds

Not using the function strcpy() to one C-string to another

Not using strcmp() to compare two C-strings.



C++ strings

The string data type is not built into C++

In C++, the string data type is defined by a class
High-Level Language Programming ( 高级语言程序设计 )
7.4 C++ strings

Program Example
Remark:
• C++ strings are compared using
==, <, >,etc instead of
strcmp() in C-strings.
• C++ has many useful string
member functions.

High-Level Language Programming ( 高级语言程序设计 )


7.4 C++ strings

string initialization and assignment • str2 shows how to
assign a C++ string
with a number of
identical characters
• The string that is
being assigned can
be on two or more
lines.
• str5 is called an
empty string.

High-Level Language Programming ( 高级语言程序设计 )


7.4 C++ strings

string initialization and assignment

line33 assign(): assign part of str1 to str5.


• str1 is the string to assign from
• 1 is the starting position
• 3 is the number of characters to assign

High-Level Language Programming ( 高级语言程序设计 )


7.4 C++ strings

string swap

High-Level Language Programming ( 高级语言程序设计 )


7.4 C++ strings

string concatenation

High-Level Language Programming ( 高级语言程序设计 )


7.4 C++ strings

string length, string indexing and sub-strings
• Change the first and last
characters
• Check the index value to
ensure it is not out of range by
at().

High-Level Language Programming ( 高级语言程序设计 )


7.4 C++ strings

• substr() can extract


part of a C++ string.
• The 1st argument is a
starting position
• 2nd argument is the
number of characters
High-Level Language Programming ( 高级语言程序设计 ) to extract.
7.4 C++ strings

string replace, erase, insert and empty strings

• Character at position 0 is the


first character.
• Replace 3 characters from str1
starting at the 2nd character
position with 4 characters from
str2, starting at the 3rd
character position.

High-Level Language Programming ( 高级语言程序设计 )


7.4 C++ strings

string replace, erase, insert and empty strings

• Erase from the 10th character position to the


end of str1.

High-Level Language Programming ( 高级语言程序设计 )


7.4 C++ strings
• Erase 2
characters
starting at the
5th character
• position.
Erase the
entire string.

• Judge
whether str1
is empty?
• Starting at
the 2nd
character of
str2, insert 6
characters at
the 5th
character
High-Level Language Programming ( 高级语言程序设计 ) position of
7.4 C++ strings

string searching
• Hold the position of the first
occurrence of "CDE" in str1. If
"CDE" is not in str1, p = -1.

• Reverse find the last occurrence


of "CDE".

• Find the first occurrence of any


one of a number of characters.

High-Level Language Programming ( 高级语言程序设计 )


7.4 C++ strings

string searching
• Find the last occurrence of any
one of a number of characters.

• Find the first occurrence of any


character that is not one of a
number of characters.

• Find the last occurrence of any


character that is not one of a
number of characters.

High-Level Language Programming ( 高级语言程序设计 )


7.4 C++ strings

string comparisons
• C++ strings can be
compared with the
standard comparison
operators : ==, !=, <=,
>= < and >
• result is < 0 if the first
differing character in str1
is less than the character
in the same position in
str2.
• result is 0 if all the
characters of str1 and
str2 are equal and the
two strings are the same
length.
High-Level Language Programming ( 高级语言程序设计 ) • Otherwise result is > 0.
7.4 C++ strings

string comparisons
• Compares a 3-
character substring
of str1 starting at
the second
character with all
the characters of
str2.

High-Level Language Programming ( 高级语言程序设计 )


7.4 C++ strings

Just like any other data
type, arrays of C-strings
and arrays of C++ strings
can be defined. Arrays of
C++ strings are much
easier to work with.

High-Level Language Programming ( 高级语言程序设计 )


7.5 Character
classification

There are a number of C++ functions that can be used to test the value
of a single character. These functions return a true (non-zero integer)
value or a false (zero integer) value depending on whether or not the
character belongs to a particular set of characters.

High-Level Language Programming ( 高级语言程序设计 )


7.5 Character
classification

C++ has also two further functions that are used to covert the
case of a character: tolower and toupper.

High-Level Language Programming ( 高级语言程序设计 )


7.5 Character
classification
A sample run of this program:

High-Level Language Programming ( 高级语言程序设计 )



Q&A
ZhangMei
Shan

High-Level Language Programming ( 高级语言程序设计 ) 40/37


Homework

1. What is the output from the following program
segment?

High-Level Language Programming ( 高级语言程序设计 )


Homework

2. Modify exercise 1 to use C++ strings rather than C-
strings.

3. Given the following:
char c_str1[18] ;
char c_str2[6] = " abcde " ;

what is in c_str1 after each of the following?


(a ) strcpy( c_str1, " A string " ) ;
(b) strcat( c_str1, " of text. " ) ;
(c) strncpy( c_str1, c_str2, 1 ) ;
High-Level Language Programming ( 高级语言程序设计 )
Homework

4. Write a program to input a C-string from the keyboard
and replace each space in the string with the character '_'.

5. What is the output from the following program
segment?

High-Level Language Programming ( 高级语言程序设计 )


Homework

6. Write a program to input a string. If every character in
the string is a digit ('0' to '9'), then convert the string to an
integer, add 1 to it, and display the result. If any one of the
characters in the string is not a digit, display an error
message.

High-Level Language Programming ( 高级语言程序设计 )

You might also like