Answer to Quiz 3 Fri 26 Jan 2001
1. Write next to the CountChar the equivalent pointer version.
Your answer may NOT contain any bracket [ or ]!
int CountChar(char s[], char c) int CountChar(char *s, char c)
{ {
int i,cnt=0; int cnt=0;
char *p;
for (i=0; s[i]!='\0'; ++i) for (p=s; *p !='\0';p++)
cnt += (s[i] == c); cnt += (*p == c);
return cnt; return cnt;
} }
2. The input to
void FirstChars(int n, char *words[], char init[])
is an array words of n strings.
FirstChars fills up init with the first character
of every string in words.
Assume init is an array of size > n.
Write the C code for FirstChars.
void FirstChars(int n, char *words[], char init[])
{
int i;
for (i=0; i<n; ++i)
init[i] = words[i][0];
init[n] = '\0';
}