code | comments |
int count_char_in_string(char c, char *s) | // count_char_in_string is a function that
takes in // a character c and a string s, and returns an integer |
{ int count = 0, i = 0; | // declare count and i (the current position in the
string), // both integers initialized to zero |
while (s[i] != '\0') | // while we are not at the end of the string |
{ if (s[i] == c) count++; | // if the current character is equal to c, increment count |
i++; } | // otherwise move on to the next character |
return count; } | // done counting, so return count |
void increment(int i) { i++; }Now let's try to predict what would happen if we were to execute this block:
{ int x = 2; increment(x); printf("%d\n", x); }What value would be printed?
code | comments |
int count_char_in_string(char c, char *s) | // count_char_in_string is a function that
takes in // a character c and a string s, and returns an integer |
{ int count = 0; | // declare count and initialize it to zero |
while (*s != '\0') | // while the value pointed to by s is not
'\0' // (i.e. while we are not at the end of the string |
{ if (*s == c) count++; | // if the current character is equal to c, increment count |
s++; } | // otherwise move on to the next character |
return count; } | // done counting, so return count |
void make_empty(char *s) { *s = '\0'; }Although we never changed the value of s, we made a permanent change to the character s points to. This means that if you executed the following block:
{ char *s = "hello"; printf("%s\n", s); make_empty(s); printf("%s\n", s); }The output would be:
helloThe first call to printf() printed the word hello, then we called make_empty(), which put a '\0' in the first character of string s, thus making it an empty string. That's why the second line of the output is empty (actually, it is the newline character printed by printf()).
code | comments |
int find_first_occurence(char c, char *s) | // find_first_occurence is a function that
takes in // a character c and a string s, and returns an integer |
{ int pos = 0; | // declare pos (the current position) and initialize it to
// zero (we'll start at the first character of s). |
while (s[pos] != '\0') | // while we are not at the end of the string |
{ if (s[pos] == c) return pos; | // if the current character is equal to c, return the current position |
pos++; } | // otherwise move on to the next character |
return -1; } | // if we ever get this far, c never appears in s // so we return -1 |
code | comments |
int find_first_occurence(char c, char *s) | // find_first_occurence is a function that
takes in // a character c and a string s, and returns an integer |
{ int pos = 0; | // declare pos (the current position) and initialize it to
// zero (we'll start at the first character of s). |
while (*s != '\0') | // while we are not at the end of the string |
{ if (*s == c) return pos; | // if the current character is equal to c, return the current position |
s++; pos++; } |
// otherwise move on to the next character, // without forgetting to update the position |
return -1; } | // if we ever get this far, c never appears in s // so we return -1 |
code | comments |
int find_first_occurence(char c, char *s) | // find_first_occurence is a function that
takes in // a character c and a string s, and returns an integer |
{ char *orig = s; | // declare orig (the original string) and initialize it to s. |
while (*s != '\0') | // while we are not at the end of the string |
{ if (*s == c) return s - orig; | // if the current character is equal to c, return the current position (explained later) |
s++; } |
// otherwise move on to the next character, |
return -1; } | // if we ever get this far, c never appears in s // so we return -1 |