Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.97 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <learncs.h>
  3. #include <ctype.h>
  4.  
  5. void exercise2(void);
  6. void exercise3(void);
  7. void exercise4(void);
  8. int stringLength(char arr[]);
  9. int countSpaces(char arr[]);
  10. void removeVowels(char arr[]);
  11.  
  12.  
  13. // main()
  14. // Do not change this function at all.
  15. int main(int argc, char * argv[])
  16. {
  17.     exercise2();
  18.     exercise3();
  19.     exercise4();
  20.     printf("\n");
  21.     return 0;
  22. }
  23.  
  24.  
  25. void exercise2(void)
  26. {
  27.     char    arr[17];
  28.     int     magic = 42;
  29.  
  30.     printf("\n--------------------\n");
  31.     printf("EXERCISE 2\n");
  32.     printf("--------------------\n\n");
  33.  
  34.     // Print out the magic value
  35.     printf("magic = %d\n", magic);
  36.    
  37.     // Prompt for string input
  38.     printf("Enter a character string: ");
  39.    
  40.     // Retrieve up to 16 characters (plus the null terminator)
  41.     getString(arr, 16+1);
  42.     printf("The length of string [%s] is %d\n", arr, stringLength(arr));
  43.    
  44.     // Print out the magic value again
  45.     printf("magic = %d\n", magic);
  46.  
  47.     /*
  48.         Because we allocated only 16 + 1 element we go pass the stack creating a buffer overflow.
  49.         This will erase the value for magic due to the overflow.
  50.        
  51.     */
  52.  
  53. }
  54.  
  55.  
  56. void exercise3(void)
  57. {
  58.     int         spaces;
  59.     char        string[] =
  60.       "This is a test of the emergency broadcasting system. This is only a test.";
  61.    
  62.     printf("\n--------------------\n");
  63.     printf("EXERCISE 3\n");
  64.     printf("--------------------\n\n");
  65.    
  66.     // Count the number of spaces in the string.
  67.     spaces = countSpaces(string);
  68.    
  69.     // The original countSpaces() function you are given simply returns -1.
  70.     // If it still does that, it just means that you haven't yet implemented
  71.     // the countSpaces() function according to the Exercise 2 instructions.
  72.     if (spaces == -1)
  73.     {
  74.         printf("This exercise has not been completed yet.\n");
  75.     }
  76.     else
  77.     {
  78.         printf("The number of spaces in [%s] is %d\n", string, spaces);
  79.     }
  80. }
  81.  
  82.  
  83. void exercise4(void)
  84. {
  85.     int         removed;
  86.     char        string[] =
  87.       "Hello world, how are you?";
  88.    
  89.     printf("\n--------------------\n");
  90.     printf("EXERCISE 4\n");
  91.     printf("--------------------\n\n");
  92.    
  93.     // Count the number of spaces in the string.
  94.     removed = removeVowels(string);
  95.    
  96.     // The original removeVowels() function you are given simply returns -1.
  97.     // If it still does that, it just means that you haven't yet implemented
  98.     // the countSpaces() function according to the Exercise 2 instructions.
  99.     if (removed == -1)
  100.     {
  101.         printf("This exercise has not been completed yet.\n");
  102.     }
  103.     else
  104.     {
  105.         printf("%d vowels were removed, yielding [%s]\n", removed, string);
  106.     }
  107. }
  108.  
  109.  
  110. /**
  111.  * Calculate the length of a character string
  112.  *
  113.  * @param arr
  114.  *   The address of the first element of an array of characters containing
  115.  *   the string.
  116.  *
  117.  * @return
  118.  *   The number of characters in the string, not including the string's
  119.  *   null terminator.
  120.  */
  121. int stringLength(char arr[])
  122. {
  123.     int     len;
  124.    
  125.     // Assume initially that the array is length 0.
  126.     len = 0;
  127.    
  128.     // Look at each element of the array. If we find something other than
  129.     // the null terminator, count this character by incrementing the length
  130.     // variable.
  131.     while (arr[len] != '\0')
  132.     {
  133.         // This character wasn't the null terminator, so increment the length
  134.         ++len;
  135.     }
  136.    
  137.     // Give 'em the calculated string length
  138.     return len;
  139. }
  140.  
  141.  
  142. /**
  143.  * Count the number of space characters in a string.
  144.  *
  145.  * @param arr
  146.  *   The address of the first element of an array of characters containing
  147.  *   the string.
  148.  *
  149.  * @return
  150.  *   The number of space characters in the string.
  151.  */
  152. int countSpaces(char arr[])
  153. {
  154.     int i = 0, spaces = 0;
  155.    
  156.     while(arr[i] != '\0')
  157.     {
  158.         if(isspace(arr[i]))
  159.         {
  160.             spaces++;
  161.         }
  162.         i++;
  163.     }
  164.     return spaces;
  165. }
  166.  
  167.  
  168. /**
  169.  * "Remove" each vowel from the provided character array, moving all
  170.  * subsequent characters forward in the array to take up the space of the
  171.  * removed vowel. Only the following characters are considered to be vowels:
  172.  * 'a', 'e', 'i', 'o', and 'u'
  173.  *
  174.  * @param arr
  175.  *   The address of the first element of an array of characters containing
  176.  *   the string whose vowels are to be removed.
  177.  *
  178.  * @return
  179.  *   The number of vowels removed from the provided string.
  180.  */
  181. int removeVowels(char arr[])
  182. {
  183.     int i = 0, vowels = 0;
  184.    
  185.     while(arr[i] != '\0')
  186.     {
  187.         if(arr[i] != 'a' && arr[i] != 'e' && arr[i] != 'i' && arr[i] != 'o' && arr[i] != 'u' )
  188.         {
  189.             arr[vowels] = arr[i]; //copy original to a temp array;
  190.             vowels++; // move on to next element skip vowels
  191.         }
  192.         i++; //move to next element
  193.     }
  194.     //printf("i is %d, vowels is %d", i, vowels);
  195.     return i - vowels; //original array subtract temp array to get how many vowel remove.
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement