Transcript Document

Exercise 10
Review: pointers, strings and
recursion
Pointers – reminder
int nums[] = {1, 2, 3};
char str[] = “moshe”;
int * q = nums;
char * p = str;
1
q
2
3
moshe
p
Pointers – reminder
int nums[] = {1, 2, 3};
char str[] = “moshe”;
int * q = nums;
char * p = str;
1
(q+1)
2
3
moshe
(p+3)
Pointers – reminder
p[0]
p[i]
*p
*(p+i)
p[2] p[4]
q[0] q[1] q[2]
1
q
2
3
moshe
p
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
Solution

str_any.c
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
‘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
‘main’ prototype
int main(int argc, char * argv[])
argc :
3
argv :
progname
moshe
178
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;
}
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)
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>
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
Solution

args_rectangle.c
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
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;
}
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)
Madam, I’m Adam

Write a recursive function that decides
whether a string is palindrome.
Solution

rec_palindrome.c