> word; int n = (int) word.length (); cout << "String length is " << n << endl; for (int i = 0; i < n; i++) { cout << word[i] << endl; } return 0; } rWord.length() returns the length of the string rThe length is returned as a strange data type (size_t) rWe use a coercion to get rid of the warning message rTo visit each character in turn I write a For Loop rThe loop executes with I = 0, then I = 1, then I = 2 etc. rTherefore I use the expression word[i] to get each character.">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 1 -- 1Computer Science I - Martin Hardwick Strings #include using namespace std; int main () { string word; cout << "Enter a word" << endl; cin.

Similar presentations


Presentation on theme: "Lecture 1 -- 1Computer Science I - Martin Hardwick Strings #include using namespace std; int main () { string word; cout << "Enter a word" << endl; cin."— Presentation transcript:

1 Lecture 1 -- 1Computer Science I - Martin Hardwick Strings #include using namespace std; int main () { string word; cout << "Enter a word" << endl; cin >> word; if (word == "bang") { cout << "You entered bang!" << endl; } else { cout << "You did not enter bang!" << endl; } return 0; } rString is a type defined in string.h rString is an object rThere are many methods that work on string objects rOne of them is == rAnother is [ ] rFor more see the web l http://www.cprogramming.com/tutorial/strin g.html http://www.cprogramming.com/tutorial/strin g.html l Is a good page

2 Lecture 1 -- 2Computer Science I - Martin Hardwick Accessing one character in a string #include using namespace std; int main () { string word; cout << "Enter a word" << endl; cin >> word; if (word[0] == 'b') { cout << "You entered bang!" << endl; } else { cout << "You did not enter bang!" << endl; } return 0; } rWord[0] gets the first character in the string rWord[1] would get the second character rEtc. rWhat if I want to get each character in a string? rI will need to know the number of characters rTry typing word. in Visual C++ you should see a long list of methods. rNow make a guess for a function that returns the character size

3 Lecture 1 -- 3Computer Science I - Martin Hardwick Accessing every character in a string #include using namespace std; int main () { string word; cout << "Enter a word" << endl; cin >> word; int n = (int) word.length (); cout << "String length is " << n << endl; for (int i = 0; i < n; i++) { cout << word[i] << endl; } return 0; } rWord.length() returns the length of the string rThe length is returned as a strange data type (size_t) rWe use a coercion to get rid of the warning message rTo visit each character in turn I write a For Loop rThe loop executes with I = 0, then I = 1, then I = 2 etc. rTherefore I use the expression word[i] to get each character.

4 Lecture 1 -- 4Computer Science I - Martin Hardwick The For Loop Statement rSyntax: for (count = 1; count <= 36; count = count + 1) {... } rMeaning: initial value of counter final value of counter counter increment count = 1count = count + 1 count <= 36 statement... done YES NO

5 Lecture 1 -- 5Computer Science I - Martin Hardwick For Loop Variations rThe counter variable can be initialized to anything. rThe terminating condition can be any logical expression involving the counter variable. l comparison operators: rThe increment can be any value, positive or negative. rExamples: for (i = 0; i < 10; i = i + 2) for (count = 10; count >= 0; count = count - 1) for (j = 1; j != 6; j = j + 2) Alert: What happens in the last example? greater than =greater or equal = =equal! =not equal

6 Lecture 1 -- 6Computer Science I - Martin Hardwick What Is Displayed By Each Program Segment? for (i = 0; i < 10; i = i + 2) { cout << i << endl; } for (j = 20; j >= 0; j = j - 3) { cout << j << endl; } 0246802468 20 17 14 11 8 5 2

7 Lecture 1 -- 7Computer Science I - Martin Hardwick Nested For Loop Examples for (j = 0; j <= 10; j = j + 5) { for (k = 10; k > 0; k = k - 5) { cout << j << << k << endl; } for (a = 1; a < 3; a = a + 1) { for (b = 5; b <= 6; b = b + 1) { for (c = 2; c > 0; c = c - 1) { cout << a << << b << << c << endl; } How many lines of output are generated? 6 What are the lines of output? 0 10 0 5 5 10 5 10 10 5 How many lines of output are generated? 8 What are the lines of output? 1 5 2 1 5 1 1 6 2 1 6 1 2 5 2 2 5 1 2 6 2 2 6 1

8 Lecture 1 -- 8Computer Science I - Martin Hardwick Visit the characters in a string in reverse order #include using namespace std; int main () { string word; cout << "Enter a word" << endl; cin >> word; int n = (int) word.length (); cout << "String length is " << n << endl; for (int i = ??; ??? ; i--) { cout << word[i] << endl; } return 0; } rStart the loop at the end rUse a decrement (i--) rStop the loop at the beginning rBe careful to get the start and end conditions right.

9 Lecture 1 -- 9Computer Science I - Martin Hardwick Other string facts #include using namespace std; int main () { string word; int n; word = Two lines\nSecond"; n = (int) word.length (); cout << word << endl << length is " << n << endl; word = Tab in line\tSecond"; in = (int) word.length (); cout << word << length is " << n << endl; return 0; } r\n is one character not two r\n is carriage return (endl) r\t is tab r\\ is \ r\r is ???? (look it up but it gets complicated so you are not expected to know)

10 Lecture 1 -- 10Computer Science I - Martin Hardwick Controlling Output Format #include.. M = 2.6; P = 123.4; cout << M << P << endl; cout << M << " " << P << endl; cout << endl; cout << setw(8) << M << setw(12) << P << endl; cout << setw(8) << P << setw(12) << M << endl; cout << setprecision(3) << setiosflags(ios::fixed); cout << setw(8) << M << setw(12) << P << endl; cout << setw(8) << P << setw(12) << M << endl; setw(number) -- specify the width (how many characters) to use to print the next number. by default, numbers are right justified in this width if the width is too small, the compiler uses the minimum it needs setiosflags(ios::fixed) -- float numbers will be printed with a fixed number of digits to the right of the decimal point. defaults to variable setprecision(number) -- specify the number of digits to the right of the decimal point when the ios::fixed flag is set.

11 Lecture 1 -- 11Computer Science I - Martin Hardwick Controlling Output Format (continued)


Download ppt "Lecture 1 -- 1Computer Science I - Martin Hardwick Strings #include using namespace std; int main () { string word; cout << "Enter a word" << endl; cin."

Similar presentations


Ads by Google