# My Programming Lab - Ch10

## Unlock all answers in this set

question
Given a char variable c that has already been declared, write some code that repeatedly reads a value from standard input into c until at last a 'Y' or 'y' or 'N' or 'n' has been entered.
do{ cin >> c; }while (!(c == 'Y' || c == 'y' || c=='N' || c == 'n'));
question
Declare a char array named line suitable for storing C-strings as large as 50 characters, and write a statement that reads in the next line of standard input into this array. (Assume no line of input is 50 or more characters.)
char line[50]; cin.get(line, 50);
question
Declare a two-dimensional character array named "gasgiants" that is capable of storing the names of the gas giant planets of our solar system: jupiter, saturn, uranus, and neptune. Do not make the array any larger than it needs to be.
char gasgiants[4][8];
question
Declare and initialize a two-dimensional character array named "stooges" to store the names "moe", "larry" and "curly" (in that order). Do not make the array any larger than it needs to be.
char stooges[3][6] = {"moe", "larry", "curly"};
question
Assume that scotus is a two-dimensional character array that stores the family names (last names) of the nine justices on the Supreme Court of the United States. Assume no name is longer than twenty characters. Assume that scotus has been initialized in alphabetical order. Write some code that will print each to standard output , each on a separate line with no other characters
for (int i = 0; i < 9; i++){ cout << scotus[i] << "n"; }
question
Write an expression that evaluates to true if and only if the C-string s equals the C-string "end"
strcmp(s, "end") == 0
question
Write an expression that evaluates to true if and only if the C-string s does not equal "end"
!strcmp(s, "end") == 0
question
Write an expression that evaluates to true if the value of the C-string s1 is greater than the value of C-string s2
strcmp(s1, s2) > 0
question
Given the string variables name1 and name2, write a fragment of code that assigns the larger of the two to the variable first (assume all three are already declared and name1 and name2 assigned values).
if (strcmp(name1, name2) > 0) first = name1; else first = name2;
question
Write a function max that has two C string parameters and returns the larger of the two. Instructor Notes: Hint: Use char* as the type for the c-strings here.
char* max(char *number1, char *number2){ if (strcmp(number1, number2) > 0) return number1; else return number2; }
question
Write a function min that has three C string parameters and returns the smallest. Instructor Notes: Hint: Use char* as the type for the c-strings here.
char* min(char *string1, char *string2, char *string3) { if(strcmp(string1, string2) > 0){ if (strcmp(string2, string3) > 0) return string3; else return string2; } else{ if(strcmp(string1, string3) > 0) return string3; else return string1; } }
question
Assume that name is a variable of type string that has been assigned a value. Write an expression whose value is the first character of the value of name. So if the value of name were "Smith" the expression 's value would be 'S'.
name[0]
question
Assume that name is a variable of type string that has been assigned a value. Write an expression whose value is the last character of the value of name. So if the value of name were "Blair" the expression's value would be 'r'.
name[name.length() -1]
question
Assume that name is a variable of type string that has been assigned a value. Write an expression whose value is the second character of the value of name. So if the value of name were "Green" the expression 's value would be 'r'
name[1]
question
Assume that word is a variable of type string that has been assigned a value . Write an expression whose value is a string consisting of the first three characters of the value of word . So if the value of word were "dystopia" the expression 's value would be "dys".
word.substr(0, 3)
question
Assume that word is a variable of type string that has been assigned a value . Write an expression whose value is a string consisting of the last three characters of the value of word . So if the value of word were "biggest" the expression 's value would be "est".
word.substr(word.length() - 3, 3)
question
Assume that given , middle and family are three variables of type string that have been assigned values . Write an expression whose value is a string consisting of the first character of given followed by a period followed by the first character of middle followed by a period followed by the first character of family followed by a period: in other words, the initials of the name . So if the values of these three variables were "John" "Fitzgerald" "Kennedy", then the expression 's value would be "J.F.K.".
given.substr(0, 1) + "." + middle.substr(0,1) + "." + family.substr(0,1) + "."
question
Assume that sentence is a variable of type string that has been assigned a value . Assume furthermore that this value is a string consisting of words separated by single space characters with a period at the end. For example: "This is a possible value of sentence." Assume that there is another variable declared , firstWord , also of type string . Write the statements needed so that the first word of the value of sentence is assigned to firstWord . So, if the value of sentence were "Broccoli is delicious." your code would assign the value "Broccoli" to firstWord
firstWord = sentence.substr(0, sentence.find(' ', 0));
question
Assume that word is a variable of type string that has been assigned a value . Assume furthermore that this value always contains the letters "dr" followed by at least two other letters. For example: "undramatic", "dreck", "android", "no-drip". Assume that there is another variable declared , drWord , also of type string . Write the statements needed so that the 4-character substring word of the value of word starting with "dr" is assigned to drWord . So, if the value of word were "George slew the dragon" your code would assign the value "drag" to drWord
drWord = word.substr(word.find("dr", 0), 4);
question
Assume that sentence is a variable of type string that has been assigned a value . Assume furthermore that this value is a string consisting of words separated by single space characters with a period at the end. For example: "This is a possible value of sentence." Assume that there is another variable declared , secondWord , also of type string . Write the statements needed so that the second word of the value of sentence is assigned to secondWord . So, if the value of sentence were "Broccoli is delicious." your code would assign the value "is" to secondWord
secondWord = sentence.substr(sentence.find(" ") + 1); secondWord = secondWord.substr(0, secondWord.find(" "));
question
Write a sequence of statements that finds the first comma in the string line , and assigns to the variable clause the portion of line up to, but not including the comma. You may assume that an int variable pos , as well as the variables line and clause , have already been declared
clause = line.substr(0, line.find(',') );
question
Given the string variable address , write an expression that returns the position of the first occurrence of the string "Avenue" in address