Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

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 with strlen.
  • 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 with name[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). Write const whenever 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
4

A 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:

HELLO

One 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

  1. 1

    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.

  2. 2

    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!.

  3. 3

    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.

Back

10/12

  1. 1. Defining Functions
  2. 2. Calling Functions
  3. 3. Function Prototypes
  4. 4. Parameters and Arguments
  5. 5. Return Values
  6. 6. Print or Return?
  7. 7. return Ends the Function
  8. 8. Local Variables
  9. 9. Arrays and Functions
  10. 10. Strings and Functions
  11. 11. Math Functions
  12. 12. Summary
Next

Try it yourself

Do this exercise right here in your browser. No account needed.