LecLabs 2.3 on Pointers


Intro to Programming Languages.

[ ← Module List ]

This module dives into creating and using pointers in C with a focus on their use in c-strings.


Challenges

Lecture video on Stack pointers

Connect with SSH

Link your SSH key, then connect with: ssh hacker@pwn.college

Objective

The program will indentify non-printable characters and display them in a readable format.

CTRL-Click Here for Follow Along Video

Requirements

  1. Check for non-printable characters contains_non_printable(char *str): checks if the string contains any non-printable characters.
    • Takes a pointer to c-string as input
    • Loop through each character in the string
      • Use the isprint function from ctype.h to check if each character is printable.
      • Returns 1 if any non-printable character is detected in the string
    • Otherwise return 0
  2. Print non-printable characters print_non_printable_hex(char *str): prints non-printable characters in the format \xXX and prints them in red using ANSI escape codes.
    • Takes a pointer to a c-string as input
    • Loop through each character in the string
    • If a character is non-printable
      • Print its hexadecimal value in the format \xXX using red text.
      • Use ANSI escape codes \033[31m to start red color and \033[0m to reset color.
      • The printf should use this formatter "\033[31m\\x%02X\033[0m"
    • Else, print the character
  3. The main function
    • If no argument is provided, printf("Usage: %s \n", argv[0]); and return 1
    • Retrieve the input string from the command-line arguments.
    • Use print_non_printable_hex to check for non-printable characters.
    • If non-printable characters are found, use print_non_printable_hex to print the string with non-printable characters highlighted.
    • Otherwise, print the string as is.
  4. Example
    • If the input string is "Hello\x01\x02\x03World!", the output should be:
    • 
          Hello\x01\x02\x03World!
          with \x01, \x02, and \x03 displayed in red.
                
    • If the input string is "Hello World!", the output should simply be:
    • 
          Hello World!
                

Steps to Complete

  • Implement the requirements
  • Compile and test the program
  • Create 2 user test cases, use the desciption in the test case to guide the creation of them.
  • Run /challenge/tester
  • Get flag

Connect with SSH

Link your SSH key, then connect with: ssh hacker@pwn.college

Lecture video on Examples Single Value

Connect with SSH

Link your SSH key, then connect with: ssh hacker@pwn.college

Lecture video on Examples of Ptr to Array

Connect with SSH

Link your SSH key, then connect with: ssh hacker@pwn.college

Set character values using pointers.

Connect with SSH

Link your SSH key, then connect with: ssh hacker@pwn.college

Set strings using pointers.

Connect with SSH

Link your SSH key, then connect with: ssh hacker@pwn.college

Lecture video on strstr video

Connect with SSH

Link your SSH key, then connect with: ssh hacker@pwn.college

Using the strstr function to find a string within another string.

Connect with SSH

Link your SSH key, then connect with: ssh hacker@pwn.college

Objective

Write a C program that takes a filename and a word as command-line arguments. The program will read the file and count the occurrences of the specified word.

CTRL-Click Here for Follow Along Video

Requirements

  1. Create a function that counts the occurrences of the specified word in each line
  2. Take two command-line arguments: the name of the file and the word to count, if both are not specified it should give a usage message.
  3. Open the file
  4. Read each line in the file in a loop using fgets
    • Call the occurrence counter function
    • Add the number of occurrences to a running total
  5. Output the results using "The word '%s' occurs %d times in the file\n"

Example Usage


./word_count filename.txt crazy
The word 'crazy' occurrs 18 times in the file

Steps to Complete

  • Implement the requirements
  • Compile and test the program
  • Run /challenge/tester
  • Get flag

Connect with SSH

Link your SSH key, then connect with: ssh hacker@pwn.college

Lecture video on strtok video

Connect with SSH

Link your SSH key, then connect with: ssh hacker@pwn.college

Using the strtok function to extract delimited values (i.e., tokens) from a string.

Connect with SSH

Link your SSH key, then connect with: ssh hacker@pwn.college

Objective

The program will read a line of text from the user and print out the number of times each word appears in the input.

CTRL-Click Here for Follow Along Video Part 1

CTRL-Click Here for Follow Along Video Part 2

Requirements

  1. Change string to have all lowercase letters to_lowercase(char *str)
    • Loop through the string and change all the letters to lowercase
  2. Search for words already found find_word(char words[][MAX_WORD_LENGTH], int size, char *word)
    • Search through all the words already found and see if a match exists with the current word
    • If a match is found, return the index of the match
    • If no match is found then return -1
  3. The main function
    • Provided input (for user input), found_words (a matrix that holds the found words), counts(an array that holds counts at an index)
    • Prompt user to enter a sentence
    • Receive the sentence via standard input using fgets
    • If the last character of the input is a newline, then remove it
    • Use strtok to tokenize the input based on space (" ")
    • While strtok finds a new word continue to loop through the words in the sentence
      • Call find_words with the current word and the found words
      • If the return is -1, copy the current word to the found_words array and increase count for the max index
      • If the return is not -1, then increase the associated value in counts
      • Increase word_count
    • Print out the counts for each word

Steps to Complete

  • Implement the requirements
  • Compile and test the program
  • Create 1 user test case, use the desciption in the test case to guide the creation of the test case.
  • Run /challenge/tester
  • Get flag

Connect with SSH

Link your SSH key, then connect with: ssh hacker@pwn.college

Lecture video on strchr video

Connect with SSH

Link your SSH key, then connect with: ssh hacker@pwn.college

Lecture video on memset

Connect with SSH

Link your SSH key, then connect with: ssh hacker@pwn.college

30-Day Scoreboard:

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

Rank Hacker Badges Score