lect06_Strings

lect06_Strings - More on C+ strings More on C+ strings...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
More on C++ strings SIS1117 Computer Programming CS S Co pute og a g 1 c1117 lecture 6
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
ontents Contents omparing C++ trings Comparing C++ strings Simple data type char Member functions provided by C++ strings Build-in function getline() , getchar() c1117 lecture 6 2
Background image of page 2
++ String C++ String ecall that: C++ has a tring rary at provides Recall that: C++ has a string library that provides more convenient ways to handle texts . o use the tring we need to include the eader To use the string , we need to include the header : #include <string> e can eclare tring variables store ring alues We can declare string variables to store string values We can assign a string constant , or the value of another string to a string . We can concatenate two strings by the operator "+" tring str1 = "I loves"; string str1 = "I loves"; string str2 = "HKU"; out << str1 + str2 + " :- " << endl; I loves HKU :-> c1117 lecture 6 3 cout << str1 + str2 + : > << endl;
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
++ trings n also be compared C++ strings can also be compared lexicographically (dictionary order) by relational operators. See name.cc & monthdays.cc string str1 = "Apple"; string str2 = "apple"; string str3 = "apples"; string str4 = "orange"; bool t1 = (str1 == str2); ool t2 = (str1 < str2); bool t2 = (str1 < str2); bool t3 = (str2 < str3); bool t4 = (str3 != str4); t1 = false 2 = true bool t5 = (str4 > str3); tt u e t3 = true t4 = true 5 = true c1117 lecture 6 4
Background image of page 4
ever compare two ring literals irectly! Never compare two string literals directly! At least one of the operands must be a C++ string . ee tring mp cc See string cmp.cc if("zebra" < "ant") cout << "Yes" << endl; else cout << "No" << endl; It is a valid expression, no error message is given, but nexpected result is gained, we can explain it at this moment. See string-cmp.cc unexpected result is gained, we can t explain it at this moment. c1117 lecture 6 5
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
fact a tring composed of a quence of In fact, a string is composed of a sequence of characters . haracters in a tring n be ecified y their Characters in a string can be specified by their positions. or a tring ith length the positions of it For a string with length x , the positions of it s characters are ranging from 0 to x-1 iven a tring variable we can access an Given a string variable , we can access an individual character of it using the subscript perator ] quare bracket) operator [] (square bracket).
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 05/04/2011.

Page1 / 22

lect06_Strings - More on C+ strings More on C+ strings...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online