Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

Lesson 12: C‑Style Strings and std::stri | C++ Course lesson | BraveProgrammer
  1. Courses
  2. /
  3. C++ Course

Lesson 12 of 45

·C++

Lesson 12: C‑Style Strings and std::string

11 min

Course progress27%

What you'll learn

Keep these ideas in mind as you work through the lesson.

4 points
  • C‑style string functions (`strcpy`, `strlen`, `strcmp`)
  • std::string methods (`size`, `substr`, `find`)
  • String literals and concatenation
  • Conversion between C‑style and std::string

C‑Style Strings and std::string

A C‑style string is a null‑terminated character array (char[]). The C++ Standard Library augments this with std::string, a more convenient, dynamic, and safer alternative.


C‑style example

char name[20] = \"Alice\"; // null‑terminated automatically
std::cout << \"Name: \" << name << \" (length \" << std::strlen(name) << \")\
\";

// Concatenation with `strcat` (unsafe – may overflow)
char buffer[30] = \"Hello \";
std::strcat(buffer, name);
std::cout << buffer << '\
';

std::string example

std::string greeting = \"Hello \";
std::string name = \"Bob\";
std::string message = greeting + name + \"!\"; // automatic resizing
std::cout << message << \" (size \" << message.size() << \")\
\";

std::string manages its own memory, provides a rich set of member functions (substr, find, replace), and works seamlessly with STL algorithms.

Tip: Prefer std::string for new code; only use C‑style strings when interacting with legacy APIs.


Note

Both representations can be mixed: std::string s = \"text\"; const char* c = s.c_str();
Make it stickPractice what you've learned1 quiz · 2 practical tests · Optional activities from C++ CourseBrowse activities
Previous lessonLesson 11: ArraysContinue to lesson 13Lesson 13: Pointers BasicsBack to C++ Course curriculum