Financial Accounting and Analysis balancesheet.pdf
ppt8-string-1.pptx
1. C++ Strings
// NOVEMBER 11, 2021
• Strings are used for storing text.
• A string variable contains a collection of characters
surrounded by double quotes
3. C++ String Concatenation
The + operator can be used between strings to add them together to
make a new string. This is called concatenation:
string firstName = “Michael ";
string lastName = “Espiritu ";
string fullName = firstName + lastName;
cout << fullName;
CONCATENATION
4. APPEND
A string in C++ is actually an object, which contain functions that
can perform certain operations on strings.
For example, you can also concatenate strings with the append()
function:
string firstName = “Michael ";
string lastName = “Espiritu ";
string fullName = firstName.append(lastName);
cout << fullName;
5. C++ uses the + operator for both addition and concatenation.
Numbers are added. Strings are concatenated.
Example
int x = 10;
int y = 20;
int z = x + y; // z will be 30 (an integer)
Example
string x = "10";
string y = "20";
string z = x + y; // z will be 1020 (a string)
//Adding Numbers and Strings
6. # include<bits/stdc++.h>
using namespace std;
int main ()
{ char arr1[] = " An array with src";
char arr2[] = " needs to concatenate with dest";
strcat (arr1, arr2);
cout << arr1 << arr2;
return 0;
}
//Adding Numbers and Strings
7. string txt = “MichaelEspiritu";
cout << "The length of the txt string is: " << txt.length();
//Answer is 15
string txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
cout << "The length of the txt string is: " << txt.size();
//Answer is 26
//C++ String Length
To get the length of a string, use the length() function:
9. #include <iostream>
#include <cstring>
using namespace std;
int main() {
// initialize C-string
char song[] = "We Will Rock You!";
// print the length of the song string
cout << strlen(song);
return 0;
}
// Output: 17
10. C++ String
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:
string myString = "Hello";
cout << myString[0];
//It will output the letter H
Access Strings
12. C++ String
To change the value of a specific character in a string, refer to the
index number, and use single quotes:
string myString = "Hello";
myString[0] = 'M';
cout << myString;
// Outputs Mello instead of Hello
Change String Characters
13. C++ String
string firstName;
cout << "Type your first name: ";
cin >> firstName;
cout << "Your name is: " << firstName;
C++ User Input Strings
14. C++ String
string fullName;
cout << "Type your full name: ";
getline (cin, fullName);
cout << "Your name is: " << fullName;
C++ User Input Strings
cin considers a space as a terminating character, which means that it can only display a single word
the getline() function to read a line of text.
15. C++ String
#include <iostream>
# include<bits/stdc++.h>
#include <cstring>
#include<stdlib.h> //for using clear screen function
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main()
{
string fullName;
cout << "Type your full name: ";
getline (cin, fullName);
system("CLS");
cout<<"full name: "<<fullName<<endl;
cout<<"the length is "<<fullName.length();
return 0;
}
C++ User Input Strings
Editor's Notes
// Type your first name: Mary Rose
// Your name is: Mary
// Type your full name: Michael Angelo
// Your name is: Michael Angelo
// Type your full name: Michael Angelo
// Your name is: Michael Angelo