C++ Part 28

Strings


A char literal is indicated with single quotes (e.g., ’c’), while a string literal is indicated with double quotes (e.g., “abc”).
These are C++ strings and are in library:
  • =: Assignment
  • <,> , <=, >= ,== , ! =, . . .:
  • s[i]: Indexing, i.e., i’th character of s (i ≥ 0)
    •  Ex: string s = ‘‘cat’’; char c = s[2] sets c to ’t’
  •  + :  Concatenation (overloaded for characters)
    • Ex: ‘‘apple’’ + ‘‘pear’’ evaluates to “applepear” while ‘‘apple’’+’s’ evaluates to “apples”
  • s.empty(): true if s is an empty string 
  • s.length(): length of s (≥ 0) 
//Assignment and comparison operators work for strings but NOT
for all C++ data types.

<<, >> : input/output (still uses whitespace as delimiter).
e.g., cin >> s1 >> s2 would assign “Navid” to s1 and “Plus” to s2,
if the user typed “Navid Plus”.
Post a Comment

Popular Posts