1 / 18

Exercise 10

Exercise 10. Review: pointers, strings and recursion. m o s h e. 1 2 3. Pointers – reminder. int nums[] = {1, 2, 3}; char str[] = “moshe”; int * q = nums; char * p = str;. q. p. m o s h e. 1 2 3. Pointers – reminder. int nums[] = {1, 2, 3}; char str[] = “moshe”;

abner
Download Presentation

Exercise 10

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Exercise 10 Review: pointers, strings and recursion

  2. m o s h e 1 2 3 Pointers – reminder int nums[] = {1, 2, 3}; char str[] = “moshe”; int * q = nums; char * p = str; q p

  3. m o s h e 1 2 3 Pointers – reminder int nums[] = {1, 2, 3}; char str[] = “moshe”; int * q = nums; char * p = str; (q+1) (p+3)

  4. m o s h e 1 2 3 Pointers – reminder p[0] *p p[i] *(p+i) p[2] p[4] q[0] q[1] q[2] q p

  5. Exercise with pointers and strings • Implement the following function: char * str_any(char *str1, char *str2); • Input – two strings str1, str2 • Output – pointer to the first instance in str1 of any of the characters contained in str2 • Write a program that accepts a string from the user and replaces all punctuation signs (,.;:!?) with spaces

  6. Solution • str_any.c

  7. Command line arguments • Command line arguments are arguments for the main function • Recall that main is basically a function • It can receive arguments like other functions • The ‘calling function’ in this case is the operating system, or another program

  8. ‘main’ prototype int main(int argc, char * argv[]) • When we want main to accept command line arguments, we must define it like this • argc holds the number of arguments that were entered by the caller • argv is an array of pointers to char – an array of strings – holding the text values of the arguments • The first argument is always the program’s name

  9. ‘main’ prototype int main(int argc, char * argv[]) 3 argc : argv : p r o g n a m e m o s h e 1 7 8

  10. Example /* This program displays its command-line arguments */ #include <stdio.h> int main(int argc, char *argv[]) { int i; printf("The program's command line arguments are: \n"); for (i=0; i<argc; i++) printf("%s\n", argv[i]); return 0; }

  11. Specifying the arguments • We can specify to the Visual Studio compiler what command line arguments we want to pass to our program • Project  Settings  Debug, in the ‘program arguments’ field • We can also specify the arguments directly, by using the Windows console (StartRun…, then type ‘cmd’ and drag the executable into the window. Then type the arguments and Enter)

  12. Helper functions – atoi/atof int atoi(char s[]); double atof(char s[]); • Command line arguments are received in the form of strings • These functions are used when we want to transform them into numbers • For example – atof(“13.5”) returns the number 13.5. • Must #include <stdlib.h>

  13. Exercise • Write a program that accepts two numbers as command line arguments, representing a rectangle’s height and width (as floating-point numbers). • The program should display the rectangle’s area and perimeter

  14. Solution • args_rectangle.c

  15. Recursion – reminder • Recursion: any function that calls itself • recursion base: function does something, but does not call itself • recursion rule: function does something and calls itself with a different input • Recursion can always be replaced by some loop

  16. Exercise • What does this function do? What does it assume about its input string? #include <string.h> int secret(char *str) { int val, len; if (str[0]=='\0') return 0; len = strlen(str); val = str[len - 1] – '0'; str[len - 1] = '\0'; return val + secret(str)*10; }

  17. Recursive palindrome • Palindrome is a string that reads the same from left to right and from right to left (ignoring case, spaces, commas and anything else that is not a letter) • Write a recursive function that decides whether a string is palindrome. Madam, I’m Adam

  18. Solution • rec_palindrome.c

More Related