Comp Sci Ch 5 Ch 6 Flashcards
What does equality mean with string comparisons
same number of characters and each corresponding character is identical
what does relational mean with string comparisons
begins at index 0 and compares each character until the evaluation results in false, or end of string is reached
What do you have to include when you want to use character operations
include <cctype></cctype>
What are the 5 functions when working with characters and what do they do
isalpha(c) - true if c is a letter
isdigit(c)- true if c is a number 0-9
isspace(c)- true if whitespace or endl;
toupper(c)- returns the uppercase version of c
tolower(c)- returns the lowercase version of c
How to access an input string
getline(cin, name);
How do you access a string character
name.at(x)
if you know length, how do you find index
length - 1w
what do you use to find the string size
name.size()
how can you append one string to another one
string1.append(string2)
what are test cases you can use when testing your code involving a string
0 indexes or the start or end
when to use while loop vs for loop in string code
while loop for when user to providing sentinel value
for loop when we know end/stop
how do you find an item and how in a specific index range
string.find(item)
string.find(item, index)
what is returned when an item is not found
string::npos
how to get a substring starting at an index and with having length characters
string.substr(index, length)
-if no length goes to end
how to add a character to the end of a string
string.push_back(c)
how to insert a sub string to a string at a specific index
string.insert(index, substr)
how to replace characters at some indexes with a substr
string.replace(index, num, substr)
index to num-1
how to return a copy of string 1 with string 2 appended
str1 + str2
The 3 parts of a for loop expression
loop initialization
loop expression
loop variable update
++i vs i++
pre increment and post increment
pre- increment then output
post- output then increment
-in for loops it does not matter but use ++i instead :)
When doing a loop that involves some kind of false statement or true, what should you do
when its false, make the loop stop
How do you iterate through a string with a FOR loop
starting at 0 to name.size()
how do you iterate until done with a string with a WHILE looo
while ( name.find(item) != string::npos)
What is a nested for loop
a for loop that appears in the body of another for loop
how should you walk through a for loop
check, body run, increment
What is a nested for loop for
used to generate all combinations of some items
how do you walk through a nested for loop
check 1st loop, 2 run inside loop as you would normally walk through, increment outer loop
What types of comments should you add for developing a program incrementally and how should you check a loop works
FIXME, output each i
what must you declare to use ifstream and ofstream
include <fstream></fstream>
How to read from a file
–declare ifstream
–open the file using name.open(name of file)
–check if an if branch if it opened using !name.is_open() as expression–inside if cout the error and return 1;
–read each one using while loop with expression name» a variable you declared to store each new value
–close file using name.close()
How to write to a file
–declare of ofstream
–open the file using name.open(name of file)
–check if an if branch if it opened using !name.is_open() as expression–inside cout an error message and return 1
–write to the string using name «_space;“stuff”
–close file using name.close()