Intro to Programming Languages

LecLabs 2.2 on Arrays and Strings

[ ← Module List ]

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

19 challenges

Challenges

CHALLENGE 01

Lecture 2.2.1.1. C-Arrays

— solves

Lecture video on C-Arrays

CHALLENGE 02

Lecture 2.2.2. C-Strings

— solves

Lecture video on C-Strings

CHALLENGE 03

Lab 2.2.2.0 - Print C-String by Character

— solves

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
CHALLENGE 04

Lab 2.2.2.1 - Debugging with Seg Faults

— solves

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 description in the test case
  • Run /challenge/tester
  • Get flag
CHALLENGE 05

Lecture 2.2.3.1. Input to String

— solves

Lecture video on Input to String

CHALLENGE 06

Lecture 2.2.3.2 Using fgets

— solves

Lecture video on Using fgets

CHALLENGE 07

Lecture 2.2.3.3. Input Buffer

— solves

Lecture video on Input Buffer

CHALLENGE 08

Lecture 2.2.4. File operations

— solves

Lecture video on File operations

CHALLENGE 09

Lab 2.2.4.1 - LeeT CaSe

— solves

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 description 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
CHALLENGE 10

Lecture 2.2.5.1. strlen

— solves

Lecture video on strlen

CHALLENGE 11

EzLab 2.2.5.1 - String Length (strlen)

— solves

Try out using strlen.

CHALLENGE 12

Lecture 2.2.5.2. strcat

— solves

Lecture video on strcat

CHALLENGE 13

EzLab 2.2.5.2 - String Concatenate (strcat)

— solves

Try out using strcat.

CHALLENGE 14

Lecture 2.2.5.3. strcmp

— solves

Lecture video on strcmp

CHALLENGE 15

EzLab 2.2.5.3 - String Compare (strcmp)

— solves

Try out using strcmp.

CHALLENGE 16

Lecture 2.2.5.4. strcpy

— solves

Lecture video on strcpy

CHALLENGE 17

EzLab 2.2.5.4 - String Copy (strcpy/strncpy)

— solves

Try out using strcpy and strncpy.

CHALLENGE 18

Lecture 2.2.5.6. strspn

— solves

Lecture video on strspn

CHALLENGE 19

Lab 2.2.5.6 - String Highlighter

— solves

Objective

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

CTRL-Click Here for Follow Along Video

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
    • Remove the newline character from the end of each line read from the file (if present)
    • Process each line of the file character by character
      • Set the word variable to an empty string (hint set position 0 to null terminator)
      • Copy the characters to the word variable until a space or the end of the line is reached
      • Set the location after the last character copied to null terminator (to end the word)
      • If a word in the line matches the user's keyword (strcmp == 0)
        • If strcmp is 0 then, surround the keyword in the line with asterisks (**) on each side and copy to the result c-string
        • If strcmp is not 0, then copy the current word to the result c-string
      • Add a space to the result c-string
  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