C++ Tutorial

C++ Strings

string holds text. Include <string>, then concatenate, measure length, and read a line.

Include string, then declare

A string holds a sequence of characters. Include <string> at the top. Then declare the variable with type string and a value in double quotes. <iostream>alone is not enough for this type.

Try this at /cpp/try. The C++ editor compiles with g++.

Concatenate with +

+ joins strings. At least one side must already be a string object."Hi" + " there" is two literals and will not compile. Store one piece in a stringfirst, then add the rest.

Example

#include <iostream>
#include <string>
using namespace std;

int main() {
  string first = "Ada";
  string last = "Lovelace";
  string full = first + " " + last;
  cout << first << endl;
  cout << full << endl;
  cout << first + " wrote programs" << endl;
  return 0;
}

length and the first character

length() returns how many characters are in the string. Index [0] is the first character, type char. Indexes start at zero, so the last character is atlength() - 1 when the string is not empty.

Example

#include <iostream>
#include <string>
using namespace std;

int main() {
  string word = "C++";
  cout << word.length() << endl;
  cout << word[0] << endl;
  cout << word[1] << endl;
  return 0;
}

Do not read word[0] when the string is empty. That is undefined behavior. Checklength() first, or only index strings you just filled with text.

cin stops at a space

cin >> name reads one word. It skips leading whitespace, then stops at the next space or newline. A full name such as Ada Lovelace would store only Ada.

Example

#include <iostream>
#include <string>
using namespace std;

int main() {
  string word;
  cout << "Type one word: ";
  cin >> word;
  cout << "Got: " << word << endl;
  return 0;
}

getline reads a whole line

getline(cin, line) reads from the current position until newline and stores everything except that newline. Spaces stay in the string. Use it when the answer can contain spaces.

Example

#include <iostream>
#include <string>
using namespace std;

int main() {
  string line;
  cout << "Type a full line:" << endl;
  getline(cin, line);
  cout << "You typed: " << line << endl;
  cout << "length " << line.length() << endl;
  return 0;
}

If you mix cin >> and then getline, the leftover newline from the first read can make getline return immediately. For this chapter, use one style in a program: either>> for words, or getline for lines.

Single quotes versus double quotes

'A' is a char. "A" is a string literal. Concatenation, length(), and getline belong to string. Next: math functions in <cmath>.

Example

#include <iostream>
#include <string>
using namespace std;

int main() {
  string name = "StudyGrid";
  string tag = name.substr(0, 5);
  cout << name << endl;
  cout << "first five: " << tag << endl;
  cout << "find Grid: " << name.find("Grid") << endl;
  return 0;
}

Worked examples

The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.

Open them in the C++ editor at /cpp/try. Change one measurement and check whether the result still has the right unit.

Biology

A short DNA motif

DNA is a sequence of bases: A, T, C, G. A pairs with T, C with G. The string "ATGCCAT" is not a gene. It is a motif you might search for in a longer strand.

std::string holds the characters. dna[0] is the first base. size() is how many bases, not including a hidden terminator you have to manage yourself.

Example

#include <iostream>
#include <string>
using namespace std;

int main() {
  string dna = "ATGCCAT";
  cout << "strand: " << dna << endl;
  cout << "first base: " << dna[0] << endl;
  return 0;
}

Chemistry

A formula as text

H2SO4 is sulphuric acid. Subscripts are written as ordinary digits in a string because C++ has no chemical typesetter. The letters are not variables named H and S.

Example

#include <iostream>
#include <string>
using namespace std;

int main() {
  string formula = "H2SO4";
  cout << "sulphuric acid: " << formula << endl;
  return 0;
}

FAQ: C++ Strings

Common questions about this page.

What is the StudyGrid C++ tutorial?

The StudyGrid C++ tutorial is a full beginner-to-advanced track: syntax, types, input, loops, functions, classes, the STL, templates, maps, and lambdas. Each chapter has copy-and-run examples.

Should I run c++ strings examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn c++ strings in this C++ C++ lesson (C++ Strings).

Is the C++ editor the same as Try Python or Try HTML?

No. Try C++ compiles with g++ at /cpp/try and shows stdout plus compiler messages. Try Python stays at /try. Try HTML stays at /html/try. C++ lessons never open those editors.

Do I need to install a compiler to learn C++?

No. Open a chapter, click Try it in C++, and compile in the browser. You can also download a .cpp file and compile locally with g++.

Where should I start the C++ tutorial?

Start at C++ Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After classes, open C++ Examples, then templates, map, and lambdas. Use Next at the bottom of each chapter.

Is the C++ tutorial free?

Yes. The C++ workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.