C++Introduction to C++Learn C++

Learn to Use Strings in C++

In C programming language ASCII codes are used as in char arrays to store texts in ASCII mode. You can use char arrays in both C and C++, they are faster in operations and they have less memory usage. In s modern way, strings are useful for storing texts and they are defined in the string library. A string class contains a collection of characters surrounded by double quotes as we used in char arrays.

A string variable can be assigned as below,

Here is the full C++ example that shows how to print out a string to screen,

and the output will be,


1. Reading and Modifying Characters of a string

strings are used like char arrays, you can reach each character by using [ ] brackets and the index number. See example below

or you can use std in namespace so you don’t need to use std:: in each std commands as below,

output will be second character as below,

Note that index numbers in C and C++ starts with 0, and str[0] is ‘T’ character. We can modify character of a string as below,

String Concatenation

strings are easy to operate with them in many ways, while we use strcat(), strcpy() functions in char arrays.

strings are classes with many features, this is why they are preferred in modern C++. For example you can add another string by using its append() function.

this is same as

Length of a string

Length of string, number of characters can be obtained by using it’s length() command as below,

Size of a string

Size of string is size of string in the memory, if characters has 1 byte in compiler option this will equal to length, if characters has 2 bytes it will have double size of its length.

Note that, in C++ Builder both string and String are used in same ways. In the last C++ Builder versions Strings are UnicodeString, supports globally and operations are explained before in Unicode Strings in C++ On Windows


Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He was born in 1974, Eskisehir-Turkey, started coding in college and graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. He worked as a research assistant at the same university for more than 10 years. He received his MSc and PhD degrees from the same department at the same university. Since 2012, he is the founder and CEO of Esenja LLC Company. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Learn C++Syntax

What Is Deleted Implicitly-declared Copy Assignment Operator In C++?

C++C++11C++14C++17C++20Learn C++Syntax

What Is A Forced (Default) Copy Assignment Operator In Modern C++

C++C++11C++14C++17C++20Learn C++Syntax

What is Implicitly-declared Copy Assignment Operator In C++?

C++C++11C++14C++17C++20Learn C++Syntax

What is Avoiding Implicit Copy Assignment In C++?

Worth reading...
Quickly Learn To Use Variables Between Units In C++ Builder