cs31day8 Wk 4 M

cs31day8 Wk 4 M - string s = "Hello"; s: Hello...

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

View Full Document Right Arrow Icon
string s = "Hello"; s: Hello //start counting at zero when counting position for (int k = 0; k != s.size(); k++) 01234 cout << s.at(k) << endl; s.at(k) is the character at position k of s ex: s.at(3) is l in "Hello" s.size() is the number of characters in the string s ______________________________________ cout << "Enter some text: "; 111111111122222222223 string t; 0123456789012345678901234567890 getline(cin, t); t: Mary had a little lamb. int numberOfMs = 0; for (int k = 0; k != t.size(); k++) { if (t.at(k) == 'M' || t.at(k) == 'm') numberOfMs++; } cout << "The number of m's (upper and lower case) is " << numberOfMs << endl; t is a string individual characters are a different type then from strings character type: char t.at(k) is a char cout << t //both of these work cout << t.at(0) "Hello" "ABC" "A" "A B" //strings '?' 'A' ' ' '\n' '4' // only one character If you compare t.at(k) to a string, it won't compile because one is a string type, the other is of char type.
Background image of page 1

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

View Full DocumentRight Arrow Icon
string s = "Hello"; // s is a string char c = s.at(1); // this is initialized to lowercase e _____________________________ check if phone number is valid US phone number
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/02/2011 for the course CS 31 taught by Professor Melkanoff during the Spring '00 term at UCLA.

Page1 / 6

cs31day8 Wk 4 M - string s = &quot;Hello&quot;; s: Hello...

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

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