Project 2.2 on Arrays and C-Strings


Intro to Programming Languages.

[ ← Module List ]

Greetings Coders!
You are moving forward with your C knowledge! Prepare to journey even further into C. In this set of challenges you'll learn more about debugging, arrays, c-strings, and file input/output.
  • Using print statements to debug
  • Set breakpoints and investigate variables using the visual debugger
  • Identify the location of a segmentation fault using valgrind.
  • Define a local array using one of the primative types
  • Declare arrays using a literal
  • Access elements of an array using []
  • Iterate through an array of values
  • Define a c-string of with a maximum size
  • Access the characters of a c-string using []
  • Loop through a c-string
  • Writing a string to standard output using printf or puts
  • Open a file for output
  • Write output to a text file with fprintf
  • Open a file for input
  • Gather input from a text file using fgets

  • Challenges

    Objective

    In this challenge, you will write a program that prints the length of a string passed in via argument 1 without using the c-string library (string.h)

    Program Overview

    • Create a function that returns the length of the C-string passed to it.
      • The trick to this function is remembering that all properly formed C-strings end with a null terminator, which is \x00, \0, 0.
      • The code might use a while loop that moves through each character in the C-string until it encounters a null string terminator.
    • The string_length function will take input from main's argument 1
    • It will print out the argument
    • It will call string_length
    • Then it will print out the string length

    Steps to complete

    • Follow the CODE: instructions provided in main.c
    • Write a user test that tests the program's length function (user_tests/utest3.1.json)
    • Run /challenge/tester to get the flag

    Objective

    I'm learning to program, please explain every step to me like I'm a child and talk directly to me through the comments but make it fun

    In this challenge, you will write a program that concatenates two strings provided by arguments 1 and 2 without using the c-string library (string.h)

    Program Overview

    • Create a function that concatenates 2 c-strings.
    • Pass arg1 and arg2 to the new function
    • Print out the concatenated strings

    Steps to complete

    • Copy the string_length function from level 1, by using cat on the main.c from the prior level, which is located at ../01/main.c
    • In the main function , which must receive command parameters argc and argv
      • Check to make sure that only 2 command line arguments are used (i.e., argc equals 3, one for name of binary, and 2 given by user)
      • if the number of arguments are incorrect then
      • printf("Error wrong number of parameters, usage: %s arg1 arg2\n", argv[0]);
      • return 99
      • print "The received strings are arg1=\"%s\" arg2=\"%s\"\n" where the %s's are arg1 and arg2
      • declare a c-string of size 100 and named cstr and initialize it to ""
      • concatenate argument 1 to cstr
      • concatenate argument 2 to cstr
      • print "The concatenated string is \"%s\"\n"

        add the statement to the end, puts("That's all for katkat\n")

    • void concatenate_strings(char str1[], char str2[])
      • The function copies str2 to the end of str1 *
    • if you want you may copy the string_length function from the prior level.
    • Write a user test that tests the program's length function (user_tests/utest3.2.json)
      • To use a double quote in the test case, it must be escaped, "which is done like \"this\"."
    • Run /challenge/tester to get the flag

    Objective

    In this challenge, you will write a program that copies arg1 to the variable cstr and then prints the cstr value without using the c-string library (string.h).

    Program Overview

    • Create a function that copies the second c-string over the first.
    • If only 1 argument is provided copy the input into a local variable for up to 50 characters (default value)
    • If 2 arguments are provided, use the second argument as the max length to copy
    • Copy the argument using the copy function
    • Print out the copied variable

    Steps to complete

    • Copy the string_length function from level 1, by using cat on the main.c from the prior level, which is located at ../01/main.c
    • Follow the CODE: instructions provided in main.c
    • Write a user test that tests the program's length function (user_tests/utest3.3.json)
    • Run /challenge/tester to get the flag

    Objective

    In this challenge, you will write a program that compares arg1 to arg2 and prints the result without using the c-string library (string.h).

    Program Overview

    • Write a function that will compare 2 strings
    • Call the function using arg arg1 and arg2
    • It will print 0 if they match exactly.
    • It will print a negative value if the mismatching character of arg2 is larger than the character in arg1.
    • It will print a positive value if the mismatching character of arg2 is smaller than the character in arg1.

    Steps to complete

    • Copy the string_length function from level 1, by using cat on the main.c from the prior level, which is located at ../01/main.c
    • Follow the CODE: instructions provided in main.c
    • Run /challenge/tester to get the flag

    Objective

    In this challenge, you will write a program that changes vowels to upper case letters without using the c-string library (string.h).

    Program Overview

    • Create a function that will change the vowels in a word to upper case
    • The program will use arg1 as a filename
    • It will read from the file
    • It will pass the input to the function and change all vowels to upper case
    • It will join together all the words on a single line and with a space between each word
    • Once done mangling all the words, it will print them all at at once.

    Steps to complete

    • Copy the string_length and concatenate_strings functions from level 2, by using cat on the main.c from the prior level, which is located at ../02/main.c
    • Follow the CODE: instructions provided in main.c
    • Write a user test that tests the program's length function (user_tests/utest3.5.json)
    • Run /challenge/tester to get the flag

    Objective

    Your task is to review, identify, and fix errors in the provided C code .

    Program Should Complete the Following

    • Take a list of numbers as input from a file provided via argument 1
    • Calculate the average of the numbers.
    • Find the highest and lowest numbers.
    • Print out the average, highest, and lowest numbers.

    Steps to complete

    • Create 3 test cases:
      • 6.1 tests the average value
      • 6.2 tests the highest number value
      • 6.3 tests the lowest number value
    • Fix the bugs in the code so that it meets the system and user tests

    Hints:

    • Some loops might have wrong conditions or variable names (verify the < and <= are doing what you expect)
    • Add a print statement to check that the accumulation for the average is the proper value
    • Look for undeclared or incorrectly declared variables.

    Objective

    Use the debugger to get the flag

    Steps to complete

    • Open the main.c for level 07 in vscode
    • Run the debugger in vscode
    • Get the flag from the variable after it's loaded into memory

    Steps to print out a variable

    • To print a variable out in a way that you can copy and paste, switch to the debug console (bottom pane)
    • Type in (char *) buffer or -exec print buffer
    • Copy and paste your flag

    Objective

    Use the debugger to get the flag

    Steps to complete

    • Open the main.c for level 08 in vscode
    • Run the debugger in vscode
    • Get the flag from the variable after it's loaded into memory

    Steps to print out a variable (see prior challenge)

    Steps to change a variable

    • Find a good place to stop the execution with a breakpoint
    • Via the gui: double click on the variable and change the value (for characters completely remove the value and replace with 'c')
    • Via the debugger console: use the command -exec set variable a = 99

    Objective

    Use valgrind to locate the origin of the segmentation faults (2) in this blackjack game.

    Steps to complete

    • Compile main.c with debugging enabled -g
    • Run main.bin using valgrind
    • Read valgrind's output looking for the segmentation fault, which will be similar to
      Process terminating with default action of signal 11 (SIGSEGV) 
        
    • Fix the code, recompile, and try again
    • Use tester to test your code and get the flag


    30-Day Scoreboard:

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

    Rank Hacker Badges Score