Download presentation
Presentation is loading. Please wait.
1
Exercise 10 Review: pointers, strings and recursion
2
Pointers – reminder int nums[] = {1, 2, 3}; char str[] = “moshe”; int * q = nums; char * p = str; 1 2 3 m o s h e q p
3
Pointers – reminder int nums[] = {1, 2, 3}; char str[] = “moshe”; int * q = nums; char * p = str; 1 2 3 m o s h e (q+1) (p+3)
4
Pointers – reminder p[i] *(p+i) 1 2 3 m o s h e q p q[1]q[2]q[0] p[2]p[4] p[0] *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 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 int main(int argc, char * argv[])
9
‘main’ prototype int main(int argc, char * argv[]) argc : 3 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 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 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 int atoi(char s[]); double atof(char s[]);
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 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.