M4ritimeSeeker

PASS Week 10 Solutions

Nov 2nd, 2021
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. WARMUP SOLUTION
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5.  
  6. int main(){
  7.  
  8. char str[20];
  9.  
  10. printf("Enter in your first name: ");
  11. scanf("%s", str);
  12. printf("%s\n", str);
  13.  
  14. return 0;
  15. }
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26. PROBLEM #1 SOLUTION
  27.  
  28. #include <stdio.h>
  29. #include <string.h>
  30.  
  31. int main(){
  32.  
  33. char fName[25];
  34. char lName[25];
  35. int age;
  36.  
  37. printf("Enter in your first name, last name, and age: \n");
  38. scanf("%s %s %d", fName, lName, &age);
  39. printf("%s %s %d\n", fName, lName, age);
  40.  
  41. return 0;
  42. }
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55. PROBLEM #2 SOLUTION
  56.  
  57. #include <stdio.h>
  58. #include <string.h>
  59.  
  60. int main(){
  61.  
  62. char ary1[40];
  63. char ary2[40];
  64. char ary3[40];
  65. char ary4[120];
  66.  
  67. printf("Enter in your first name: ");
  68. fgets(ary1, 40, stdin);
  69. printf("Enter in your middle name: ");
  70. fgets(ary2, 40, stdin);
  71. printf("Enter in your last name: ");
  72. fgets(ary3, 40, stdin);
  73.  
  74. strcpy(ary4, ary1);
  75. strcat(ary4, ary2);
  76. strcat(ary4, ary3);
  77.  
  78. printf("%s%d\n", ary4, (int) strlen(ary4));
  79.  
  80. return 0;
  81. }
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91. PROBLEM #3 SOLUTION
  92.  
  93. #include <stdio.h>
  94. #include <string.h>
  95.  
  96. int main(){
  97.  
  98. char ary[] = "Mary had a little lamb";
  99.  
  100. for(int i = strlen(ary); i >= 0; i--){
  101. printf("%c", ary[i]);
  102. }
  103.  
  104. return 0;
  105. }
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118. PROBLEM #4 SOLUTION
  119.  
  120. #include <stdio.h>
  121. #include <string.h>
  122. #include <ctype.h>
  123.  
  124. int main(){
  125.  
  126. char ary[81];
  127. int size;
  128.  
  129. printf("Enter in a sentence: ");
  130. fgets(ary, 80, stdin);
  131. ary[strcspn(ary, "\n")] = 0;
  132. size = sizeof(ary);
  133.  
  134. for (int i = 0; i < size; i++){
  135. if (!isupper(ary[i])){
  136. ary[i] = toupper(ary[i]);
  137. }
  138. }
  139.  
  140. printf("%s\n", ary);
  141.  
  142. return 0;
  143. }
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158. PROBLEM #5 SOLUTION
  159.  
  160. #include <stdio.h>
  161. #include <string.h>
  162. int main(void) {
  163. char userInput[50]; // User numbers
  164. int i;
  165. int charCount;
  166.  
  167. fgets(userInput, 50, stdin);
  168.  
  169. charCount = 0;
  170. for (i = 0; i < strlen(userInput); ++i) {
  171. if ( (userInput[i] != ' ') && (userInput[i] != '.') && (userInput[i] != ',')
  172. && (userInput[i] != '!') && (userInput[i] != '\n')) {
  173. charCount = charCount + 1;
  174. }
  175. }
  176. printf("%d\n",charCount);
  177.  
  178. return 0;
  179. }
  180.  
Advertisement
Add Comment
Please, Sign In to add comment