Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This week we're covering pointers, and you should have completed all the course material on pointers by now, so you can start thinking about how

This week we're covering pointers, and you should have completed all the course material on pointers by now, so you can start thinking about how best to approach this exercise.

The goal of this exercise is to:

  • help you practice using pointers to access information that is not declared inside some function
  • help you practice using pointers to change information that lives outside a function
  • help you better understand the relationship between strings, arrays, and pointers

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

This week we're covering pointers, and you should have completed all the course material on pointers by now, so you can start thinking about how best to approach this exercise. The goal of this exercise is to: help you practice using pointers to access information that is not declared inside some function help you practice using pointers to change information that lives outside a function help you better understand the relationship between strings, arrays, and pointers Conditions: You are not allowed to use the string library string. h . The point of the exercise is for you to write the functions that handle the strings. You are not allowed to use array notation. Use only pointer syntax for all your functions and helpers. In this exercise, you will write functions to: fin the last occurrence of a substring in a given string extract all the vowels from a sentence and put them in a new string Download Starter Code: ex3.ca The starter code contains documentation and some example output for the functions, as well as a few tests to help you check your code. The tests in the main function are not comprehensive. You should test your code on many different input parameters yourself to ensure that they actually work as intended. What to do: Implement the functions as needed. Add helper functions if you want to make your code clearer. Test your code on your own inputs What NOT to do: Import any extra libraries, especially string.h Use array notation for anything other than in maino to initialize and declare strings Change the name of any functions in the starter code Change or remove the #ifndef _testing and #endif lines Compilation: Starting this exercise, we will start practicing compiling with more stricter settings. We will add two flags to the compiler call. -Wall Uses more strict checking for warnings, and reports anything it finds. Makes the compiler treat the warnings as if they were errors so you have to fix them in order to obtain an executable program. Please note - this flag may not be available or work properly on Mac, so it's up to you to treat warnings as errors and make sure you clear all of them. -Werror So, for instance, you might compile this exercise using: gcc -Wall-Werror ex3.c We will compile your exercise code, as well as any code you submit from now on, using these flags. Your code will not compile on our test machine if warnings have not been cleared. * Exercise 8. Fun with Painters 1 2 3 4 5 * Please read the comments below carefully, they describe your task in detail. * Any clarifications and corrections will be posted to Piazza so please keep + an eye on the forum! 6 B * NOTE: You should NOT use array notation inside your functions. Use only 9 * pointer notation for any code that you write. 10 11 * Starter code: Mustafa Quraish, 2020 12 13 14 include 15 include 16 // NO additional imports allowed. You *can* make helper functions if you wish. 17 18 #define HAX_STRING_LEN 1824 19 20 char *find_last_substring(char *str, char *a] 211 21 22 23 + Given a string 'str and another string '2', return a pointer to the start 24 * of the "LASTCccurrence of a instr" (Unlike x2, where you had to 25 * find the FIRST occurrence 26 27 For instance, we had: (here) 28 V 29 char strMAX_STRING_LEN = "Hello everyone, Hello world! char *res find last substring(str, 'Helo): 31 + ther, we would except res to be a painter to the character marked above. 33 * In particular, since the second "Hello" is at index 16, we should get 34 the following: 35 36 * ros st 16 (This is pointer arithmetic) 37 39 #lais nol a valid substring of str. returri NULL. 39 43 41 return NULL; // Replace with correct return 42 } 43 le void split_vowels (char *str, char *vowels) 45 { 46 * Move all of the vowels from str (in the order of appearance) a the 43 * string 'vowels (1o, after the function call'str should not have any 49 * vowels in it). You may assume there is enough space allocated in vowels. 53 * 51 * Look at the test case below for an example 52 53 54 return; // Not returning anything. 'str' and 'Vowels' modified directly. 55 ) 56 57 // 59 // Do nat change the lines above and below the main function. These // are here to ensure that the main() function defined here does not // Contlict with the automarker when testing your code. Changing them // will result in a @ for this exercise. You are free to change anything // inside the main() function itself. #ifndef __testing - int main() char my_str(HAX_STRING_LEN] = 'many many people have many many hobbies"; char *res - find_last_substring(my_str, "nany"); if (res - my str == 27) C printf("- find_last_substring() returned the correct result! "); else printf("I find_last_substring(0) did not work properly. "); > printti - "); char str[MAX_STRING_LEN] = "This sentence has many vowels! AEIOU"; char vowels[MAX_STRINO_LEN); split_vowels(str, vowels); printf("Expected Result) str: \"Ths sntnc hs mny vwls! \"," "vowels: \"icccaacAEIOU\" "); printf("(Your Solution) str: \"%s\", vowels: \"%s\" ", str, vowels); } #Endir This week we're covering pointers, and you should have completed all the course material on pointers by now, so you can start thinking about how best to approach this exercise. The goal of this exercise is to: help you practice using pointers to access information that is not declared inside some function help you practice using pointers to change information that lives outside a function help you better understand the relationship between strings, arrays, and pointers Conditions: You are not allowed to use the string library string. h . The point of the exercise is for you to write the functions that handle the strings. You are not allowed to use array notation. Use only pointer syntax for all your functions and helpers. In this exercise, you will write functions to: fin the last occurrence of a substring in a given string extract all the vowels from a sentence and put them in a new string Download Starter Code: ex3.ca The starter code contains documentation and some example output for the functions, as well as a few tests to help you check your code. The tests in the main function are not comprehensive. You should test your code on many different input parameters yourself to ensure that they actually work as intended. What to do: Implement the functions as needed. Add helper functions if you want to make your code clearer. Test your code on your own inputs What NOT to do: Import any extra libraries, especially string.h Use array notation for anything other than in maino to initialize and declare strings Change the name of any functions in the starter code Change or remove the #ifndef _testing and #endif lines Compilation: Starting this exercise, we will start practicing compiling with more stricter settings. We will add two flags to the compiler call. -Wall Uses more strict checking for warnings, and reports anything it finds. Makes the compiler treat the warnings as if they were errors so you have to fix them in order to obtain an executable program. Please note - this flag may not be available or work properly on Mac, so it's up to you to treat warnings as errors and make sure you clear all of them. -Werror So, for instance, you might compile this exercise using: gcc -Wall-Werror ex3.c We will compile your exercise code, as well as any code you submit from now on, using these flags. Your code will not compile on our test machine if warnings have not been cleared. * Exercise 8. Fun with Painters 1 2 3 4 5 * Please read the comments below carefully, they describe your task in detail. * Any clarifications and corrections will be posted to Piazza so please keep + an eye on the forum! 6 B * NOTE: You should NOT use array notation inside your functions. Use only 9 * pointer notation for any code that you write. 10 11 * Starter code: Mustafa Quraish, 2020 12 13 14 include 15 include 16 // NO additional imports allowed. You *can* make helper functions if you wish. 17 18 #define HAX_STRING_LEN 1824 19 20 char *find_last_substring(char *str, char *a] 211 21 22 23 + Given a string 'str and another string '2', return a pointer to the start 24 * of the "LASTCccurrence of a instr" (Unlike x2, where you had to 25 * find the FIRST occurrence 26 27 For instance, we had: (here) 28 V 29 char strMAX_STRING_LEN = "Hello everyone, Hello world! char *res find last substring(str, 'Helo): 31 + ther, we would except res to be a painter to the character marked above. 33 * In particular, since the second "Hello" is at index 16, we should get 34 the following: 35 36 * ros st 16 (This is pointer arithmetic) 37 39 #lais nol a valid substring of str. returri NULL. 39 43 41 return NULL; // Replace with correct return 42 } 43 le void split_vowels (char *str, char *vowels) 45 { 46 * Move all of the vowels from str (in the order of appearance) a the 43 * string 'vowels (1o, after the function call'str should not have any 49 * vowels in it). You may assume there is enough space allocated in vowels. 53 * 51 * Look at the test case below for an example 52 53 54 return; // Not returning anything. 'str' and 'Vowels' modified directly. 55 ) 56 57 // 59 // Do nat change the lines above and below the main function. These // are here to ensure that the main() function defined here does not // Contlict with the automarker when testing your code. Changing them // will result in a @ for this exercise. You are free to change anything // inside the main() function itself. #ifndef __testing - int main() char my_str(HAX_STRING_LEN] = 'many many people have many many hobbies"; char *res - find_last_substring(my_str, "nany"); if (res - my str == 27) C printf("- find_last_substring() returned the correct result! "); else printf("I find_last_substring(0) did not work properly. "); > printti - "); char str[MAX_STRING_LEN] = "This sentence has many vowels! AEIOU"; char vowels[MAX_STRINO_LEN); split_vowels(str, vowels); printf("Expected Result) str: \"Ths sntnc hs mny vwls! \"," "vowels: \"icccaacAEIOU\" "); printf("(Your Solution) str: \"%s\", vowels: \"%s\" ", str, vowels); } #Endir

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Essential SQLAlchemy Mapping Python To Databases

Authors: Myers, Jason Myers

2nd Edition

1491916567, 9781491916568

More Books

Students also viewed these Databases questions