SlideShare a Scribd company logo
1 of 16
C++ Strings
// NOVEMBER 11, 2021
• Strings are used for storing text.
• A string variable contains a collection of characters
surrounded by double quotes
C++ Strings
01 CONCATENATION
02 NUMBERS & STRINGS
03 STRING LENGTH
04
05
ACCESS STRING
06
CHANGE STRING
USER INPUT
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
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;
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
# 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
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:
#include <iostream>
using namespace std;
int main()
{
string name="MichaelEspiritu";
cout<<"the length is "<<name.length();
return 0;
}
#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
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
#include <iostream>
using namespace std;
int main()
{
string name="MichaelEspiritu";
cout<<name[3];
return 0;
}
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
C++ String
string firstName;
cout << "Type your first name: ";
cin >> firstName;
cout << "Your name is: " << firstName;
C++ User Input Strings
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.
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
ppt8-string-1.pptx

More Related Content

Similar to ppt8-string-1.pptx

String in programming language in c or c++
String in programming language in c or c++String in programming language in c or c++
String in programming language in c or c++Azeemaj101
 
Lecture 15_Strings and Dynamic Memory Allocation.pptx
Lecture 15_Strings and  Dynamic Memory Allocation.pptxLecture 15_Strings and  Dynamic Memory Allocation.pptx
Lecture 15_Strings and Dynamic Memory Allocation.pptxJawadTanvir
 
Array and string in C++_093547 analysis.pptx
Array and string in C++_093547 analysis.pptxArray and string in C++_093547 analysis.pptx
Array and string in C++_093547 analysis.pptxJumanneChiyanda
 
The Ring programming language version 1.10 book - Part 31 of 212
The Ring programming language version 1.10 book - Part 31 of 212The Ring programming language version 1.10 book - Part 31 of 212
The Ring programming language version 1.10 book - Part 31 of 212Mahmoud Samir Fayed
 
16 strings-and-text-processing-120712074956-phpapp02
16 strings-and-text-processing-120712074956-phpapp0216 strings-and-text-processing-120712074956-phpapp02
16 strings-and-text-processing-120712074956-phpapp02Abdul Samee
 
C++ Course - Lesson 3
C++ Course - Lesson 3C++ Course - Lesson 3
C++ Course - Lesson 3Mohamed Ahmed
 
13 Strings and Text Processing
13 Strings and Text Processing13 Strings and Text Processing
13 Strings and Text ProcessingIntro C# Book
 
String Handling in c++
String Handling in c++String Handling in c++
String Handling in c++Fahim Adil
 
C++ Tutorial.docx
C++ Tutorial.docxC++ Tutorial.docx
C++ Tutorial.docxPinkiVats1
 
The Ring programming language version 1.5.3 book - Part 23 of 184
The Ring programming language version 1.5.3 book - Part 23 of 184The Ring programming language version 1.5.3 book - Part 23 of 184
The Ring programming language version 1.5.3 book - Part 23 of 184Mahmoud Samir Fayed
 

Similar to ppt8-string-1.pptx (20)

String in programming language in c or c++
String in programming language in c or c++String in programming language in c or c++
String in programming language in c or c++
 
Lecture 15_Strings and Dynamic Memory Allocation.pptx
Lecture 15_Strings and  Dynamic Memory Allocation.pptxLecture 15_Strings and  Dynamic Memory Allocation.pptx
Lecture 15_Strings and Dynamic Memory Allocation.pptx
 
Arrays & Strings.pptx
Arrays & Strings.pptxArrays & Strings.pptx
Arrays & Strings.pptx
 
05 c++-strings
05 c++-strings05 c++-strings
05 c++-strings
 
Savitch ch 08
Savitch ch 08Savitch ch 08
Savitch ch 08
 
Savitch Ch 08
Savitch Ch 08Savitch Ch 08
Savitch Ch 08
 
Array and string in C++_093547 analysis.pptx
Array and string in C++_093547 analysis.pptxArray and string in C++_093547 analysis.pptx
Array and string in C++_093547 analysis.pptx
 
The Ring programming language version 1.10 book - Part 31 of 212
The Ring programming language version 1.10 book - Part 31 of 212The Ring programming language version 1.10 book - Part 31 of 212
The Ring programming language version 1.10 book - Part 31 of 212
 
Week6_P_String.pptx
Week6_P_String.pptxWeek6_P_String.pptx
Week6_P_String.pptx
 
16 strings-and-text-processing-120712074956-phpapp02
16 strings-and-text-processing-120712074956-phpapp0216 strings-and-text-processing-120712074956-phpapp02
16 strings-and-text-processing-120712074956-phpapp02
 
Strings
StringsStrings
Strings
 
Strings in c++
Strings in c++Strings in c++
Strings in c++
 
Chapter1.pptx
Chapter1.pptxChapter1.pptx
Chapter1.pptx
 
C++ Course - Lesson 3
C++ Course - Lesson 3C++ Course - Lesson 3
C++ Course - Lesson 3
 
13 Strings and Text Processing
13 Strings and Text Processing13 Strings and Text Processing
13 Strings and Text Processing
 
String Handling in c++
String Handling in c++String Handling in c++
String Handling in c++
 
C++ Tutorial.docx
C++ Tutorial.docxC++ Tutorial.docx
C++ Tutorial.docx
 
The Ring programming language version 1.5.3 book - Part 23 of 184
The Ring programming language version 1.5.3 book - Part 23 of 184The Ring programming language version 1.5.3 book - Part 23 of 184
The Ring programming language version 1.5.3 book - Part 23 of 184
 
String notes
String notesString notes
String notes
 
M C6java7
M C6java7M C6java7
M C6java7
 

Recently uploaded

Retail sector trends for 2024 | European Business Review
Retail sector trends for 2024  | European Business ReviewRetail sector trends for 2024  | European Business Review
Retail sector trends for 2024 | European Business ReviewAntonis Zairis
 
一比一原版(Caltech毕业证书)加州理工学院毕业证成绩单学位证书
一比一原版(Caltech毕业证书)加州理工学院毕业证成绩单学位证书一比一原版(Caltech毕业证书)加州理工学院毕业证成绩单学位证书
一比一原版(Caltech毕业证书)加州理工学院毕业证成绩单学位证书atedyxc
 
一比一原版(UPenn毕业证书)宾夕法尼亚大学毕业证成绩单学位证书
一比一原版(UPenn毕业证书)宾夕法尼亚大学毕业证成绩单学位证书一比一原版(UPenn毕业证书)宾夕法尼亚大学毕业证成绩单学位证书
一比一原版(UPenn毕业证书)宾夕法尼亚大学毕业证成绩单学位证书atedyxc
 
How to exchange my pi coins on HTX in 2024
How to exchange my pi coins on HTX in 2024How to exchange my pi coins on HTX in 2024
How to exchange my pi coins on HTX in 2024DOT TECH
 
Big developments in Lesotho Butha-Buthe.
Big developments in Lesotho Butha-Buthe.Big developments in Lesotho Butha-Buthe.
Big developments in Lesotho Butha-Buthe.ntlhabeli12
 
一比一原版(UC Davis毕业证书)加州大学戴维斯分校毕业证成绩单学位证书
一比一原版(UC Davis毕业证书)加州大学戴维斯分校毕业证成绩单学位证书一比一原版(UC Davis毕业证书)加州大学戴维斯分校毕业证成绩单学位证书
一比一原版(UC Davis毕业证书)加州大学戴维斯分校毕业证成绩单学位证书atedyxc
 
What exchange can I sell my pi coins in 2024
What exchange can I sell my pi coins in 2024What exchange can I sell my pi coins in 2024
What exchange can I sell my pi coins in 2024DOT TECH
 
Satoshi DEX Leverages Layer 2 To Transform DeFi Ecosystem.pdf
Satoshi DEX Leverages Layer 2 To Transform DeFi Ecosystem.pdfSatoshi DEX Leverages Layer 2 To Transform DeFi Ecosystem.pdf
Satoshi DEX Leverages Layer 2 To Transform DeFi Ecosystem.pdfcoingabbar
 
Rapport annuel de Encevo Group pour l'année 2023
Rapport annuel de Encevo Group pour l'année 2023Rapport annuel de Encevo Group pour l'année 2023
Rapport annuel de Encevo Group pour l'année 2023Paperjam_redaction
 
Amil baba australia kala jadu in uk black magic in usa
Amil baba australia kala jadu in uk black magic in usaAmil baba australia kala jadu in uk black magic in usa
Amil baba australia kala jadu in uk black magic in usaisrajan914
 
NO1 Best kala jadu karne wale ka contact number kala jadu karne wale baba kal...
NO1 Best kala jadu karne wale ka contact number kala jadu karne wale baba kal...NO1 Best kala jadu karne wale ka contact number kala jadu karne wale baba kal...
NO1 Best kala jadu karne wale ka contact number kala jadu karne wale baba kal...Amil baba
 
State Space Tutorial.pptxjjjjjjjjjjjjjjj
State Space Tutorial.pptxjjjjjjjjjjjjjjjState Space Tutorial.pptxjjjjjjjjjjjjjjj
State Space Tutorial.pptxjjjjjjjjjjjjjjjjoshuaclack73
 
一比一原版(Cornell毕业证书)康奈尔大学毕业证成绩单学位证书
一比一原版(Cornell毕业证书)康奈尔大学毕业证成绩单学位证书一比一原版(Cornell毕业证书)康奈尔大学毕业证成绩单学位证书
一比一原版(Cornell毕业证书)康奈尔大学毕业证成绩单学位证书atedyxc
 
一比一原版(Caltech毕业证书)加利福尼亚理工学院毕业证成绩单学位证书
一比一原版(Caltech毕业证书)加利福尼亚理工学院毕业证成绩单学位证书一比一原版(Caltech毕业证书)加利福尼亚理工学院毕业证成绩单学位证书
一比一原版(Caltech毕业证书)加利福尼亚理工学院毕业证成绩单学位证书atedyxc
 
一比一原版(UCSB毕业证书)圣塔芭芭拉社区大学毕业证成绩单学位证书
一比一原版(UCSB毕业证书)圣塔芭芭拉社区大学毕业证成绩单学位证书一比一原版(UCSB毕业证书)圣塔芭芭拉社区大学毕业证成绩单学位证书
一比一原版(UCSB毕业证书)圣塔芭芭拉社区大学毕业证成绩单学位证书atedyxc
 
Bahawalpur Culture.pptx pptx pptx pttx pttx
Bahawalpur Culture.pptx pptx pptx pttx pttxBahawalpur Culture.pptx pptx pptx pttx pttx
Bahawalpur Culture.pptx pptx pptx pttx pttxAbdulNasirNichari
 
一比一原版(SFU毕业证书)西蒙菲莎大学毕业证成绩单学位证书
一比一原版(SFU毕业证书)西蒙菲莎大学毕业证成绩单学位证书一比一原版(SFU毕业证书)西蒙菲莎大学毕业证成绩单学位证书
一比一原版(SFU毕业证书)西蒙菲莎大学毕业证成绩单学位证书atedyxc
 
The Pfandbrief Roundtable 2024 - Covered Bonds
The Pfandbrief Roundtable 2024 - Covered BondsThe Pfandbrief Roundtable 2024 - Covered Bonds
The Pfandbrief Roundtable 2024 - Covered BondsNeil Day
 
Financial Accounting and Analysis balancesheet.pdf
Financial Accounting and Analysis balancesheet.pdfFinancial Accounting and Analysis balancesheet.pdf
Financial Accounting and Analysis balancesheet.pdfmukul381940
 

Recently uploaded (20)

Retail sector trends for 2024 | European Business Review
Retail sector trends for 2024  | European Business ReviewRetail sector trends for 2024  | European Business Review
Retail sector trends for 2024 | European Business Review
 
一比一原版(Caltech毕业证书)加州理工学院毕业证成绩单学位证书
一比一原版(Caltech毕业证书)加州理工学院毕业证成绩单学位证书一比一原版(Caltech毕业证书)加州理工学院毕业证成绩单学位证书
一比一原版(Caltech毕业证书)加州理工学院毕业证成绩单学位证书
 
一比一原版(UPenn毕业证书)宾夕法尼亚大学毕业证成绩单学位证书
一比一原版(UPenn毕业证书)宾夕法尼亚大学毕业证成绩单学位证书一比一原版(UPenn毕业证书)宾夕法尼亚大学毕业证成绩单学位证书
一比一原版(UPenn毕业证书)宾夕法尼亚大学毕业证成绩单学位证书
 
How to exchange my pi coins on HTX in 2024
How to exchange my pi coins on HTX in 2024How to exchange my pi coins on HTX in 2024
How to exchange my pi coins on HTX in 2024
 
Big developments in Lesotho Butha-Buthe.
Big developments in Lesotho Butha-Buthe.Big developments in Lesotho Butha-Buthe.
Big developments in Lesotho Butha-Buthe.
 
一比一原版(UC Davis毕业证书)加州大学戴维斯分校毕业证成绩单学位证书
一比一原版(UC Davis毕业证书)加州大学戴维斯分校毕业证成绩单学位证书一比一原版(UC Davis毕业证书)加州大学戴维斯分校毕业证成绩单学位证书
一比一原版(UC Davis毕业证书)加州大学戴维斯分校毕业证成绩单学位证书
 
What exchange can I sell my pi coins in 2024
What exchange can I sell my pi coins in 2024What exchange can I sell my pi coins in 2024
What exchange can I sell my pi coins in 2024
 
Satoshi DEX Leverages Layer 2 To Transform DeFi Ecosystem.pdf
Satoshi DEX Leverages Layer 2 To Transform DeFi Ecosystem.pdfSatoshi DEX Leverages Layer 2 To Transform DeFi Ecosystem.pdf
Satoshi DEX Leverages Layer 2 To Transform DeFi Ecosystem.pdf
 
Rapport annuel de Encevo Group pour l'année 2023
Rapport annuel de Encevo Group pour l'année 2023Rapport annuel de Encevo Group pour l'année 2023
Rapport annuel de Encevo Group pour l'année 2023
 
STRATEGIC MANAGEMENT VIETTEL TELECOM GROUP
STRATEGIC MANAGEMENT VIETTEL TELECOM GROUPSTRATEGIC MANAGEMENT VIETTEL TELECOM GROUP
STRATEGIC MANAGEMENT VIETTEL TELECOM GROUP
 
Amil baba australia kala jadu in uk black magic in usa
Amil baba australia kala jadu in uk black magic in usaAmil baba australia kala jadu in uk black magic in usa
Amil baba australia kala jadu in uk black magic in usa
 
NO1 Best kala jadu karne wale ka contact number kala jadu karne wale baba kal...
NO1 Best kala jadu karne wale ka contact number kala jadu karne wale baba kal...NO1 Best kala jadu karne wale ka contact number kala jadu karne wale baba kal...
NO1 Best kala jadu karne wale ka contact number kala jadu karne wale baba kal...
 
State Space Tutorial.pptxjjjjjjjjjjjjjjj
State Space Tutorial.pptxjjjjjjjjjjjjjjjState Space Tutorial.pptxjjjjjjjjjjjjjjj
State Space Tutorial.pptxjjjjjjjjjjjjjjj
 
一比一原版(Cornell毕业证书)康奈尔大学毕业证成绩单学位证书
一比一原版(Cornell毕业证书)康奈尔大学毕业证成绩单学位证书一比一原版(Cornell毕业证书)康奈尔大学毕业证成绩单学位证书
一比一原版(Cornell毕业证书)康奈尔大学毕业证成绩单学位证书
 
一比一原版(Caltech毕业证书)加利福尼亚理工学院毕业证成绩单学位证书
一比一原版(Caltech毕业证书)加利福尼亚理工学院毕业证成绩单学位证书一比一原版(Caltech毕业证书)加利福尼亚理工学院毕业证成绩单学位证书
一比一原版(Caltech毕业证书)加利福尼亚理工学院毕业证成绩单学位证书
 
一比一原版(UCSB毕业证书)圣塔芭芭拉社区大学毕业证成绩单学位证书
一比一原版(UCSB毕业证书)圣塔芭芭拉社区大学毕业证成绩单学位证书一比一原版(UCSB毕业证书)圣塔芭芭拉社区大学毕业证成绩单学位证书
一比一原版(UCSB毕业证书)圣塔芭芭拉社区大学毕业证成绩单学位证书
 
Bahawalpur Culture.pptx pptx pptx pttx pttx
Bahawalpur Culture.pptx pptx pptx pttx pttxBahawalpur Culture.pptx pptx pptx pttx pttx
Bahawalpur Culture.pptx pptx pptx pttx pttx
 
一比一原版(SFU毕业证书)西蒙菲莎大学毕业证成绩单学位证书
一比一原版(SFU毕业证书)西蒙菲莎大学毕业证成绩单学位证书一比一原版(SFU毕业证书)西蒙菲莎大学毕业证成绩单学位证书
一比一原版(SFU毕业证书)西蒙菲莎大学毕业证成绩单学位证书
 
The Pfandbrief Roundtable 2024 - Covered Bonds
The Pfandbrief Roundtable 2024 - Covered BondsThe Pfandbrief Roundtable 2024 - Covered Bonds
The Pfandbrief Roundtable 2024 - Covered Bonds
 
Financial Accounting and Analysis balancesheet.pdf
Financial Accounting and Analysis balancesheet.pdfFinancial Accounting and Analysis balancesheet.pdf
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
  • 2. C++ Strings 01 CONCATENATION 02 NUMBERS & STRINGS 03 STRING LENGTH 04 05 ACCESS STRING 06 CHANGE STRING USER INPUT
  • 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:
  • 8. #include <iostream> using namespace std; int main() { string name="MichaelEspiritu"; cout<<"the length is "<<name.length(); return 0; }
  • 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
  • 11. #include <iostream> using namespace std; int main() { string name="MichaelEspiritu"; cout<<name[3]; return 0; }
  • 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

  1. // Type your first name: Mary Rose // Your name is: Mary
  2. // Type your full name: Michael Angelo // Your name is: Michael Angelo
  3. // Type your full name: Michael Angelo // Your name is: Michael Angelo