M4ritimeSeeker

PASS Week 4/7: Strings

Apr 5th, 2021 (edited)
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. Use osprey for todays practice problems to practice for exam #3
  2.  
  3.  
  4.  
  5. Problem #1:
  6.  
  7. This problem is about declaring strings and printing them out. Program the following:
  8.  
  9. 1) Declare a STRING and initialize it to the value “2019”.
  10. 2) Declare a CHARACTER POINTER and initialize it to NULL.
  11. 3) Declare another CHARACTER POINTER and initialize it to the value “2021”.
  12. 4) Compose 3 printf() statements to print the 3 strings you just declared.
  13. > Also print the length of those strings using strlen().
  14.  
  15. You WILL get a segmentation fault error from this program. Why does that occur?
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32. Problem #2:
  33.  
  34. This problem is about initializing strings and printing them in reverse. Program the following:
  35.  
  36. 1) Declare a string and initialize it to a short sentence.
  37. > For example: "Godzilla is gonna win."
  38. 2) Use a for loop to print the string to screen one character at a time backwards.
  39. > Your for loop should determine when to stop by using the strlen() function.
  40.  
  41. Now modify the program and declare and initialize the array
  42. using pointer notation like in problem 1. Did it work the same?
  43.  
  44.  
  45. SAMPLE OUTPUT:
  46.  
  47. .niw annog si allizdoG
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56. Problem #2B:
  57. Modify the previous problem to print the array to screen using the %s modifier with
  58. printf(). What is the difference printing it using the loop or printing with %s?
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80. Problem #3:
  81.  
  82. This problem involves taking strings as input. Program the following:
  83.  
  84. 1) Declare a string of size 80 and initialize it to nothing.
  85. > For example: [insert string] = "";
  86. 2) Prompt the user to enter in a sentence. It must be less than 80 characters in length.
  87. 3) Scan the sentence into the string. USE fgets().
  88. 4) Print the string to the screen to make sure it scanned correctly.
  89.  
  90.  
  91. SAMPLE OUTPUT:
  92.  
  93. Enter in a sentence up to 80 characters: Hot take, Raisin Bran isn't bad.
  94.  
  95. Here is your sentence: Hot take, Raisin Bran isn't bad.
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109. Problem #4:
  110.  
  111. This program mixes loops, if/else, arrays, and strings together.
  112. Using the program from problem 3, add code that will check whether each
  113. letter in the sentence is upper-case or lower-case. Program the following:
  114.  
  115. 1) Import the library with character functions to check for upper and lower case.
  116. > Remember the kahoot?
  117. 2) After scanning the string from the user, write a for loop that will iterate through the entire string.
  118. 3) Using if/else, check if the letter is upper or lower case, and print it to the screen.
  119. > Remember that spaces exist in the string, try to manage those.
  120.  
  121.  
  122.  
  123. SAMPLE OUTPUT:
  124.  
  125. Enter in a sentence up to 80 characters: Can I get that pizza BONELESS?
  126.  
  127. C is Uppercase
  128. a is Lowercase
  129. n is Lowercase
  130.  
  131. I is Uppercase
  132.  
  133. g is Lowercase
  134. e is Lowercase
  135. t is Lowercase
  136.  
  137. t is Lowercase
  138. h is Lowercase
  139. a is Lowercase
  140. t is Lowercase
  141.  
  142. p is Lowercase
  143. i is Lowercase
  144. z is Lowercase
  145. z is Lowercase
  146. a is Lowercase
  147.  
  148. B is Uppercase
  149. O is Uppercase
  150. N is Uppercase
  151. E is Uppercase
  152. L is Uppercase
  153. E is Uppercase
  154. S is Uppercase
  155. S is Uppercase
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166. Problem 4B ## CHALLENGE ##
  167.  
  168. Modify problem 4 to check whether the letters are vowels or consonants rather than upper-case/lower-case.
  169. (This one is a bit tricky, you may need to make some functions for it).
  170.  
  171. SAMPLE OUTPUT:
  172.  
  173. Enter a sentence up to 80 characters in length:
  174. Can I get that pizza BONELESS?
  175. C is consonent
  176. a is vowel
  177. n is consonent
  178.  
  179. I is vowel
  180.  
  181. g is consonent
  182. e is vowel
  183. t is consonent
  184.  
  185. t is consonent
  186. h is consonent
  187. a is vowel
  188. t is consonent
  189.  
  190. p is consonent
  191. i is vowel
  192. z is consonent
  193. z is consonent
  194. a is vowel
  195.  
  196. B is consonent
  197. O is vowel
  198. N is consonent
  199. E is vowel
  200. L is consonent
  201. E is vowel
  202. S is consonent
  203. S is consonent
  204.  
Add Comment
Please, Sign In to add comment