SlideShare a Scribd company logo
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.pptx
JawadTanvir
 
Arrays & Strings.pptx
Arrays & Strings.pptxArrays & Strings.pptx
Arrays & Strings.pptx
AnkurRajSingh2
 
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
JumanneChiyanda
 
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
Mahmoud Samir Fayed
 
Week6_P_String.pptx
Week6_P_String.pptxWeek6_P_String.pptx
Week6_P_String.pptx
OluwafolakeOjo
 
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
 
Chapter1.pptx
Chapter1.pptxChapter1.pptx
Chapter1.pptx
WondimuBantihun1
 
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 Processing
Intro 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.docx
PinkiVats1
 
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
Mahmoud Samir Fayed
 
String notes
String notesString notes
String notes
Prasadu Peddi
 

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

Intro_Economics_ GPresentation Week 4.pptx
Intro_Economics_ GPresentation Week 4.pptxIntro_Economics_ GPresentation Week 4.pptx
Intro_Economics_ GPresentation Week 4.pptx
shetivia
 
how can I sell/buy bulk pi coins securely
how can I sell/buy bulk pi coins securelyhow can I sell/buy bulk pi coins securely
how can I sell/buy bulk pi coins securely
DOT TECH
 
Summary of financial results for 1Q2024
Summary of financial  results for 1Q2024Summary of financial  results for 1Q2024
Summary of financial results for 1Q2024
InterCars
 
一比一原版Birmingham毕业证伯明翰大学|学院毕业证成绩单如何办理
一比一原版Birmingham毕业证伯明翰大学|学院毕业证成绩单如何办理一比一原版Birmingham毕业证伯明翰大学|学院毕业证成绩单如何办理
一比一原版Birmingham毕业证伯明翰大学|学院毕业证成绩单如何办理
betoozp
 
NO1 Uk Divorce problem uk all amil baba in karachi,lahore,pakistan talaq ka m...
NO1 Uk Divorce problem uk all amil baba in karachi,lahore,pakistan talaq ka m...NO1 Uk Divorce problem uk all amil baba in karachi,lahore,pakistan talaq ka m...
NO1 Uk Divorce problem uk all amil baba in karachi,lahore,pakistan talaq ka m...
Amil Baba Dawood bangali
 
what is the future of Pi Network currency.
what is the future of Pi Network currency.what is the future of Pi Network currency.
what is the future of Pi Network currency.
DOT TECH
 
Monthly Economic Monitoring of Ukraine No. 232, May 2024
Monthly Economic Monitoring of Ukraine No. 232, May 2024Monthly Economic Monitoring of Ukraine No. 232, May 2024
USDA Loans in California: A Comprehensive Overview.pptx
USDA Loans in California: A Comprehensive Overview.pptxUSDA Loans in California: A Comprehensive Overview.pptx
USDA Loans in California: A Comprehensive Overview.pptx
marketing367770
 
how can I sell pi coins after successfully completing KYC
how can I sell pi coins after successfully completing KYChow can I sell pi coins after successfully completing KYC
how can I sell pi coins after successfully completing KYC
DOT TECH
 
Which Crypto to Buy Today for Short-Term in May-June 2024.pdf
Which Crypto to Buy Today for Short-Term in May-June 2024.pdfWhich Crypto to Buy Today for Short-Term in May-June 2024.pdf
Which Crypto to Buy Today for Short-Term in May-June 2024.pdf
Kezex (KZX)
 
NO1 Uk Rohani Baba In Karachi Bangali Baba Karachi Online Amil Baba WorldWide...
NO1 Uk Rohani Baba In Karachi Bangali Baba Karachi Online Amil Baba WorldWide...NO1 Uk Rohani Baba In Karachi Bangali Baba Karachi Online Amil Baba WorldWide...
NO1 Uk Rohani Baba In Karachi Bangali Baba Karachi Online Amil Baba WorldWide...
Amil baba
 
how can I sell my pi coins for cash in a pi APP
how can I sell my pi coins for cash in a pi APPhow can I sell my pi coins for cash in a pi APP
how can I sell my pi coins for cash in a pi APP
DOT TECH
 
PF-Wagner's Theory of Public Expenditure.pptx
PF-Wagner's Theory of Public Expenditure.pptxPF-Wagner's Theory of Public Expenditure.pptx
PF-Wagner's Theory of Public Expenditure.pptx
GunjanSharma28848
 
Introduction to Indian Financial System ()
Introduction to Indian Financial System ()Introduction to Indian Financial System ()
Introduction to Indian Financial System ()
Avanish Goel
 
how to sell pi coins on Binance exchange
how to sell pi coins on Binance exchangehow to sell pi coins on Binance exchange
how to sell pi coins on Binance exchange
DOT TECH
 
what is a pi whale and how to access one.
what is a pi whale and how to access one.what is a pi whale and how to access one.
what is a pi whale and how to access one.
DOT TECH
 
how to sell pi coins on Bitmart crypto exchange
how to sell pi coins on Bitmart crypto exchangehow to sell pi coins on Bitmart crypto exchange
how to sell pi coins on Bitmart crypto exchange
DOT TECH
 
how to sell pi coins at high rate quickly.
how to sell pi coins at high rate quickly.how to sell pi coins at high rate quickly.
how to sell pi coins at high rate quickly.
DOT TECH
 
Poonawalla Fincorp and IndusInd Bank Introduce New Co-Branded Credit Card
Poonawalla Fincorp and IndusInd Bank Introduce New Co-Branded Credit CardPoonawalla Fincorp and IndusInd Bank Introduce New Co-Branded Credit Card
Poonawalla Fincorp and IndusInd Bank Introduce New Co-Branded Credit Card
nickysharmasucks
 
What price will pi network be listed on exchanges
What price will pi network be listed on exchangesWhat price will pi network be listed on exchanges
What price will pi network be listed on exchanges
DOT TECH
 

Recently uploaded (20)

Intro_Economics_ GPresentation Week 4.pptx
Intro_Economics_ GPresentation Week 4.pptxIntro_Economics_ GPresentation Week 4.pptx
Intro_Economics_ GPresentation Week 4.pptx
 
how can I sell/buy bulk pi coins securely
how can I sell/buy bulk pi coins securelyhow can I sell/buy bulk pi coins securely
how can I sell/buy bulk pi coins securely
 
Summary of financial results for 1Q2024
Summary of financial  results for 1Q2024Summary of financial  results for 1Q2024
Summary of financial results for 1Q2024
 
一比一原版Birmingham毕业证伯明翰大学|学院毕业证成绩单如何办理
一比一原版Birmingham毕业证伯明翰大学|学院毕业证成绩单如何办理一比一原版Birmingham毕业证伯明翰大学|学院毕业证成绩单如何办理
一比一原版Birmingham毕业证伯明翰大学|学院毕业证成绩单如何办理
 
NO1 Uk Divorce problem uk all amil baba in karachi,lahore,pakistan talaq ka m...
NO1 Uk Divorce problem uk all amil baba in karachi,lahore,pakistan talaq ka m...NO1 Uk Divorce problem uk all amil baba in karachi,lahore,pakistan talaq ka m...
NO1 Uk Divorce problem uk all amil baba in karachi,lahore,pakistan talaq ka m...
 
what is the future of Pi Network currency.
what is the future of Pi Network currency.what is the future of Pi Network currency.
what is the future of Pi Network currency.
 
Monthly Economic Monitoring of Ukraine No. 232, May 2024
Monthly Economic Monitoring of Ukraine No. 232, May 2024Monthly Economic Monitoring of Ukraine No. 232, May 2024
Monthly Economic Monitoring of Ukraine No. 232, May 2024
 
USDA Loans in California: A Comprehensive Overview.pptx
USDA Loans in California: A Comprehensive Overview.pptxUSDA Loans in California: A Comprehensive Overview.pptx
USDA Loans in California: A Comprehensive Overview.pptx
 
how can I sell pi coins after successfully completing KYC
how can I sell pi coins after successfully completing KYChow can I sell pi coins after successfully completing KYC
how can I sell pi coins after successfully completing KYC
 
Which Crypto to Buy Today for Short-Term in May-June 2024.pdf
Which Crypto to Buy Today for Short-Term in May-June 2024.pdfWhich Crypto to Buy Today for Short-Term in May-June 2024.pdf
Which Crypto to Buy Today for Short-Term in May-June 2024.pdf
 
NO1 Uk Rohani Baba In Karachi Bangali Baba Karachi Online Amil Baba WorldWide...
NO1 Uk Rohani Baba In Karachi Bangali Baba Karachi Online Amil Baba WorldWide...NO1 Uk Rohani Baba In Karachi Bangali Baba Karachi Online Amil Baba WorldWide...
NO1 Uk Rohani Baba In Karachi Bangali Baba Karachi Online Amil Baba WorldWide...
 
how can I sell my pi coins for cash in a pi APP
how can I sell my pi coins for cash in a pi APPhow can I sell my pi coins for cash in a pi APP
how can I sell my pi coins for cash in a pi APP
 
PF-Wagner's Theory of Public Expenditure.pptx
PF-Wagner's Theory of Public Expenditure.pptxPF-Wagner's Theory of Public Expenditure.pptx
PF-Wagner's Theory of Public Expenditure.pptx
 
Introduction to Indian Financial System ()
Introduction to Indian Financial System ()Introduction to Indian Financial System ()
Introduction to Indian Financial System ()
 
how to sell pi coins on Binance exchange
how to sell pi coins on Binance exchangehow to sell pi coins on Binance exchange
how to sell pi coins on Binance exchange
 
what is a pi whale and how to access one.
what is a pi whale and how to access one.what is a pi whale and how to access one.
what is a pi whale and how to access one.
 
how to sell pi coins on Bitmart crypto exchange
how to sell pi coins on Bitmart crypto exchangehow to sell pi coins on Bitmart crypto exchange
how to sell pi coins on Bitmart crypto exchange
 
how to sell pi coins at high rate quickly.
how to sell pi coins at high rate quickly.how to sell pi coins at high rate quickly.
how to sell pi coins at high rate quickly.
 
Poonawalla Fincorp and IndusInd Bank Introduce New Co-Branded Credit Card
Poonawalla Fincorp and IndusInd Bank Introduce New Co-Branded Credit CardPoonawalla Fincorp and IndusInd Bank Introduce New Co-Branded Credit Card
Poonawalla Fincorp and IndusInd Bank Introduce New Co-Branded Credit Card
 
What price will pi network be listed on exchanges
What price will pi network be listed on exchangesWhat price will pi network be listed on exchanges
What price will pi network be listed on exchanges
 

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