Advertisement
Guest User

cpscp259

a guest
Sep 23rd, 2014
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.60 KB | None | 0 0
  1. #include "lab2_in-lab_exercises.h"
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define SMALL_ARRAY 25
  6.  
  7. /*
  8. Main function drives the program. Every C program must have one
  9. main function. A project will not compile without one.
  10. PRE: NULL (no pre-conditions)
  11. POST: NULL (no side-effects)
  12. RETURN: IF the program exits correctly
  13. THEN 0 ELSE 1
  14. */
  15. int main ( void )
  16. {
  17. /* We start every function with a list of variables */
  18. int i = 0;
  19. int * pointer_to_some_ints;
  20.  
  21. /* Then we do things, like invoke functions and assigns their return
  22. values to variables. You don't need to change these two lines */
  23. pointer_to_some_ints = ( int * ) malloc( SMALL_ARRAY * sizeof( int ) );
  24. for ( i = 0; i < SMALL_ARRAY; ++i ) {
  25. *( pointer_to_some_ints + i ) = i;
  26. }
  27.  
  28. // You can set a breakpoint here later in order to test the debugger 'watch' tab for pointers
  29.  
  30. /* The system command forces the system to pause before closing executable window */
  31. system("pause");
  32. return 0;
  33. }
  34.  
  35. /*
  36. * Let's start with something easy. There are 3 unit tests
  37. * for this function.
  38. *
  39. * Swaps the contents of two integer variables using pointers
  40. *
  41. * PARAM: first_int is a pointer to an int
  42. * PARAM: second_int is a pointer to an int
  43. * PRE: both pointers are valid pointers to int
  44. * POST: the contents of two integer variables are swapped
  45. * RETURN: VOID
  46. */
  47. void swap_ints( int * first_int, int * second_int )
  48. {
  49. int temp = *first_int;
  50. *first_int = *second_int;
  51. *second_int = temp;
  52. }
  53.  
  54. /*
  55. * Now let's try something a little more challenging.
  56. *
  57. * Reverses the contents of the string passed to the
  58. * function. Does not move the terminating null '\0'
  59. * character.
  60. *
  61. * PARAM: string is a pointer to an array of char (a string)
  62. * PRE: the array of char terminates with a null '\0'
  63. * POST: the char array has been reversed
  64. * RETURN: VOID
  65. */
  66. void reverse_string( char * string )
  67. {
  68. int start = 0, end = strlen(string)-1;
  69. char temp;
  70.  
  71. while (start < end) {
  72. temp = *(string+start);
  73. *(string+start) = *(string+end);
  74. *(string+end) = temp;
  75. start++;
  76. end--;
  77. }
  78. }
  79.  
  80. /*
  81. * Let's see how well you read the lab document.
  82. *
  83. * Determines if candidate contains sample, and returns
  84. * 1 if it does, and 0 if it does not.
  85. *
  86. * PARAM: candidate is a pointer to an array of char (a string)
  87. * PARAM: sample is a pointer to an array of char (a string)
  88. * PRE: the arrays of char terminate with a null '\0'
  89. * PRE: candidate != NULL; sample != NULL
  90. * POST: N/A
  91. * RETURN: IF candidate contains sample THEN 1
  92. * ELSE 0.
  93. */
  94. int contains_sample( char * candidate, char * sample )
  95. {
  96. if( strstr(candidate, sample) )
  97. return 1;
  98. else
  99. return 0;
  100. }
  101.  
  102. /*
  103. * Returns the first index where sample is located inside the
  104. * candidate. For example:
  105. * IF candidate = "Hello", sample = "Hello", RETURNS 0
  106. * IF candidate = "soupspoon", sample = "spoon", RETURNS 4
  107. * IF candidate = "ACGTACGTA", sample = "CGT", RETURNS 1
  108. * IF candidate = "CGTACGTA", sample = "CGTT", returns -1
  109. *
  110. * PARAM: candidate is a pointer to an array of char (a string)
  111. * PARAM: sample is a pointer to an array of char (a string)
  112. * PRE: the arrays of char terminate with a null '\0'
  113. * POST: N/A
  114. * RETURN: IF candidate contains sample
  115. * THEN the index where the first letter of sample is inside candidate
  116. * ELSE -1.
  117. */
  118. int find_index( char * candidate, char * sample )
  119. {
  120. char * check = strstr(candidate, sample);
  121.  
  122. if ( check )
  123. return (check-candidate);
  124. else
  125. return -1;
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement