Process User Registration Strings

GolangGolangBeginner
Practice Now

Introduction

In this challenge, participants are tasked with implementing a function to format user names in a social media platform's user registration system. The goal is to ensure consistent and clean user input for data integrity and user experience. Specifically, the function should trim leading and trailing whitespaces, capitalize the first letter of each name segment, and handle multiple name segments (first, middle, last names).


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL go(("Golang")) -.-> go/DataTypesandStructuresGroup(["Data Types and Structures"]) go(("Golang")) -.-> go/FunctionsandControlFlowGroup(["Functions and Control Flow"]) go/DataTypesandStructuresGroup -.-> go/strings("Strings") go/FunctionsandControlFlowGroup -.-> go/for("For") go/FunctionsandControlFlowGroup -.-> go/functions("Functions") subgraph Lab Skills go/strings -.-> lab-436083{{"Process User Registration Strings"}} go/for -.-> lab-436083{{"Process User Registration Strings"}} go/functions -.-> lab-436083{{"Process User Registration Strings"}} end

Process User Registration Strings

In a social media platform's user registration system, maintaining consistent and clean user input is crucial for data integrity and user experience.

Tasks

  • Create a function formatUserName that takes a full name as input
  • Trim leading and trailing whitespaces from the input
  • Capitalize the first letter of each name segment
  • Return the formatted full name

Requirements

  • Create the solution in ~/project/registration.go
  • Use Go's string manipulation functions from previous lab
  • Function must handle multiple name segments (first, middle, last names)
  • Use strings.TrimSpace() to remove extra whitespaces
  • Ensure the function works with various input formats

Examples

To test the function, update the main() function with some example cases:

func main() {
    fmt.Println(formatUserName("  john doe  "))        // Output: John Doe
    fmt.Println(formatUserName("  alice bob smith  ")) // Output: Alice Bob Smith
    fmt.Println(formatUserName("JANE DOE"))            // Output: Jane Doe
}

Run the program and verify the output matches the expected results.

go run registration.go
John Doe
Alice Bob Smith
Jane Doe

Hints

  1. strings.TrimSpace() removes leading and trailing whitespaces from the input name
  2. strings.Fields() splits the name into segments, handling multiple spaces
  3. strings.ToLower() ensures consistent capitalization before applying Title()
  4. strings.Join() reconstructs the formatted name with a space between segments
โœจ Check Solution and Practice

Summary

In summary, this challenge requires participants to implement a function that formats user names in a social media platform's user registration system. The function should trim leading and trailing whitespaces, capitalize the first letter of each name segment, and handle multiple name segments (first, middle, last names). The goal is to ensure consistent and clean user input for data integrity and user experience.