Presentation is loading. Please wait.

Presentation is loading. Please wait.

208 New Book Review. String Type #include Assignment – string m=“Hi Mom”; – m=“Goodbye”; Concatenate - + – Can use variables or literals. Can use [ ]

Similar presentations


Presentation on theme: "208 New Book Review. String Type #include Assignment – string m=“Hi Mom”; – m=“Goodbye”; Concatenate - + – Can use variables or literals. Can use [ ]"— Presentation transcript:

1 208 New Book Review

2 String Type #include Assignment – string m=“Hi Mom”; – m=“Goodbye”; Concatenate - + – Can use variables or literals. Can use [ ] to address a single character – char x=m[3]; – m[3]=‘r’;

3 More String Length or Size – int r=m.length(); – int r=m.size(); Find a substring – int r=m.find(“Hi”); – if (r == string::npos) cout<<“not found”<<endl; – r=m.find(“Hi”,pos) Starts looking at position pos in string m.

4 Even More String Stuff Substring – a part of a string – string p=m.substr(start, length); Swap – exchange two strings – m.swap(p);

5 C-Strings Do it yourself, no class to help. But there are some built-in functions. Array of characters. – char m[20], p[20]; – Can hold 19 characters; one for null/terminator (\0).

6 C-string Functions Comparison – strcmp – String CLASS can use, etc – C-strings use strcmp. Returns a value 0 for greater. Int i=strcmp(m,p); Assignment – strcpy – String CLASS can use = – strcpy(m,p); Length – int i=strlen(m); I/O - > work with both strings.

7 I/O stuff Get – Reads a single character. – cin.get(char c); – Everything is a character, blank, tab, newline – cin>>c; skips white space (blank, tab, newline) Ignore – ignore, enum, read left, right Peek- like get but does not move file pointer Putback – backs the file pointer one character

8 Nested Loops Discuss on board

9 Boolean Two values, true – false. int+int->int float*float->float Data type bool – Constants true, false – Cannot I/O them – int bool – bool && bool -> bool || ~ if (bool)….. while (bool)….

10 When to use for/while Can use interchangeably. But not good style. Use for when know how many times doing loop. Use while for unknown (waiting for a condition) times through the loop.

11 Assert #include assert (hours > 0); If condition is true, continues, if false, a message indicating problem is given and the program halts. Most useful for debugging.

12 Break – Continue Don’t need, can use other logic; I don’t like them, don’t use them Be aware of their existence.

13 Const Looks like a variable Cannot change value Good for self-documenting programs float PI=3.1415; Cannot change PI anywhere in the program Good for catching errors

14 Switch Useful to shorten many if – else if (i==1)…… else if (i==2) ….. else if (i==3) …. else ….. Switch switch (i) { case 1: …..;break; case 2: …..; break; case 3:……; break; default:……..}

15 ?: - conditional Equivalent to if-else DON’T use. I don’t like it. Use ifs

16 Palindrome A string of characters that is the same forwards as backwards Ex. (ignoring blanks and quotes) – Madam I’m Adam – Can be applied to numbers when looking at digits as characters.

17 Value/Reference Parameters Arguments – what is passed by the invoking of the function Parameters – what “catches” the value passed into the function Value – a copy of the argument is made and that is passed. If the parameter is changed, the original argument is not changed Reference – the address of the argument variable is passed. If the parameter is changed, the original argument is changed

18 Value/Reference (cont.) Pass by value – default Pass by reference - & EXCEPT arrays – always pass by reference – do not use & Two reasons to pass by reference – Calling function expects/needs argument changed – Passing a LARGE variable (object, struct….). Pass by reference is more efficient If passing something large but don’t want to change pass as const &

19 Parallel Arrays Using two arrays where item k in one array is associated with item k in the other array. Ex. int id[100]; float gpa[100]; Could be done with an array of structs struct student { int id; float gpa; }; student everyone[100];

20 Globals One simple 95% rule: – DON’T USE THEM Needed in special systems programming Will see later Be happy with local variables and passing arguments.


Download ppt "208 New Book Review. String Type #include Assignment – string m=“Hi Mom”; – m=“Goodbye”; Concatenate - + – Can use variables or literals. Can use [ ]"

Similar presentations


Ads by Google