0% found this document useful (0 votes)
34 views

C++ Week 10 - New

This document discusses the String class in C++, including how to declare and initialize strings, common string methods like length(), substr(), find(), insert(), compare(), and replace(), and examples of using these methods. It also provides examples of string practices, such as splitting a string by a delimiter, checking if a string is a pangram, and counting the characters in strings.

Uploaded by

黃楷庭
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)
34 views

C++ Week 10 - New

This document discusses the String class in C++, including how to declare and initialize strings, common string methods like length(), substr(), find(), insert(), compare(), and replace(), and examples of using these methods. It also provides examples of string practices, such as splitting a string by a delimiter, checking if a string is a pangram, and counting the characters in strings.

Uploaded by

黃楷庭
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/ 19

計算機程式與應用實習

Week 10
String class
Note

• Property: 屬性
• Concatenate: 連接
• Provides: 提供
Outline

• String class
• String class declaration and initialization
• String library
• length
• substr
• find
• insert
• compare
• replace
• Practice
String class

• C++ has in its definition a way to represent a sequence of


characters as an object of the class.
• This class is called string.
• String class stores the characters as a sequence of bytes with
the functionality of allowing access to the single-byte
character.
String class declaration and initialization
Addition of string class

• We can use the "+" operator to concatenate two strings to get


a new string.
input of string class

• cin: used for strings that "do not contain '\n', '\t', and space
key characters". Space keys can be used to separate different
inputs.
• getline: Used for strings that "contain the whitespace key
character, '\t'". Only the newline key can be used to separate
different inputs.
Cin and getline

• We passed in two parameters in the getline() function:


getline(cin, srt2);. The first parameter is the cin object while
the second is the str2 string variable.
Array properties of the string class

• The string class is designed to use "[ ]" to access the


characters in the string, just like the original character array.
string length

• Like the strlen function of string.h , The string class


provides length.
• int length(void);
sub-string

• A part of a string is referred to as a substring.


• string substr(start index, len)
npos

• npos has two functions:


• Indicates that no results have been matched and can be
used as a basis for judging expressions.
• The maximum value of the string length. The default
parameter that needs to be filled with the length as a
parameter is npos, which means "until the end of the
string".
String find

• Searches the string for the first occurrence of the sequence


specified by its arguments.
• int find(target string, start search address)
String insert

• Inserts additional characters into the string right before the


character indicated
• void insert(position, target string)
String compare

int string_name.compare(start address, Maximum length of


strings to compare, target string)
String replace

• Replaces the portion of the string that begins at character pos


and spans len characters by new contents
• void str_name.replace(replaced string, replaced string length,
target string)
Practice

• split
Input is a string str, a character c, str must contain the c character,
please use the c character as the boundary to cut the str string and
output it in a new line.
example:
intput:
abc/def/ghi
/
output:
abc
def
ghi
Practice

• Input a string containing spaces, if it contains all lowercase


English letters more than once, output "Pangrams", otherwise
output "Not Pangram"

Input:
we promptly judged antique ivory buckles for the next prize
output:
pangrams

input:
we promptly judged antique ivory buckles for the prize
output:
Not pangram
Practice

• Input an integer n is the number of strings, then input each


string in sequence, please output how many characters exist
in each string.

Input:
3
abcdde
baccd
eeabg

Output:
2

You might also like