L14strings

L14strings - Strings ● strings are specific constructs...

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

View Full Document Right Arrow Icon

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

View Full Document Right Arrow Icon

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

View Full Document Right Arrow Icon

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

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Strings ● strings are specific constructs that are geared towards processing sequences of characters ● before using include string header: #include <string> don’t use string.h ● string declaration: string mystr; ● value assignment at declaration: string mystr(“Hello”); string mystr2=“Hello”; ● assignments from other types are not permitted: string error1 = ‘c’; // error string error2 = 22; // error ● can assign a character with assignment operator: mystr=‘n’; Declaring Strings ● String can be output as any other type: string s=“hello world”; cout << s << endl; ● two ways to input strings: ■ using extraction operator - strips white space and assigns the first “word” to the string: cin >> s; hello wold\n input assigns only hello to s ■ using getline function - assigns all characters to string up to newline (not included): getline(cin, s); hello wold\n input assigns hello world to s ■ do not mix one and the other way of input! String I/O ● use assignment operator as with other types: string s1, s2, s3; s1=“Intro”; s2=“OOP”; ● plus “+” is used for string concatenation: s3=s1 + “to” + s2; ● compound concatenation allowed: s1 += “duction”; ● characters can be concatenated with strings: s2=“Hell” + ‘o’; s2+=‘o’; ● no other types can be assigned to strings or concatenated with strings: s2= 42.54; // error s2=“Catch” + 22; // error String Assignment and Concatenation ● comparison operators ( > , < , >= , <= , == , != ) are applicable to strings...
View Full Document

{[ snackBarMessage ]}

Page1 / 12

L14strings - Strings ● strings are specific constructs...

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

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