C++
Numbers and
Strings
RAI MASAMUNE
Adding Numbers and Strings
If you add two numbers, the result will be a
number:
If you add two numbers, the result will be
a number:
If you add two strings, the result
will be a string concatenation:
If you try to add a number to a
string, an error occurs:
C++ String Length
String Length
To get the length of a string, use the length() function:
Tip: You might see some C++ programs that use the size() function to get the length of a string.
This is just an alias of length(). It is completely up to you if you want to use length() or size():
C++ Access Strings
Access Strings
You can access the characters in a string by referring to its index number inside
square brackets [].
This example prints the first character in myString:
Access String
Note: String indexes start with 0: [0] is the first character. [1] is the second
character, etc.
This example prints the second character in myString:
Change String Characters
To change the value of a specific character in a string, refer to the index number,
and use single quotes:

C++ Numbers and Strings.pptx

  • 1.
  • 2.
    Adding Numbers andStrings If you add two numbers, the result will be a number:
  • 3.
    If you addtwo numbers, the result will be a number:
  • 4.
    If you addtwo strings, the result will be a string concatenation:
  • 5.
    If you tryto add a number to a string, an error occurs:
  • 6.
    C++ String Length StringLength To get the length of a string, use the length() function: Tip: You might see some C++ programs that use the size() function to get the length of a string. This is just an alias of length(). It is completely up to you if you want to use length() or size():
  • 7.
    C++ Access Strings AccessStrings You can access the characters in a string by referring to its index number inside square brackets []. This example prints the first character in myString:
  • 8.
    Access String Note: Stringindexes start with 0: [0] is the first character. [1] is the second character, etc. This example prints the second character in myString:
  • 9.
    Change String Characters Tochange the value of a specific character in a string, refer to the index number, and use single quotes: