Strings and Functions
A string is an array of char values, so a function gets it as a parameter just like any other array:
#include <stdio.h>
void introduce(const char name[]) {
printf("My name is %s.\n", name);
}
int main(void) {
char friend_name[] = "Tom";
introduce("Anna");
introduce(friend_name);
return 0;
}This program prints:
My name is Anna.
My name is Tom.You can pass a text in quotes or a char array. Two things are different from the arrays of numbers in the previous exercise:
- There is no count. A string ends with the null character
'\0', so the function can find its end by itself: with a loop that stops at'\0', or withstrlen. - Before the type, there's the word
const, which you know from constants. It is a promise that the function only reads the string and doesn't change it. If the function tried, for example withname[0] = 'X';, the program wouldn't compile:error: read-only variable is not assignable(you can't assign to a variable that is only for reading). Writeconstwhenever a function only reads a string. Then anyone who reads the function knows at a glance that the text stays as it is.
A function can go through a string in a loop and return a result. This one counts how many times a letter appears in a text:
#include <stdio.h>
#include <string.h>
int count_letter(const char text[], char letter) {
int count = 0;
int length = strlen(text);
for (int i = 0; i < length; i++) {
if (text[i] == letter) {
count++;
}
}
return count;
}
int main(void) {
printf("%d\n", count_letter("banana", 'a'));
printf("%d\n", count_letter("Mississippi", 's'));
return 0;
}This program prints:
3
4A function without const may change the string. Just like with other arrays, the change stays in the array of the caller:
#include <ctype.h>
#include <stdio.h>
void make_upper(char text[]) {
for (int i = 0; text[i] != '\0'; i++) {
text[i] = toupper(text[i]);
}
}
int main(void) {
char message[] = "hello";
make_upper(message);
printf("%s\n", message);
return 0;
}This program prints:
HELLOOne thing a function can't do is return an array, so it can't return a new string the way it returns a number. When a function should produce a text, it writes it into a char array that the caller passes to it, just as make_upper changes message.
Instructions
Above main, write a function announce_player that returns nothing and has one parameter, name: a string that the function only reads. The function prints a line such as Your turn, Eva!, with the name it gets.
In main, create an array player with the text "Eva" and call announce_player(player). Then call announce_player("Adam"). The program prints Your turn, Eva! and Your turn, Adam!.
Below announce_player, write a function count_digits with a parameter text, again a string that the function only reads. It returns how many digits the text contains; isdigit from ctype.h tells you whether a character is a digit. At the end of main, before return 0;, print the result of count_digits("summer2024") with a label: Digits: 4.
Start programming for free
10/12