Hone logo
Hone
Problems

Implementing Basic String Formatting in Go

This challenge asks you to implement a simplified version of Go's fmt.Sprintf function. The goal is to understand how string formatting works under the hood and to practice working with strings and control characters. This is a fundamental skill for any Go developer.

Problem Description

You are tasked with creating a function FormatString that takes a format string and a variable number of arguments, and returns a formatted string. The format string can contain literal characters and placeholders of the form %s for strings and %d for integers. The arguments should be substituted into the placeholders in the order they appear.

What needs to be achieved:

  • Implement a function FormatString(format string, args ...interface{}) (string, error) that mimics basic string formatting.
  • The function should handle %s and %d placeholders.
  • If the number of arguments doesn't match the number of placeholders, return an error.
  • If an argument is not of the expected type (string or int), return an error.

Key Requirements:

  • The function must accept a format string and a variable number of arguments (...interface{}).
  • It must correctly substitute arguments into the format string based on the placeholders.
  • Error handling is crucial: return an error if there's a mismatch between placeholders and arguments, or if an argument's type is incorrect.
  • The function should return the formatted string and an error (if any).

Expected Behavior:

The function should parse the format string, identify placeholders, and replace them with the corresponding arguments. It should handle cases where the format string contains literal characters that are not placeholders.

Edge Cases to Consider:

  • Empty format string.
  • Format string with no placeholders.
  • Format string with more placeholders than arguments.
  • Format string with fewer placeholders than arguments (should not panic, but return an error).
  • Arguments of incorrect types (e.g., a float64 passed for a %d placeholder).
  • Multiple consecutive placeholders.
  • Placeholders other than %s and %d (should return an error).

Examples

Example 1:

Input: FormatString("Hello, %s!", "World")
Output: "Hello, World!"
Explanation: The `%s` placeholder is replaced with the string "World".

Example 2:

Input: FormatString("The answer is %d", 42)
Output: "The answer is 42"
Explanation: The `%d` placeholder is replaced with the integer 42.

Example 3:

Input: FormatString("Name: %s, Age: %d", "Alice", 30)
Output: "Name: Alice, Age: 30"
Explanation: Two placeholders are replaced with the corresponding string and integer.

Example 4:

Input: FormatString("Too many arguments: %s %s %s", "a", "b")
Output: error: Mismatch in number of arguments and placeholders
Explanation: There are three placeholders but only two arguments.

Example 5:

Input: FormatString("Incorrect type: %d", "hello")
Output: error: Incorrect argument type for %d, expected int, got string
Explanation: The argument "hello" is a string, but the placeholder expects an integer.

Constraints

  • The format string will only contain literal characters, %s, and %d.
  • The number of arguments will be between 0 and 10 (inclusive).
  • Arguments can be of type string or int.
  • The function must return an error if any constraint is violated.
  • Performance is not a primary concern for this challenge; focus on correctness and clarity.

Notes

  • Consider using a loop to iterate through the format string and identify placeholders.
  • Use type assertions to check the type of each argument.
  • Error handling is critical; provide informative error messages.
  • You can use the fmt package for basic string manipulation, but avoid using fmt.Sprintf directly. The goal is to implement the formatting logic, not just call an existing function.
  • Think about how to handle cases where the format string contains multiple consecutive placeholders.
Loading editor...
go