M4ritimeSeeker

PASS Week 4/14 Solutions

Apr 11th, 2021
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. Problem 1
  2.  
  3.  
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <ctype.h>
  7.  
  8. int main(int argc,char* argv[])
  9. {
  10. for(int i = argc - 1; i >=0; i--) {
  11. printf("%s \n", argv[i]);
  12. }
  13.  
  14. return 0;
  15. }//end main
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22. Problem 2
  23.  
  24.  
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28.  
  29. int main(void)
  30. {
  31. FILE* fp = NULL;
  32. char filename[50];
  33. printf("Enter your file: ");
  34. fscanf(stdin, "%s", filename);
  35. fp = fopen(filename, "r");
  36.  
  37. if (fp != NULL){
  38. printf("File exists");
  39. }
  40. else{
  41. printf("Sorry B");
  42. }
  43. fclose(fp);
  44.  
  45. return 0;
  46. }//end main
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55. COMPETITION PROGRAM Un-Mystified
  56.  
  57.  
  58. #include <stdio.h>
  59. #include <string.h>
  60. #include <stdbool.h>
  61.  
  62. void FlushInput(void);
  63. bool FileExists(char*);
  64. bool CreateFile(char*, char*);
  65.  
  66. int main(void){
  67. FILE* fIn = NULL;
  68. char textIn[300];
  69. char fileName[80];
  70. char saveFile = 'y';
  71. bool result = false;
  72.  
  73. printf("Enter in some text to save:");
  74.  
  75. if(fgets(textIn,300,stdin)){
  76. printf("Your entered in %d characters.\n", (int) strlen(textIn));
  77. printf("Enter in the name of a text file: ");
  78. fscanf(stdin,"%s",fileName);
  79.  
  80. if(FileExists(fileName) == true){
  81. printf("The file exists, enter 'y' to overwrite:");
  82. FlushInput();
  83. scanf("%c", &saveFile);
  84.  
  85. if(saveFile == 'y')
  86. result = CreateFile(textIn, fileName);
  87. else
  88. printf("You chose not to save the data.\n");
  89. }
  90. else{
  91. result = CreateFile(textIn, fileName);
  92. }//end if-else
  93. }
  94. else {
  95. printf("You did not enter in anything to save.\n");
  96. }
  97.  
  98. if(result)
  99. printf("You successfully created a file named %s\n", fileName);
  100. else
  101. printf("You did not create a file named %s\n", fileName);
  102.  
  103. return 0;
  104. }//end main
  105.  
  106.  
  107. bool FileExists(char* fileName){
  108. FILE* fIn = fopen(fileName,"r");
  109.  
  110. if(fIn == NULL)
  111. return false;
  112. else{
  113. fclose(fIn);
  114. return true;
  115. }
  116. }//end function
  117.  
  118.  
  119. void FlushInput(void){
  120. while(getchar() != '\n');
  121. return;
  122. }//end function
  123.  
  124.  
  125. bool CreateFile(char* textIn, char* fName){
  126. FILE* fPtr = fopen(fName,"w");
  127.  
  128. if(fPtr == NULL){
  129. printf("Error opening file.\n");
  130. return false;
  131. }
  132.  
  133. else{
  134. fputs(textIn, fPtr);
  135. fclose(fPtr);
  136. return true;
  137. }//end if-else
  138.  
  139. }//end function
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148. Problem 3
  149.  
  150. #include <stdio.h>
  151. #include <stdlib.h>
  152.  
  153. int main () {
  154. int i, n;
  155. int *a = NULL;
  156.  
  157. printf("Number of elements to be entered:");
  158. scanf("%d",&n);
  159.  
  160. a = (int*)calloc(n, sizeof(int));
  161.  
  162. printf("Enter %d numbers:\n",n);
  163. for( i=0 ; i < n ; i++ ) {
  164. scanf("%d",&a[i]);
  165. }
  166.  
  167. printf("The numbers entered are: ");
  168. for( i=0 ; i < n ; i++ ) {
  169. printf("%d ",a[i]);
  170. }
  171.  
  172. free( a );
  173.  
  174. return(0);
  175. }
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185. Problem 4
  186.  
  187. #include <stdio.h>
  188. #include <stdlib.h>
  189. #include <string.h>
  190.  
  191. int main () {
  192. char *str;
  193.  
  194. /* Initial memory allocation */
  195. str = (char *) malloc(13);
  196. strcpy(str, "Maryhadalamb");
  197. printf("String = %s, Address = %u\n", str, str);
  198.  
  199. /* Reallocating memory */
  200. str = (char *) realloc(str, 25);
  201. strcat(str, ", it stunk!");
  202. printf("String = %s, Address = %u\n", str, str);
  203.  
  204. free(str);
  205.  
  206. return(0);
  207. }
Add Comment
Please, Sign In to add comment