Menu
Coddy logo textTech

Recap - Building a Utility

Part of the Logic & Flow section of Coddy's GO journey — lesson 57 of 68.

challenge icon

Challenge

Easy

Create a comprehensive stringutil package with string manipulation functions and demonstrate its usage in a main program. This challenge will test your ability to create a custom utility package, implement exported functions, and use your package from another program.

You will receive two inputs:

  • A string containing function specifications in the format "function1:description1,function2:description2" (e.g., "Reverse:reverses a string,ToUpper:converts to uppercase,ToLower:converts to lowercase")
  • A string containing test strings separated by pipes in the format "string1|string2|string3" (e.g., "hello|Go Programming|WORLD")

Your task is to:

  1. Create a stringutil package by declaring package stringutil
  2. Parse the first input by splitting on commas to get individual function specifications
  3. For each function specification, split on colons to get the function name and description
  4. Implement the following exported functions in your stringutil package:
    • Reverse(s string) string - returns the string with characters in reverse order
    • ToUpper(s string) string - returns the string converted to uppercase
    • ToLower(s string) string - returns the string converted to lowercase
    • Length(s string) int - returns the length of the string
  5. In your main program, import the stringutil package
  6. Display the package header: "=== STRINGUTIL PACKAGE DEMONSTRATION ==="
  7. Display the available functions:
    • "Available functions in stringutil package:"
    • For each function from the first input: "- [function_name]: [description]"
  8. Parse the second input by splitting on pipes to get individual test strings
  9. Display the testing header: "Testing stringutil functions:"
  10. For each test string, demonstrate all functions:
    • "Testing with: \"[test_string]\""
    • " stringutil.Reverse: \"[reversed_result]\""
    • " stringutil.ToUpper: \"[uppercase_result]\""
    • " stringutil.ToLower: \"[lowercase_result]\""
    • " stringutil.Length: [length_result]"
  11. Calculate and display statistics:
    • "=== STATISTICS ==="
    • "Total test strings processed: [number_of_test_strings]"
    • "Functions available in package: [number_of_functions_from_first_input]"
    • "Total function calls made: [total_function_calls]"
  12. Display the longest and shortest strings:
    • "Longest string: \"[longest_string]\" (length: [length])"
    • "Shortest string: \"[shortest_string]\" (length: [length])"
  13. Display the completion message: "Package demonstration completed successfully"

Use the strings package to split input strings and manipulate text, and the fmt package for formatted output. For the Reverse function, you'll need to convert the string to a slice of runes, reverse the order, and convert back to a string. Remember that all exported functions must start with capital letters to be accessible from other packages. This challenge demonstrates how to create reusable utility packages and organize related functionality together.

Try it yourself

package main

import (
	"bufio"
	"fmt"
	"os"
	"strings"
)

// TODO: Create your stringutil package functions here
// Since we can't use separate files, define the functions directly
// These would normally be in a separate package file

// func Reverse(s string) string {
//     // Convert to runes, reverse, convert back to string
// }

// func ToUpper(s string) string {
//     // Use strings.ToUpper
// }

// func ToLower(s string) string {
//     // Use strings.ToLower
// }

// func Length(s string) int {
//     // Return len(s)
// }

func main() {
	// Read input using bufio.Scanner to handle spaces properly
	scanner := bufio.NewScanner(os.Stdin)
	
	scanner.Scan()
	functionSpecs := scanner.Text()
	
	scanner.Scan()
	testStrings := scanner.Text()
	
	// Parse function specifications
	funcList := strings.Split(functionSpecs, ",")
	
	// Parse test strings
	testList := strings.Split(testStrings, "|")
	
	// TODO: Write your code below
	// 1. Display package header
	// 2. Display available functions from funcList (check array lengths)
	// 3. Test all functions with each test string
	// 4. Calculate and display statistics
	// 5. Find and display longest/shortest strings (check if testList has elements)
	// 6. Display completion message
	
	// Remember to use your stringutil package functions like:
	// Reverse(testString)
	// ToUpper(testString)
	// ToLower(testString)
	// Length(testString)
}

All lessons in Logic & Flow