M4ritimeSeeker

PASS Week 10: Strings

Nov 2nd, 2021 (edited)
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.13 KB | None | 0 0
  1. WARMUP (Review from Last Week): Take String as Input
  2.  
  3. Create a program that will take the user's first name as input and stores it as a string.
  4. Then print the string back to the screen as output.
  5.  
  6. SAMPLE OUTPUT:
  7.  
  8. Enter in your first name: Justin
  9. The name you entered: Justin
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19. Mystery Program:
  20.  
  21. Decipher what this program is doing.
  22.  
  23.  
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <time.h>
  27.  
  28. int p1(int, int, int);
  29.  
  30. int main()
  31. {
  32. int num1, num2, num3;
  33.  
  34. scanf("%d %d %d", &num1, &num2, &num3);
  35.  
  36. p1(num1, num2, num3);
  37.  
  38. return 0;
  39. }
  40.  
  41. int p1(int n3, int n2, int n1){
  42. int ary[n3];
  43. srand(time(NULL));
  44. int ff = 0;
  45.  
  46. for(int i = 0; i < n3; i++){
  47. ary[i] = rand() % n2;
  48. }
  49.  
  50. for(int i = 0; i < n3; i++){
  51. if(ary[i] < n1){
  52. ff++;
  53. }
  54. }
  55.  
  56. printf("%d\n%d\n%d\n", n1, n2, n3);
  57.  
  58. for(int i = 0; i < n3; i++){
  59. printf("%d ", ary[i]);
  60. }
  61. printf("\n");
  62.  
  63. return ff;
  64. }
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79. PROBLEM #1: Taking Multiple Strings and Data as Input
  80.  
  81. Similar to problem 1, create a program that will prompt the user to enter their first name, last name, and age. However, you must take all 3 inputs IN A SINGLE scanf statement. Then print them back out to the user.
  82. First name will be stored in a string, last name will be stored in a second string, and age is an integer.
  83.  
  84. SAMPLE OUTPUT:
  85.  
  86. Enter in your first name, last name, and age:
  87. Justin Bentley 21
  88. Hello Justin Bentley age 21
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101. PROBLEM #2: Using fgets(), Strings and String Functions
  102.  
  103. Compose a program that prompts the user to enter 3 separate sentences and stores them in 3 separate strings (Each character array will be 40 in length). Then copy/concatenate all 3 sentences into one huge string of length 120.
  104.  
  105. Prompt the user and use fgets() to read the input into the first array.
  106. Then prompt the user for a second sentence and store it in the second array.
  107. Then do the same again storing a third string into a third array.
  108.  
  109. Next, use the strcpy() function to copy the first array into the fourth array.
  110. Then use the strcat() function to concatenate the second array onto the end of the fourth array. Then do it again, concatenating the third array into the fourth array.
  111. Then print the fourth array using the %s modifier, and the length using strlen().
  112.  
  113. SAMPLE OUTPUT:
  114.  
  115. Enter in your first name: Justin
  116. Enter in your middle name: William
  117. Enter in your last name: Bentley
  118. Justin
  119. William
  120. Bentley
  121. 23
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137. PROBLEM #3: String Reversals
  138.  
  139. Create a program that will reverse a string
  140. Declare and initialize a string with the value "Raisin Bran isn't bad".
  141. Then use a loop and the strlen() function and print the string one character at a time backwards.
  142.  
  143. SAMPLE OUTPUT:
  144.  
  145. dab t'nsi narB nisiaR
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159. PROBLEM #4: Converting Strings to Uppercase
  160.  
  161. Compose a program with a character array of 81 characters in size. Also declare
  162. an integer variable to store the size of the sentence later on.
  163.  
  164. Prompt the user to enter in a sentence up to 80 characters and then store it in the array using fgets().
  165. Get the sizeof() the sentence, and store it in the integer you made earlier.
  166.  
  167. Now, using a for loop, examine the stored sentence one character at a time check if they are uppercase using the isupper() function. If it ISN'T uppercase, then make it uppercase using the toupper() functin.
  168. Also, remove the carriage return ā€˜\n’ character from the array if you use fgets() to get the input string.
  169.  
  170. Finally, print out the new string using printf.
  171.  
  172. SAMPLE OUTPUT:
  173.  
  174. Enter in a sentence: This is a sentence
  175. THIS IS A SENTENCE
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189. PROBLEM #5: Counting Specific Characters in a String
  190.  
  191. Re-using your solution to problem #4, modify the program to read in a sentence up to 80
  192. characters but this time count the number characters that are not spaces, periods,
  193. exclamation points or commas.
  194.  
  195. Print out the total and this should be the number of characters that weren't specified.
  196.  
  197. SAMPLE OUTPUT:
  198.  
  199. Enter in a sentence: This is a sentence
  200. Total: 15
  201.  
  202.  
  203.  
Advertisement
Add Comment
Please, Sign In to add comment