LecLabs 2.2 on Arrays and Strings


Intro to Programming Languages.

[ ← Module List ]

This module introduces the basics of arrays and strings in C.


Challenges

Lecture video on C-Arrays

Lecture video on C-Strings

Objective

Work with c-strings and print them out character by character.

CTRL-Click Here for Follow Along Video

Requirements

  1. Include the Standard Input/Output Library
    • At the top of your file, include the standard I/O library with #include .
  2. Define the Function to Print Strings
    • Write a function named print_string that takes a character array str[] as a parameter.
    • Inside the function, use a loop to iterate through each character of the array until it encounters a null character (0 or '\0' or '\x00').
    • Print each character one at a time
  3. Define the main Function
    • Define the main function with the parameters int argc and char *argv[].
    • Use a for loop to iterate over each argument passed to the program, starting from argv[1] to argv[argc-1].
    • Inside the loop
      • Print the argument index with "arg : "
      • Call the print_string function with the current argument to print the argument's string value.
      • Print a newline character after each argument with "\n".
  4. Return Statement
    • At the end of the main function, include a return 0; statement to indicate successful completion of the program.
  5. Compile the Program
    • Save your file and compile it using gcc.
    • gcc -o main.bin main.c
  6. Run the Program
    • Execute the compiled program from the command line, passing several arguments to it.
    • Example command: ./main.bin string1 string2 string3
  7. Test the code
    • Fill out the user test that will test whether the arguments get printed out
    • Run the tester
    • Get the flag

Objective

Improve your debugging skills by identifying and fixing errors in the provided C code.

CTRL-Click Here for Follow Along Video

Program specification

  • Print out the array of Score values using print_grades
  • Calculate the sum of the Score values in sum_scores
  • Print out the calculated sum

Steps to Complete

  • Debug the provided program and fix the errors so that it meets the specifications above
  • Compile and test the program
  • Create 2 user test cases, using the desciption in the test case
  • Run /challenge/tester
  • Get flag

Lecture video on Input to String

Lecture video on Using fgets

Lecture video on Input Buffer

Lecture video on File operations

Objective

Change the arguments to be formatted with all consonants being uppercase.

CTRL-Click Here for Follow Along Video

Requirements

  1. Input Handling
    • The program a command-line argument that is a path to an input file.
    • The input can be a single or multiple words that are space delimited
  2. String Processing
    • The program must define a function leet_print that takes a single string as input.
    • Inside leet_print:
      • Iterate over each character of the string.
      • If a character is a vowel ('a', 'e', 'i', 'o', 'u'), it remains unchanged.
      • If a character is a consonant, convert it to its uppercase form.
      • The modified string is then printed to the console.
  3. Output
    • The program must print each argument string after processing, preceded by the argument number (starting from 1).
  4. Main Function Behavior
    • Check if the program was run with argument 1, if not printf("Usage: %s ", argv[0]); and return 0
    • Open the file
    • Use fgets and a while loop to loop through the file
      • Pass each line to leet_print
      • Print a new line
  5. Constraints
    • The program should only process alphabetic characters; non-alphabetic characters remain unchanged.
    • The program should handle any number of command-line arguments.
    • Assume the input strings are null-terminated.

Steps to Complete

  • Implement the requirements
  • Compile and test the program
  • Create 1 user test casesuse the desciption in the test case
    • Create a file in the user_tests directory and fill with a sentence or two
    • Add an argument to the "args" field that gives the path and filename of the input file (e.g., user_tests/input4.3.1.dat)
    • Determine the proper output of the program and add to output
  • Run /challenge/tester
  • Get flag

Lecture video on strlen

Try out using strlen.

Lecture video on strcat

Try out using strcat.

Lecture video on strcmp

Try out using strcmp.

Lecture video on strcpy

Try out using strcpy and strncpy.

Lecture video on strspn

Objective

Add the missing code to create a program that will highlight user designated words.

Requirements

  1. Input Handling
    • The program must take a file name as an argument and open the file for reading.
    • The file will contain a list of words (aka a sentence), with one sentence per line.
    • The program will read a keyword from the user via standard input (stdin).
  2. File Processing
    • Get user's keyword from standard input
    • Read from the provided file
    • Process each line of the file
    • If a word in the line matches the user's keyword
      • If it is, surround the keyword in the line with asterisks (**) on each side.
      • If it is not, do not modify the keyword.
  3. Output
    • The program should print the modified text to standard output (stdout).

Steps to Complete

  • Implement the requirements (see CODE comments in main.c)
  • Compile and test the program
  • Create a test file with a sentence or two
  • Test the code's functionality by running the program with the test file as an argument and entering a keyword when prompted.
  • Once working correctly, run /challenge/tester
  • Get flag

30-Day Scoreboard:

This scoreboard reflects solves for challenges in this module after the module launched in this dojo.

Rank Hacker Badges Score