Skip to content

Strings working#5

Open
Butcher3Years wants to merge 6 commits intomainfrom
Strings---working
Open

Strings working#5
Butcher3Years wants to merge 6 commits intomainfrom
Strings---working

Conversation

@Butcher3Years
Copy link
Copy Markdown
Owner

PR Title Suggestion

Add basic std::string usage and manipulation examples

Description

This PR introduces a simple demonstration of std::string in modern C++.

What this code does:

  • Shows how to create and initialize std::string objects from string literals
  • Demonstrates string concatenation using += and operator+
  • Passes strings efficiently to functions using const std::string& (avoids unnecessary copies)
  • Uses find() to search for substrings and checks result against std::string::npos
  • Prints string content using std::cout
  • Briefly highlights why std::string is preferred over raw C-style strings (char* / char[])

Key learning points covered:

  • Construction from const char*
  • Safe and efficient string appending
  • Substring search with find()
  • Best practice: pass by const reference to avoid copying
  • size() for getting string length (O(1))
  • Comparison to old C functions (strlen, strcpy, strcat)

Why this is useful:
Helps beginners understand how modern C++ handles text safely and conveniently compared to raw character arrays.

Future improvements (optional next steps):

  • Add more string methods (substr, replace, erase, etc.)
  • Show move semantics and SSO (small string optimization)
  • Compare performance of copy vs reference passing

Closes #

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant