SlideShare a Scribd company logo
1 of 13
Download to read offline
M.JANCYPRIYA, ASST. PROF., DEPT. OF CA., BON SECOURS COLLEGE, THANJAVUR
 A string is a sequence of character.
 We have used null terminated <char> arrays
(C-strings or C-style strings) to store and
manipulate strings.
 ANSI C++ provides a class called string.
 We must include <string> in our program.
M.JANCYPRIYA, BON SECOURS
COLLEGE, THANJAVUR
 Creating string objects.
 Reading string objects from keyboard.
 Displaying string objects to the screen.
 Finding a substring from a string.
 Modifying string objects.
 Adding string objects.
 Accessing characters in a string.
 Obtaining the size of string.
M.JANCYPRIYA, BON SECOURS
COLLEGE, THANJAVUR
 String();
◦ // For creating an empty string.
 String(const char *str);
◦ // For creating a string object from a null-
terminated string.
 String(const string &str);
◦ // For creating a string object from other string
object.
M.JANCYPRIYA, BON SECOURS
COLLEGE, THANJAVUR
 string s1, s3; // Using constructor with no arguments.
 string s2(“xyz”);// Using one-argument
constructor.
 s1 = s2; // Assigning string objects
 s3 = “abc” + s2; // Concatenating strings
 cin >> s1; // Reading from keyboard (one
word)
 cout << s2; // Display the content of s2
 getline(cin, s1) // Reading from keyboard a line of text
 s3 += s1; // s3 = s3 + s1;
 s3 += “abc”; // s3 = s3 + “abc”;
M.JANCYPRIYA, BON SECOURS
COLLEGE, THANJAVUR
 string s1(“12345”);
 string s2(“abcde”);
 s1.insert(4, s2); // s1 = 1234abcde5
 s1.erase(4, 5); // s1 = 12345
 s2.replace(1, 3, s1);// s2 = a12345e
M.JANCYPRIYA, BON SECOURS
COLLEGE, THANJAVUR
 insert()
 erase()
 replace()
 append()
M.JANCYPRIYA, BON SECOURS
COLLEGE, THANJAVUR
 string s1(“ABC”); string s2(“XYZ”);
 int x = s1.compare(s2);
◦ x == 0 if s1 == s2
◦ x > 0 if s1 > s2
◦ x < 0 if s1 < s2
Operator Meaning
== Equality
!= Inequality
< Less than
<= Less than or equal
> Greater than
>= Greater than or equal
M.JANCYPRIYA, BON SECOURS
COLLEGE, THANJAVUR
Function Task
size() Number of elements currently
stored
length() Number of elements currently
stored
capacity() Total elements that can be stored
max_size() Maximum size of a string object
that a system can support
Empty() Return true or 1 if the string is
empty otherwise returns false or
0
resize() Used to resize a string object
(effects only size and length)
M.JANCYPRIYA, BON SECOURS
COLLEGE, THANJAVUR
Function Task
at() For accessing individual characters
substr() For retrieving a substring
find() For finding a specific substring
find_first_of() For finding the location of first occurrence of the
specific character(s)
find_last_of() For finding the location of first occurrence of the
specific character(s)
[] operator For accessing individual character. Makes the string
object to look like an array.
M.JANCYPRIYA, BON SECOURS
COLLEGE, THANJAVUR
void display(string &str)
{
cout << “Size = ” << str.size() << endl;
cout << “Length = ” << str.length() << endl;
cout << “Capacity = ” << str.capacity() << endl;
cout << “Max Size = ” << str.max_size() <<
endl;
cout << “Empty: ” << (str.empty() ? “yes” : “no”)
<< endl;
cout << endl << endl;
}
M.JANCYPRIYA, BON SECOURS
COLLEGE, THANJAVUR
 There is another overloaded version of compare
 int compare(int start_1, int length_1, string s_2,
int start_2, int length_2)
 string s1, s2;
 int x = s1.compare(0, 2, s2, 0, 2);
 s1.swap(s2)
 Exchanges the content of string s1 and s2
M.JANCYPRIYA, BON SECOURS
COLLEGE, THANJAVUR
M.JANCYPRIYA, BON SECOURS
COLLEGE, THANJAVUR

More Related Content

What's hot (20)

Chapter 7 String
Chapter 7 StringChapter 7 String
Chapter 7 String
 
Strings in Java
Strings in JavaStrings in Java
Strings in Java
 
Strings and common operations
Strings and common operationsStrings and common operations
Strings and common operations
 
String handling(string class)
String handling(string class)String handling(string class)
String handling(string class)
 
Java Strings
Java StringsJava Strings
Java Strings
 
Chap 8(strings)
Chap 8(strings)Chap 8(strings)
Chap 8(strings)
 
String in java
String in javaString in java
String in java
 
Introduction to Java Strings, By Kavita Ganesan
Introduction to Java Strings, By Kavita GanesanIntroduction to Java Strings, By Kavita Ganesan
Introduction to Java Strings, By Kavita Ganesan
 
L13 string handling(string class)
L13 string handling(string class)L13 string handling(string class)
L13 string handling(string class)
 
String classes and its methods.20
String classes and its methods.20String classes and its methods.20
String classes and its methods.20
 
String Builder & String Buffer (Java Programming)
String Builder & String Buffer (Java Programming)String Builder & String Buffer (Java Programming)
String Builder & String Buffer (Java Programming)
 
Strings In OOP(Object oriented programming)
Strings In OOP(Object oriented programming)Strings In OOP(Object oriented programming)
Strings In OOP(Object oriented programming)
 
L14 string handling(string buffer class)
L14 string handling(string buffer class)L14 string handling(string buffer class)
L14 string handling(string buffer class)
 
Java String
Java String Java String
Java String
 
Arrays string handling java packages
Arrays string handling java packagesArrays string handling java packages
Arrays string handling java packages
 
String handling session 5
String handling session 5String handling session 5
String handling session 5
 
String Handling
String HandlingString Handling
String Handling
 
Java strings
Java   stringsJava   strings
Java strings
 
STRINGS IN JAVA
STRINGS IN JAVASTRINGS IN JAVA
STRINGS IN JAVA
 
Strings
StringsStrings
Strings
 

Similar to Strings (20)

String and string buffer
String and string bufferString and string buffer
String and string buffer
 
Manipulating strings
Manipulating stringsManipulating strings
Manipulating strings
 
Strings
StringsStrings
Strings
 
Java String Handling
Java String HandlingJava String Handling
Java String Handling
 
String handling(string class)
String handling(string class)String handling(string class)
String handling(string class)
 
JavaScript Objects
JavaScript ObjectsJavaScript Objects
JavaScript Objects
 
Lecture 7
Lecture 7Lecture 7
Lecture 7
 
Java string handling
Java string handlingJava string handling
Java string handling
 
Strings1
Strings1Strings1
Strings1
 
Module-1 Strings Handling.ppt.pdf
Module-1 Strings Handling.ppt.pdfModule-1 Strings Handling.ppt.pdf
Module-1 Strings Handling.ppt.pdf
 
Strings
StringsStrings
Strings
 
String handling(string buffer class)
String handling(string buffer class)String handling(string buffer class)
String handling(string buffer class)
 
String handling(string buffer class)
String handling(string buffer class)String handling(string buffer class)
String handling(string buffer class)
 
07slide
07slide07slide
07slide
 
Computer programming 2 Lesson 12
Computer programming 2  Lesson 12Computer programming 2  Lesson 12
Computer programming 2 Lesson 12
 
11-ch04-3-strings.pdf
11-ch04-3-strings.pdf11-ch04-3-strings.pdf
11-ch04-3-strings.pdf
 
Scala taxonomy
Scala taxonomyScala taxonomy
Scala taxonomy
 
String and string manipulation
String and string manipulationString and string manipulation
String and string manipulation
 
String and string manipulation x
String and string manipulation xString and string manipulation x
String and string manipulation x
 
String handling
String handlingString handling
String handling
 

More from Jancypriya M

Introduction to computer
Introduction to computerIntroduction to computer
Introduction to computerJancypriya M
 
Library automation software
Library automation softwareLibrary automation software
Library automation softwareJancypriya M
 
Flip Flop and Its Types
Flip Flop and Its TypesFlip Flop and Its Types
Flip Flop and Its TypesJancypriya M
 
Introduction to pagemaker
Introduction to pagemakerIntroduction to pagemaker
Introduction to pagemakerJancypriya M
 
Standard template library
Standard template libraryStandard template library
Standard template libraryJancypriya M
 

More from Jancypriya M (8)

Introduction to computer
Introduction to computerIntroduction to computer
Introduction to computer
 
Library automation software
Library automation softwareLibrary automation software
Library automation software
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Flip Flop and Its Types
Flip Flop and Its TypesFlip Flop and Its Types
Flip Flop and Its Types
 
Corel draw tools
Corel draw toolsCorel draw tools
Corel draw tools
 
Applet in java
Applet in javaApplet in java
Applet in java
 
Introduction to pagemaker
Introduction to pagemakerIntroduction to pagemaker
Introduction to pagemaker
 
Standard template library
Standard template libraryStandard template library
Standard template library
 

Recently uploaded

18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxUnboundStockton
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 

Recently uploaded (20)

18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docx
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 

Strings

  • 1. M.JANCYPRIYA, ASST. PROF., DEPT. OF CA., BON SECOURS COLLEGE, THANJAVUR
  • 2.  A string is a sequence of character.  We have used null terminated <char> arrays (C-strings or C-style strings) to store and manipulate strings.  ANSI C++ provides a class called string.  We must include <string> in our program. M.JANCYPRIYA, BON SECOURS COLLEGE, THANJAVUR
  • 3.  Creating string objects.  Reading string objects from keyboard.  Displaying string objects to the screen.  Finding a substring from a string.  Modifying string objects.  Adding string objects.  Accessing characters in a string.  Obtaining the size of string. M.JANCYPRIYA, BON SECOURS COLLEGE, THANJAVUR
  • 4.  String(); ◦ // For creating an empty string.  String(const char *str); ◦ // For creating a string object from a null- terminated string.  String(const string &str); ◦ // For creating a string object from other string object. M.JANCYPRIYA, BON SECOURS COLLEGE, THANJAVUR
  • 5.  string s1, s3; // Using constructor with no arguments.  string s2(“xyz”);// Using one-argument constructor.  s1 = s2; // Assigning string objects  s3 = “abc” + s2; // Concatenating strings  cin >> s1; // Reading from keyboard (one word)  cout << s2; // Display the content of s2  getline(cin, s1) // Reading from keyboard a line of text  s3 += s1; // s3 = s3 + s1;  s3 += “abc”; // s3 = s3 + “abc”; M.JANCYPRIYA, BON SECOURS COLLEGE, THANJAVUR
  • 6.  string s1(“12345”);  string s2(“abcde”);  s1.insert(4, s2); // s1 = 1234abcde5  s1.erase(4, 5); // s1 = 12345  s2.replace(1, 3, s1);// s2 = a12345e M.JANCYPRIYA, BON SECOURS COLLEGE, THANJAVUR
  • 7.  insert()  erase()  replace()  append() M.JANCYPRIYA, BON SECOURS COLLEGE, THANJAVUR
  • 8.  string s1(“ABC”); string s2(“XYZ”);  int x = s1.compare(s2); ◦ x == 0 if s1 == s2 ◦ x > 0 if s1 > s2 ◦ x < 0 if s1 < s2 Operator Meaning == Equality != Inequality < Less than <= Less than or equal > Greater than >= Greater than or equal M.JANCYPRIYA, BON SECOURS COLLEGE, THANJAVUR
  • 9. Function Task size() Number of elements currently stored length() Number of elements currently stored capacity() Total elements that can be stored max_size() Maximum size of a string object that a system can support Empty() Return true or 1 if the string is empty otherwise returns false or 0 resize() Used to resize a string object (effects only size and length) M.JANCYPRIYA, BON SECOURS COLLEGE, THANJAVUR
  • 10. Function Task at() For accessing individual characters substr() For retrieving a substring find() For finding a specific substring find_first_of() For finding the location of first occurrence of the specific character(s) find_last_of() For finding the location of first occurrence of the specific character(s) [] operator For accessing individual character. Makes the string object to look like an array. M.JANCYPRIYA, BON SECOURS COLLEGE, THANJAVUR
  • 11. void display(string &str) { cout << “Size = ” << str.size() << endl; cout << “Length = ” << str.length() << endl; cout << “Capacity = ” << str.capacity() << endl; cout << “Max Size = ” << str.max_size() << endl; cout << “Empty: ” << (str.empty() ? “yes” : “no”) << endl; cout << endl << endl; } M.JANCYPRIYA, BON SECOURS COLLEGE, THANJAVUR
  • 12.  There is another overloaded version of compare  int compare(int start_1, int length_1, string s_2, int start_2, int length_2)  string s1, s2;  int x = s1.compare(0, 2, s2, 0, 2);  s1.swap(s2)  Exchanges the content of string s1 and s2 M.JANCYPRIYA, BON SECOURS COLLEGE, THANJAVUR