Programming II 2024/2025 (CCINF1002) - DCC/FCUP

Practical Class #05 - Strings (25/03 and 26/03)


Exercises for submission

In what concerns the "exercises during classes" component of the evaluation, the exercises that you can submit for this class are:

Deadline for submission: April 7th (submit to PII's Mooshak)

You are encouraged to talk to the professors and your colleagues if you encounter difficulties. However, any more direct help that you receive should be acknowledged in the comments of the code you submit.
After the deadline the problems will still be available on Mooshak, but the submissions will not count towards your grade.
Each practical class is worth 18% of your final component for exercices during class. Since there will be 6 classes with submissions, you can achieve maximum grade even without doing fully doing all classes.
For a problem to count you must pass all tests (that is, to have an accepted). Even if you solve all problems, the maximum on one class is 100%.
To obtain 100% it will always be enough to solve the main exercises.


With the exercises in this class you will develop the following skills:

  1. Explore function definition and calling, argument passing and return statements
  2. Understand that a string it at its essence an array of chars with a terminator character
  3. Understand how to read, print, create and manipulate strings
  4. Understand how to traverse a string and how to do something with it
  5. Know some library functions to work with strings
  6. Consolidate your knowledge of coding, debugging and problem solving

If you feel stuck, go back and revise all the lectures, with special emphasis on 06: Arrays, 08: Basic Types and 09: Strings, .


This week's theme for the exercises is Game of Thrones.


1) Getting started with strings and characters

  1. On this class you will be using functions that receive strings. Here is a very simple example:

    Save the following code to a file, compile, execute it and make sure you understand everything that it is doing:

    #include <stdio.h>
    
    // Function to compute the string length
    // (iterate over all position until terminator is reached)
    int length(char str[]) {
      int len = 0;
      while (str[len] != '\0')
        len++;
      return len;
    }
    
    int main(void) {
    
      // Create 3 strings
      char s1[2] = "c";          // we can use exact size (+1 for '\0')
      char s2[100] = "code";     // we can use larger size than length
      char s3[] = "programming"; // we can ommit size
    
      // Call the length function for each of the 3 strings
      printf("length of \"%s\" is %d\n", s1, length(s1));
      printf("length of \"%s\" is %d\n", s2, length(s2));
      printf("length of \"%s\" is %d\n", s3, length(s3));
          
      return 0;
    }
    

    Here is its output:

    length of "c" is 1
    length of "code" is 4
    length of "programming" is 11
    

     
  2. Now that you understand string basics, let's see an example of working with chars and what is the <ctype.h> header file (see its documentation)

    Save this code to a file, compile, execute it and make sure you understand everything that it is doing:

    #include <stdio.h>
    #include <ctype.h> // A library with utilities for characters
    
    int main(void) {
    
      char c = 'a';
    
      // Test several isX functions
      // . if true, return number different than 0
      // . if false, return 0
      printf("islower(%c) = %d\n", c, islower(c));
      printf("isupper(%c) = %d\n", c, isupper(c));
      printf("isalpha(%c) = %d\n", c, isalpha(c));
      printf("isspace(%c) = %d\n", c, isspace(c));
      printf("isdigit(%c) = %d\n", c, isdigit(c));
    
      // Test several toX functions
      printf("tolower(%c) = %c\n", c, tolower(c));
      printf("toupper(%c) = %c\n", c, toupper(c));
      
      return 0;
    }
    
    

    Here is its output:

    islower(a) = 512
    isupper(a) = 0
    isalpha(a) = 1024
    isspace(a) = 0
    isdigit(a) = 0
    tolower(a) = a
    toupper(a) = A
    c = a; code of c is 97
    c = a; c+1 = b
    c = a; c+2 = c
    

2) My first string functions
 

  • Armed with what you learned from the previous exercise, and also knowing how to work with cycles, arrays and functions, you are now ready to tackle the first problems from this class.


    Read the statement, code and try to submit Accepted solutions to the following problems. Don't forget to test first on your computer!



    3) Reading and storing several strings

    For the next problem you will make again a full program (and not just a function). It will be helpful to store several strings.

    Here is some example code that might help you get started for the next problem. Test the code. Make sure to compile, run and understand what's happening:

    #include <stdio.h>
    
    #define N_STRINGS 3
    #define MAX_SIZE  100
    
    int main(void) {
    
      // Array of strings (array of array of chars)
      char str[N_STRINGS][MAX_SIZE];
    
      // Read N_STRINGS
      for (int i=0; i<N_STRINGS; i++)
        scanf("%s", str[i]);
    
      // Print N_STRINGS
      for (int i=0; i<N_STRINGS; i++)
        printf("str[%d] = \"%s\"\n", i, str[i]);
        
      return 0;
    }

    For instance, if we give the code the following input:

    um dois tres
    

    Then the output would be:

    str[0] = "um"
    str[1] = "dois"
    str[2] = "tres"
    

     

    You are now ready to solve the next problem:


    Read the statement, code and try to submit an Accepted solution to the following problem. Don't forget to test first on your computer!



    Extra exercises for consolidating your knowledge [extra]

    Do you want to continue your adventure on the world of Westeros? Here are more exercises 🙂


    Read the statements, code and try to submit Accepted solutions to all of the following problems. Don't forget to test first on your computer!

    1. [IP038] Whispers in the Shadows
       
      Hints

      - Our suggestion is to create a function to check if a key appears exactly at position i on the string (simpler problem than original)

      - Solving the problem is now traversing all positions and calling the previously defined function to increment a counter if there is a match on that position


       
    2. [IP039] You know nothing, Jon Snow
       
      Hints

      - Our suggestion is to create a function to transform the current string into the next generation

      - You can use an auxiliary temporary string to build the next string

      - The problem can now be solved by a simple cycle that calls the previous function n times and keeps printing the current string


       

    Challenge exercise [challenge]

    Congratulations, brave solver of the Game of Strings! 🎉

    You have demonstrated the skill and the mind of a true Maester, solving each riddle from the cold North to the burning South of Westeros. But beware, for one final challenge remains. 🐉🔥

    Daenerys Targaryen, the Mother of Dragons, is not easily satisfied. There is one last puzzle you must conquer, one final problem to prove your worth. Succeed, and you will earn the respect of the Seven Kingdoms. Fail, and you may find yourself facing the fiery wrath of the dragons...

    This challenge is of algorithmic nature, so you need and efficient solution (both on memory and space) and not just a correct (but more "brute force"). Note that this is until now the (algorithmically) most difficult exercise that was proposed on this course 😈.

    Give yourself some time to think about it an be proud if you make it! Be sure to chat with your professors for hints or to proudly talk about how you solve it 😁

    Happy coding! 😊