SlideShare a Scribd company logo
OObbjjeecctt oorriieenntteedd PPrrooggrraammmmiinngg 
wwiitthh CC++++ 
MMaanniippuullaattiinngg SSttrriinnggss 
By 
Nilesh Dalvi 
LLeeccttuurreerr,, PPaattkkaarr--VVaarrddee CCoolllleeggee.. 
http://www.slideshare.net/nileshdalvi01
Introduction 
• A string is a sequence of characters. 
• Strings can contain small and capital letters, 
numbers and symbols. 
• Each element of string occupies a byte in the 
memory. 
• Every string is terminated by a null 
character(‘0’). 
• Its ASCII and Hex values are zero. 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Introduction 
• The string is stored in the memory as follows: 
char country [6] = "INDIA"; 
I N D I A '0' 
73 78 68 73 65 00 
• Each character occupies a single byte in 
memory as shown above. 
• The various operations with strings such as 
copying, comparing, concatenation, or 
replacing requires a lot of effort in ‘C’ 
programming. 
• These string is called as C-style string. 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
String library functions 
Functions Description 
strlen() Determines length of string. 
strcpy() Copies a string from source to destination 
strcmp() Compares characters of two strings. 
stricmp() Compares two strings. 
strlwr() Converts uppercase characters in strings to lowercase 
strupr() Converts lowercase characters in strings to uppercase 
strdup() Duplicates a string 
strchr() Determines first occurrence of given character in a string 
strcat() Appends source string to destination string 
strrev() Reversing all characters of a string 
strspn() finds up at what length two strings are identical 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Program explains above functions 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). 
#include <iostream> 
#include <string> 
using namespace std; 
int main() 
{ 
char name[10]; 
cout << "Enter name: " << endl; 
cin >> name; 
cout << "Length :" << strlen (name)<< endl; 
cout << "Reverse :" << strrev (name)<< endl; 
return 0; 
} 
Output: 
Enter name: 
ABC 
Length :3 
Reverse :CBA
Moving from C-String to C++String 
• Manipulation of string in the form of char 
array requires more effort, C uses library 
functions defined in string.h. 
• To make manipulation easy ANSI committee 
added a new class called string. 
• It allows us to define objects of string type 
and they can e used as built in data type. 
• The programmer should include the 
string header file. 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Declaring and initializing string objects 
• In C, we declare strings as given below: 
char name[10]; 
• Whereas in C++ string is declared as an 
object. 
• The string object declaration and 
initialization can be done at once using 
constructor in string class. 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
String constructors 
Constructors Meaning 
string (); Produces an empty string 
string (const char * text); Produces a string object from a 
null ended string 
string (const string & text); Produces a string object from 
other string objects 
We can declare and initialize string objects as follows: 
string text; // Declaration of string objects 
//using construtor without argument 
string text("C++"); //using construtor with one argument 
text1 = text2; //Asssignment of two string objects 
text = "C++"+ text1; //Concatenation of two strings objects 
Nilesh Dalvi, Lecturer@Patkar-Varde College, 
Goregaon(W).
Performing assignment and concatenation 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). 
#include <iostream> 
#include <string> 
using namespace std; 
int main() 
{ 
string text; 
string text1(" C++"); 
string text2(" OOP"); 
cout << "text1 : "<< text1 << endl; 
cout << "text2 : "<< text2 << endl; 
text = text1; // assignment operation 
text = text1 + text2; // concatenation 
cout << "Now text : "<<text << endl; 
return 0; 
} 
Output: 
text1 : C++ 
text2 : OOP 
Now text : C++ OOP
String manipulating functions 
Functions Description 
append() Adds one string at the end of another string 
assign() Assigns a specified part of string 
at() Access a characters located at given location 
begin() returns a reference to the beginning of a string 
capacity() calculates the total elements that can be stored 
compare() compares two strings 
empty() returns false if the string is not empty, otherwise true 
end() returns a reference to the termination of string 
erase() erases the specified character 
find() finds the given sub string in the source string 
insert() inserts a character at the given location 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
String manipulating functions 
Functions Description 
length() calculates the total number of elements in string 
replace() substitutes the specified character with the given string 
resize() modifies the size of the string as specified 
size() provides the number of character n the string 
swap() Exchanges the given string with another string 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
String Relational Operators 
Operator Working 
= Assignment 
+ joining two or more strings 
+= concatenation and assignment 
== Equality 
!= Not equal to 
< Less than 
<= Less than or equal 
> Greater than 
>= Greater than or equal 
[] Subscription 
<< insertion 
>> Extraction 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Comparing two strings using operators 
#include<iostream> 
#include<string> 
using namespace std; 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). 
int main() 
{ 
string s1("OOP"); 
string s2("OOP"); 
if(s1 == s2) 
cout << "n Both strings are identical"; 
else 
cout << "n Both strings are different"; 
return 0; 
}
Comparing two strings using compare() 
#include<iostream> 
#include<string> 
using namespace std; 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). 
int main() 
{ 
string s1("abd"); 
string s2("abc"); 
int d = s1.compare (s2); 
if(d == 0) 
cout << "n Both strings are identical"; 
else if(d > 0) 
cout << "n s1 is greater than s2"; 
else 
cout << "n s2 is greater than s1"; 
return 0; 
}
Inserting string using insert() 
#include<iostream> 
#include<string> 
using namespace std; 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). 
int main() 
{ 
string s1("abcpqr"); 
string s2("mno"); 
cout << "S1: " << s1 << endl; 
cout << "S2: " << s2 << endl; 
cout << "After Insertion: " << endl; 
s1.insert(3,s2); 
cout << "S1: " << s1 << endl; 
cout << "S2: " << s2 << endl; 
return 0; 
} 
Output :: 
S1: abcpqr 
S2: mno 
After Insertion: 
S1: abcmnopqr 
S2: mno
Remove specified character s using erase() 
#include<iostream> 
#include<string> 
using namespace std; 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). 
int main() 
{ 
string s1("abc1234pqr"); 
cout << "S1: " << s1 << endl; 
cout << "After Erase : " << endl; 
s1.erase(3,5); 
cout << "S1: " << s1 << endl; 
return 0; 
} 
Output : 
S1: abc1234pqr 
After Erase : 
S1: abcqr
String Attributes: size() 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). 
#include<iostream> 
#include<string> 
using namespace std; 
int main() 
{ 
string s1; 
cout << "Size : " << s1.size () << endl; 
s1 = "abc"; 
cout << "Size : " << s1.size () << endl; 
return 0; 
} 
Output :: 
Size : 0 
Size : 3
String Attributes: length() 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). 
#include<iostream> 
#include<string> 
using namespace std; 
int main() 
{ 
//length () : determines the length i.e. no. of characters 
string s1; 
cout << "Length : " << s1.length () << endl; 
s1 = "hello"; 
cout << "Length : " << s1.length () << endl; 
return 0; 
} 
Output :: 
Length : 0 
Length : 5
String Attributes: capacity() 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). 
#include<iostream> 
#include<string> 
using namespace std; 
int main() 
{ 
//capacity () : determines capacity of string object 
// i.e. no. of characters it can hold. 
string s1; 
cout << "Capacity : " << s1.capacity () << endl; 
s1 = "hello"; 
cout << "Capacity : " << s1.capacity () << endl; 
s1 = "abcdefghijklmnopqr"; 
cout << "Capacity : " << s1.capacity () << endl; 
return 0; 
} 
Output : 
Capacity : 15 
Capacity : 15 
Capacity : 31
String Attributes: empty() 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). 
#include<iostream> 
#include<string> 
using namespace std; 
int main() 
{ 
string s1; 
cout << "Empty : " <<( s1.empty ()? "True" : "False") << endl; 
s1 = "hello"; 
cout << "Empty : " <<( s1.empty ()? "True" : "False") << endl; 
return 0; 
}
Accessing elements of string : at() 
#include<iostream> 
#include<string> 
using namespace std; 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). 
int main() 
{ 
string s1("abcdefghijkl"); 
for(int i = 0; i < s1.length (); i++) 
cout << s1.at(i); // using at() 
for(int i = 0; i < s1.length (); i++) 
cout << s1. [i]; // using operator [] 
return 0; 
}
Accessing elements of string : find() 
#include<iostream> 
#include<string> 
using namespace std; 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). 
int main() 
{ 
string s1("TechTalks : Where everything is out of box!"); 
int x = s1.find ("box"); 
cout << "box is found at " << x << endl; 
return 0; 
} 
Output :: 
box is found at 39
Exchanging content of two strings: swap() 
#include<iostream> 
#include<string> 
using namespace std; 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). 
int main() 
{ 
string s1("ttt"); 
string s2("rrr"); 
cout << "S1 :" << s1 << endl; 
cout << "S2 :" << s2 << endl; 
s1.swap (s2); 
cout << "S1 :" << s1 << endl; 
cout << "S2 :" << s2 << endl; 
return 0; 
} 
Output : 
S1 :ttt 
S2 :rrr 
S1 :rrr 
S2 :ttt
Miscellaneous functions: assign() 
#include<iostream> 
#include<string> 
using namespace std; 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). 
int main() 
{ 
string s1("c plus plus"); 
string s2; 
s2.assign (s1, 0,6); 
cout << s2; 
return 0; 
} 
Output : 
c plus
Miscellaneous functions: begin() 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Miscellaneous functions: end() 
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Strings

More Related Content

PDF
Managing I/O in c++
PPTX
Inheritance in java
PPTX
Dynamic memory allocation in c
PPTX
C Programming: Control Structure
PPTX
Constructors in C++
PPTX
Strings in c++
PPTX
If else statement in c++
PPT
C++ Arrays
Managing I/O in c++
Inheritance in java
Dynamic memory allocation in c
C Programming: Control Structure
Constructors in C++
Strings in c++
If else statement in c++
C++ Arrays

What's hot (20)

PPTX
Unit 3. Input and Output
PPTX
Recursive Function
PPTX
07. Virtual Functions
PPTX
PDF
Function overloading ppt
PPTX
python conditional statement.pptx
PPTX
classes and objects in C++
PPT
Function overloading(c++)
PPTX
Looping statement in python
PPT
Operator Overloading
PPTX
Loops c++
PPTX
Control statements in c
PPTX
Loops in C Programming Language
PPT
RECURSION IN C
PPTX
Polymorphism In c++
PDF
Strings in python
PPTX
Data types in c++
PPTX
Function in c
PDF
Arrays in Java
Unit 3. Input and Output
Recursive Function
07. Virtual Functions
Function overloading ppt
python conditional statement.pptx
classes and objects in C++
Function overloading(c++)
Looping statement in python
Operator Overloading
Loops c++
Control statements in c
Loops in C Programming Language
RECURSION IN C
Polymorphism In c++
Strings in python
Data types in c++
Function in c
Arrays in Java
Ad

Viewers also liked (20)

DOC
String in c
PPTX
PPTX
C programming - String
PDF
PPT
PPT
Strings Functions in C Programming
PPSX
C programming string
PPTX
Strings in C
PPTX
String in programming language in c or c++
PDF
Implementation of c string functions
PPT
File handling
PPTX
String Handling in c++
PPT
Computer Programming- Lecture 5
PDF
file handling c++
PPTX
Array in c language
PPSX
INTRODUCTION TO C PROGRAMMING
PPT
Handling Exceptions In C &amp; C++ [Part B] Ver 2
PDF
05 c++-strings
PPT
7. Multithreading
String in c
C programming - String
Strings Functions in C Programming
C programming string
Strings in C
String in programming language in c or c++
Implementation of c string functions
File handling
String Handling in c++
Computer Programming- Lecture 5
file handling c++
Array in c language
INTRODUCTION TO C PROGRAMMING
Handling Exceptions In C &amp; C++ [Part B] Ver 2
05 c++-strings
7. Multithreading
Ad

Similar to Strings (20)

PPTX
The string class
PDF
C Strings oops with c++ ppt or pdf can be done.pdf
PDF
Strinng Classes in c++
PDF
Strings
PPTX
Cs1123 9 strings
PPT
Manipulation of Strings
PPT
C++ Strings.ppt
PPT
lecture5.ppt
PPT
lecture 5 string in c++ explaination and example.ppt
PPTX
3 (3)Arrays and Strings for 11,12,college.pptx
PPTX
lecture-5 string.pptx
PPTX
ppt8-string-1.pptx
PDF
String class and function for b.tech iii year students
PDF
C string _updated_Somesh_SSTC_ Bhilai_CG
PPTX
Manipulation strings in c++
PPTX
String in programming language in c or c++
PDF
Chapter 4 (Part II) - Array and Strings.pdf
PPTX
programming for problem solving using C-STRINGSc
PDF
Manipulating strings
The string class
C Strings oops with c++ ppt or pdf can be done.pdf
Strinng Classes in c++
Strings
Cs1123 9 strings
Manipulation of Strings
C++ Strings.ppt
lecture5.ppt
lecture 5 string in c++ explaination and example.ppt
3 (3)Arrays and Strings for 11,12,college.pptx
lecture-5 string.pptx
ppt8-string-1.pptx
String class and function for b.tech iii year students
C string _updated_Somesh_SSTC_ Bhilai_CG
Manipulation strings in c++
String in programming language in c or c++
Chapter 4 (Part II) - Array and Strings.pdf
programming for problem solving using C-STRINGSc
Manipulating strings

More from Nilesh Dalvi (20)

PPT
14. Linked List
PPT
13. Queue
PPT
12. Stack
PPT
11. Arrays
PPT
10. Introduction to Datastructure
PPT
9. Input Output in java
PPT
8. String
PPT
6. Exception Handling
PPT
5. Inheritances, Packages and Intefaces
PPT
4. Classes and Methods
PPT
3. Data types and Variables
PPT
2. Basics of Java
PPT
1. Overview of Java
PPT
Standard Template Library
PPT
Templates
PPT
Input and output in C++
PPT
Polymorphism
PPT
Inheritance : Extending Classes
PDF
Constructors and destructors
PDF
Classes and objects
14. Linked List
13. Queue
12. Stack
11. Arrays
10. Introduction to Datastructure
9. Input Output in java
8. String
6. Exception Handling
5. Inheritances, Packages and Intefaces
4. Classes and Methods
3. Data types and Variables
2. Basics of Java
1. Overview of Java
Standard Template Library
Templates
Input and output in C++
Polymorphism
Inheritance : Extending Classes
Constructors and destructors
Classes and objects

Recently uploaded (20)

PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
Pharma ospi slides which help in ospi learning
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Cell Structure & Organelles in detailed.
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Basic Mud Logging Guide for educational purpose
PDF
Business Ethics Teaching Materials for college
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PPTX
Institutional Correction lecture only . . .
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
RMMM.pdf make it easy to upload and study
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Pharma ospi slides which help in ospi learning
Final Presentation General Medicine 03-08-2024.pptx
Cell Structure & Organelles in detailed.
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Basic Mud Logging Guide for educational purpose
Business Ethics Teaching Materials for college
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
Institutional Correction lecture only . . .
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Abdominal Access Techniques with Prof. Dr. R K Mishra
RMMM.pdf make it easy to upload and study
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
O5-L3 Freight Transport Ops (International) V1.pdf
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf

Strings

  • 1. OObbjjeecctt oorriieenntteedd PPrrooggrraammmmiinngg wwiitthh CC++++ MMaanniippuullaattiinngg SSttrriinnggss By Nilesh Dalvi LLeeccttuurreerr,, PPaattkkaarr--VVaarrddee CCoolllleeggee.. http://www.slideshare.net/nileshdalvi01
  • 2. Introduction • A string is a sequence of characters. • Strings can contain small and capital letters, numbers and symbols. • Each element of string occupies a byte in the memory. • Every string is terminated by a null character(‘0’). • Its ASCII and Hex values are zero. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 3. Introduction • The string is stored in the memory as follows: char country [6] = "INDIA"; I N D I A '0' 73 78 68 73 65 00 • Each character occupies a single byte in memory as shown above. • The various operations with strings such as copying, comparing, concatenation, or replacing requires a lot of effort in ‘C’ programming. • These string is called as C-style string. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 4. String library functions Functions Description strlen() Determines length of string. strcpy() Copies a string from source to destination strcmp() Compares characters of two strings. stricmp() Compares two strings. strlwr() Converts uppercase characters in strings to lowercase strupr() Converts lowercase characters in strings to uppercase strdup() Duplicates a string strchr() Determines first occurrence of given character in a string strcat() Appends source string to destination string strrev() Reversing all characters of a string strspn() finds up at what length two strings are identical Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 5. Program explains above functions Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). #include <iostream> #include <string> using namespace std; int main() { char name[10]; cout << "Enter name: " << endl; cin >> name; cout << "Length :" << strlen (name)<< endl; cout << "Reverse :" << strrev (name)<< endl; return 0; } Output: Enter name: ABC Length :3 Reverse :CBA
  • 6. Moving from C-String to C++String • Manipulation of string in the form of char array requires more effort, C uses library functions defined in string.h. • To make manipulation easy ANSI committee added a new class called string. • It allows us to define objects of string type and they can e used as built in data type. • The programmer should include the string header file. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 7. Declaring and initializing string objects • In C, we declare strings as given below: char name[10]; • Whereas in C++ string is declared as an object. • The string object declaration and initialization can be done at once using constructor in string class. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 8. String constructors Constructors Meaning string (); Produces an empty string string (const char * text); Produces a string object from a null ended string string (const string & text); Produces a string object from other string objects We can declare and initialize string objects as follows: string text; // Declaration of string objects //using construtor without argument string text("C++"); //using construtor with one argument text1 = text2; //Asssignment of two string objects text = "C++"+ text1; //Concatenation of two strings objects Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 9. Performing assignment and concatenation Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). #include <iostream> #include <string> using namespace std; int main() { string text; string text1(" C++"); string text2(" OOP"); cout << "text1 : "<< text1 << endl; cout << "text2 : "<< text2 << endl; text = text1; // assignment operation text = text1 + text2; // concatenation cout << "Now text : "<<text << endl; return 0; } Output: text1 : C++ text2 : OOP Now text : C++ OOP
  • 10. String manipulating functions Functions Description append() Adds one string at the end of another string assign() Assigns a specified part of string at() Access a characters located at given location begin() returns a reference to the beginning of a string capacity() calculates the total elements that can be stored compare() compares two strings empty() returns false if the string is not empty, otherwise true end() returns a reference to the termination of string erase() erases the specified character find() finds the given sub string in the source string insert() inserts a character at the given location Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 11. String manipulating functions Functions Description length() calculates the total number of elements in string replace() substitutes the specified character with the given string resize() modifies the size of the string as specified size() provides the number of character n the string swap() Exchanges the given string with another string Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 12. String Relational Operators Operator Working = Assignment + joining two or more strings += concatenation and assignment == Equality != Not equal to < Less than <= Less than or equal > Greater than >= Greater than or equal [] Subscription << insertion >> Extraction Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 13. Comparing two strings using operators #include<iostream> #include<string> using namespace std; Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). int main() { string s1("OOP"); string s2("OOP"); if(s1 == s2) cout << "n Both strings are identical"; else cout << "n Both strings are different"; return 0; }
  • 14. Comparing two strings using compare() #include<iostream> #include<string> using namespace std; Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). int main() { string s1("abd"); string s2("abc"); int d = s1.compare (s2); if(d == 0) cout << "n Both strings are identical"; else if(d > 0) cout << "n s1 is greater than s2"; else cout << "n s2 is greater than s1"; return 0; }
  • 15. Inserting string using insert() #include<iostream> #include<string> using namespace std; Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). int main() { string s1("abcpqr"); string s2("mno"); cout << "S1: " << s1 << endl; cout << "S2: " << s2 << endl; cout << "After Insertion: " << endl; s1.insert(3,s2); cout << "S1: " << s1 << endl; cout << "S2: " << s2 << endl; return 0; } Output :: S1: abcpqr S2: mno After Insertion: S1: abcmnopqr S2: mno
  • 16. Remove specified character s using erase() #include<iostream> #include<string> using namespace std; Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). int main() { string s1("abc1234pqr"); cout << "S1: " << s1 << endl; cout << "After Erase : " << endl; s1.erase(3,5); cout << "S1: " << s1 << endl; return 0; } Output : S1: abc1234pqr After Erase : S1: abcqr
  • 17. String Attributes: size() Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). #include<iostream> #include<string> using namespace std; int main() { string s1; cout << "Size : " << s1.size () << endl; s1 = "abc"; cout << "Size : " << s1.size () << endl; return 0; } Output :: Size : 0 Size : 3
  • 18. String Attributes: length() Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). #include<iostream> #include<string> using namespace std; int main() { //length () : determines the length i.e. no. of characters string s1; cout << "Length : " << s1.length () << endl; s1 = "hello"; cout << "Length : " << s1.length () << endl; return 0; } Output :: Length : 0 Length : 5
  • 19. String Attributes: capacity() Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). #include<iostream> #include<string> using namespace std; int main() { //capacity () : determines capacity of string object // i.e. no. of characters it can hold. string s1; cout << "Capacity : " << s1.capacity () << endl; s1 = "hello"; cout << "Capacity : " << s1.capacity () << endl; s1 = "abcdefghijklmnopqr"; cout << "Capacity : " << s1.capacity () << endl; return 0; } Output : Capacity : 15 Capacity : 15 Capacity : 31
  • 20. String Attributes: empty() Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). #include<iostream> #include<string> using namespace std; int main() { string s1; cout << "Empty : " <<( s1.empty ()? "True" : "False") << endl; s1 = "hello"; cout << "Empty : " <<( s1.empty ()? "True" : "False") << endl; return 0; }
  • 21. Accessing elements of string : at() #include<iostream> #include<string> using namespace std; Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). int main() { string s1("abcdefghijkl"); for(int i = 0; i < s1.length (); i++) cout << s1.at(i); // using at() for(int i = 0; i < s1.length (); i++) cout << s1. [i]; // using operator [] return 0; }
  • 22. Accessing elements of string : find() #include<iostream> #include<string> using namespace std; Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). int main() { string s1("TechTalks : Where everything is out of box!"); int x = s1.find ("box"); cout << "box is found at " << x << endl; return 0; } Output :: box is found at 39
  • 23. Exchanging content of two strings: swap() #include<iostream> #include<string> using namespace std; Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). int main() { string s1("ttt"); string s2("rrr"); cout << "S1 :" << s1 << endl; cout << "S2 :" << s2 << endl; s1.swap (s2); cout << "S1 :" << s1 << endl; cout << "S2 :" << s2 << endl; return 0; } Output : S1 :ttt S2 :rrr S1 :rrr S2 :ttt
  • 24. Miscellaneous functions: assign() #include<iostream> #include<string> using namespace std; Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). int main() { string s1("c plus plus"); string s2; s2.assign (s1, 0,6); cout << s2; return 0; } Output : c plus
  • 25. Miscellaneous functions: begin() Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 26. Miscellaneous functions: end() Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).