1. Trang chủ
  2. » Giáo Dục - Đào Tạo

C and C++ Strings

41 318 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 41
Dung lượng 245,97 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

An Array Type for Strings • C-strings can be used to represent strings of characters – C-strings are stored as arrays of characters – C-strings use the null character '\0' to end a stri

Trang 1

C and C++ Strings

Trang 2

An Array Type for Strings

• C-strings can be used to represent strings of characters

– C-strings are stored as arrays of characters

– C-strings use the null character '\0' to end a string

• The Null character is a single character

– To declare a C-string variable, declare an array of characters:

Trang 3

• Declaring a C-string as char s[10] creates space for only nine characters

– The null character terminator requires one space

• A C-string variable does not need a size

variable

– The null character immediately follows the last

character of the string

• Example:

s[0] H i s[1] s[2] M s[3] s[4] o m s[5] ! s[6] \0 s[7] s[8] ? s[9] ?

C-string Details

Trang 4

Initializing a C-string

• To initialize a C-string during declaration:

char my_message[20] = "Hi there.";

– The null character '\0' is added for you

• Another alternative:

char short_string[ ] = "abc";

but not this:

char short_string[ ] = {'a', 'b', 'c'};

– because null character '\0' will NOT be added

Is called a string constant

or string literal

Trang 5

Length of C Strings

To calculate the length of a string use the strlen() function – returns the

number of characters between the start of the string to the terminating

Trang 6

Assignment With C-strings

• This statement is illegal:

char my_name[20], your_name[20]; my_name = "Thao";  ERROR!

– This is an assignment statement, not an

Trang 8

A Problem With strcpy

• strcpy can create problems if not used

strcpy (my_name, "Thao Thi Nguyen");

 this will overwrite memory!

Trang 9

A Solution for strcpy

• A safer version of strcpy is named strncpy

– strncpy uses a third argument representing the maximum number of characters to copy

– Example: char another_string[10];

strncpy(another_string,

another_string[9] = ‘\0’;

This code copies up to 9 characters into

another_string, leaving one space for '\0'

Trang 10

Comparing C-strings

• The = = operator does not work with C-strings

– The predefined function strcmp is used to compare C-string variables

– Example: #include <cstring>

Trang 13

More C-string Functions

– The second argument is added to the end of the first

– The result is placed in the first argument

– Example:

char string_var[20] = "The rain";

strcat(string_var, "in Spain");

Now string_var contains "The rainin Spain "

– A third parameter specifies a limit for the number

of characters to concatenate

Trang 14

C-string Output

• C-strings can be output with the insertion

operator <<

char news[] = "is good.";

cout << "The news " << news << endl;

Output: The news is good

Trang 15

Example

Trang 16

C-string Input

• The extraction operator >> can fill a C-string

– Whitespace ends reading of data

– Example: char a[80], b[80];

cout << "Enter input: " << endl;

Trang 17

Issues with cin >> for strings

cin >> (extraction operator) uses whitespace (spaces, tab,

newline) to delineate a string Therefore it will only read one word when it extracts input for a string (skipping all the

whitespace characters before reading each word)

char name[20], address[50];

cout << "Please enter your name:\n";

cin >> name;

user types "John Smith" and hits enter key

cout << "Please enter your address:\n";

Trang 18

Reading an Entire Line

• Predefined member function getline can read an entire line, including spaces

– getline is a member of all input streams (like cin)

– getline has two arguments

• The first is a C-string variable to receive input

• The second is an integer, specifying the size of the string variable

Trang 19

Using getline

spaces into a single C-string variable

– char a[80];

cout << "Enter input:\n";

cin.getline(a, 80);

cout << a << End Of Output\n";

and could produce:

Enter some input:

Do be do to you!

Do be do to you!End of Output

Trang 20

Mixing cin >> and getline

• Recall cin >> n skips whitespace (spaces, tabs, newline) to find what it needs to read, then stops reading when whitespace is found

• cin >> leaves the '\n' character in the input

Trang 21

getline after cin >>

double value;

cin >> value;

char str[80];

cin.getline(str, 80);

And you type: 3.14<return>

• 3.14 is read into value

• The newline following the 3.14 is still sitting in the input buffer

• cin.getline(str)immediately processes the newline that

is still on the input buffer

str becomes an empty string (" \0")

• It looks like that the application "skipped" the cin.getline()

statement

• Solution?

• Add a call to cin.get()/cin.ignore() after calling cin >> x

3 1 4 \n

Trang 22

cin.ignore

characters, including '\n' that remain in a line

– Ignore takes two arguments

• First, the maximum number of characters to discard

• Second, the character that stops reading and discarding – Example:

• cin.ignore(1000, '\n');

discards up to 1000 characters until it finds a '\n' cin.ignore();

discards the next character

• discard:sự chui bài, sự dập bài

Trang 24

The Standard string Class

• The string class allows the programmer to

treat strings as a basic data type

– No need to deal with the implementation as with C-strings (array of chars)

– No need to worry about size of string!

• The string class is defined in the string library and the names are in the standard namespace

– To use the string class you need these lines:

#include <string>

using namespace std;

Trang 26

Using + With strings

• Variables of type string can be concatenated with the + operator

Trang 27

string Constructors

• The default string constructor initializes a

string object to the empty string

• Another string constructor takes a C-string

Trang 28

Comparing strings

For C Strings use strcmp

if (str1 == "Danang"){ …

Trang 29

Member Function length

• The string class member function length return the number of characters in the string object:

string str1 = "ABC ";

cout << str1.length(); // prints 3

• Is exactly the same as member function size()

• string.empty() is the same as string.length() == 0

str1 is an object (or instance) of class string

length() is a member function of class string

Trang 30

• If you use int you will get compiler warnings:

for (int i = 0; i < str.length(); i++)

• Solution – use size_t instead:

for (size_t i = 0; i < str.length(); i++)

Trang 31

Operator[] and the string Class

Operator[] works the same as for C-strings char arrays

Trang 32

I/O With Class string

• The insertion operator << is used to output objects of type string

– Example: string s = "Hello Mom!";

cout << s;

• The extraction operator >> can be used to

input data for objects of type string

Example: string s1;

cin >> s1;

>> skips leading whitespaces and stops on

encountering more whitespace (reads one word)

Trang 33

getline and Type string

• A getline function exists to read entire lines into a string variable

– This version of getline is not a member of the istream class, it is a non-member function

– Syntax for using this getline is different than that used with cin: cin.getline(…)

getline(Istream_Object, String_Object);

Trang 34

cout << line << "END OF OUTPUT\n";

Output could be:

Enter some input:

Do be do to you!

Do be do to you!END OF OUTPUT

Trang 35

Another Version of getline

• The versions of getline we have seen, stop reading at the end of line marker '\n'

• getline can stop reading at any character specified in the argument list

• This code stops reading when a '?' is read

Trang 36

C++ string vs C-string

Old-style C-string

• pointer to character array, null-terminated ('\0')

• string functions are dangerous (strcpy, strncpy, )

• String literals are actually C-strings ("hello\n");

C++ string

• are objects of class string

• are much easier and safer to use than C-Strings

• have member functions such as:

length(); find(…); insert( ); replace( );

Trang 37

Converting C++ string to C-string

Converting C-string to C++ string

• Happens automatically in most cases

string s = "abc";

• Can force using string("abc")

string s = "abc" + "def"; // compile error

string s = string("abc") + "def";

Converting C++ string to C-string

• Some older functionality requires use of C-string

• Use member function c_str() :

printf("%s", last_name.c_str());

For more information on string class see:

http://www.cplusplus.com/reference/string/string/

Trang 38

Strings and Unicode

To store Unicode strings C++ defines the wstring type, which uses wchar_t as its character type

All wide string literals need to be prefixed with 'L'

Trang 39

string::find()

string class implements a find function:

size_t find(string pattern, size_t start_position);

returns position where pattern found,

or the constant string::npos if not found

string str = "My name is Pam";

size_t found;

found = str.find("name"); // returns 3

found = str.find("am", 4); // returns 12

found = str.find("john"); // returns string::npos

There is also a similar string::rfind() function which searches

from the end of the string (reverse find)

:: means member function of class

Trang 40

string::substr()

string substr(size_t pos, size_t n);

Returns the substring that starts at character position pos and has a length of n characters

If you omit the n parameter it means ‘to then end of the string’

Ngày đăng: 22/10/2015, 17:12

Xem thêm

TỪ KHÓA LIÊN QUAN