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

What's hot

Classes and objects
Classes and objectsClasses and objects
Classes and objects
Nilesh Dalvi
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
HalaiHansaika
 
Strings in C
Strings in CStrings in C
Strings in C
Kamal Acharya
 
Structure in C
Structure in CStructure in C
Structure in C
Kamal Acharya
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of Constructors
Dhrumil Panchal
 
Python-03| Data types
Python-03| Data typesPython-03| Data types
Python-03| Data types
Mohd Sajjad
 
C++ string
C++ stringC++ string
C++ string
Dheenadayalan18
 
The string class
The string classThe string class
The string class
Syed Zaid Irshad
 
Function in C
Function in CFunction in C
Function in C
Dr. Abhineet Anand
 
Templates in c++
Templates in c++Templates in c++
Templates in c++
ThamizhselviKrishnam
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
Shuvongkor Barman
 
Array ppt
Array pptArray ppt
Array ppt
Kaushal Mehta
 
String Handling in c++
String Handling in c++String Handling in c++
String Handling in c++Fahim Adil
 
Strings in c
Strings in cStrings in c
Strings in c
vampugani
 
Python Flow Control
Python Flow ControlPython Flow Control
Python Flow Control
Mohammed Sikander
 
String C Programming
String C ProgrammingString C Programming
String C Programming
Prionto Abdullah
 
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 Data Structures - Lecture 9 [Stack & Queue using Linked List] Data Structures - Lecture 9 [Stack & Queue using Linked List]
Data Structures - Lecture 9 [Stack & Queue using Linked List]
Muhammad Hammad Waseem
 

What's hot (20)

Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
 
Strings in C
Strings in CStrings in C
Strings in C
 
Structure in C
Structure in CStructure in C
Structure in C
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of Constructors
 
Python-03| Data types
Python-03| Data typesPython-03| Data types
Python-03| Data types
 
C++ string
C++ stringC++ string
C++ string
 
The string class
The string classThe string class
The string class
 
Function in C
Function in CFunction in C
Function in C
 
Templates in c++
Templates in c++Templates in c++
Templates in c++
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
 
Array ppt
Array pptArray ppt
Array ppt
 
String Handling in c++
String Handling in c++String Handling in c++
String Handling in c++
 
File handling in c++
File handling in c++File handling in c++
File handling in c++
 
Strings in c
Strings in cStrings in c
Strings in c
 
String functions in C
String functions in CString functions in C
String functions in C
 
Python Flow Control
Python Flow ControlPython Flow Control
Python Flow Control
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
String C Programming
String C ProgrammingString C Programming
String C Programming
 
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 Data Structures - Lecture 9 [Stack & Queue using Linked List] Data Structures - Lecture 9 [Stack & Queue using Linked List]
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 

Viewers also liked

String in c
String in cString in c
String in c
Suneel Dogra
 
C programming - String
C programming - StringC programming - String
C programming - String
Achyut Devkota
 
String c
String cString c
Strings Functions in C Programming
Strings Functions in C ProgrammingStrings Functions in C Programming
Strings Functions in C Programming
DevoAjit Gupta
 
C programming string
C  programming stringC  programming string
C programming string
argusacademy
 
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++
Samsil Arefin
 
Implementation of c string functions
Implementation of c string functionsImplementation of c string functions
Implementation of c string functions
mohamed sikander
 
File handling
File handlingFile handling
File handling
Nilesh Dalvi
 
Computer Programming- Lecture 5
Computer Programming- Lecture 5 Computer Programming- Lecture 5
Computer Programming- Lecture 5
Dr. Md. Shohel Sayeed
 
file handling c++
file handling c++file handling c++
file handling c++Guddu Spy
 
Array in c language
Array in c languageArray in c language
Array in c languagehome
 
INTRODUCTION TO C PROGRAMMING
INTRODUCTION TO C PROGRAMMINGINTRODUCTION TO C PROGRAMMING
INTRODUCTION TO C PROGRAMMING
Abhishek Dwivedi
 
Handling Exceptions In C &amp; C++ [Part B] Ver 2
Handling Exceptions In C &amp; C++ [Part B] Ver 2Handling Exceptions In C &amp; C++ [Part B] Ver 2
Handling Exceptions In C &amp; C++ [Part B] Ver 2
ppd1961
 
7. Multithreading
7. Multithreading7. Multithreading
7. Multithreading
Nilesh Dalvi
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
Nilesh Dalvi
 
14. Linked List
14. Linked List14. Linked List
14. Linked List
Nilesh Dalvi
 
Handling Exceptions In C &amp; C++[Part A]
Handling Exceptions In C &amp; C++[Part A]Handling Exceptions In C &amp; C++[Part A]
Handling Exceptions In C &amp; C++[Part A]
ppd1961
 

Viewers also liked (20)

String in c
String in cString in c
String in c
 
C programming - String
C programming - StringC programming - String
C programming - String
 
Strings
StringsStrings
Strings
 
String c
String cString c
String c
 
Strings Functions in C Programming
Strings Functions in C ProgrammingStrings Functions in C Programming
Strings Functions in C Programming
 
C programming string
C  programming stringC  programming string
C programming string
 
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++
 
Implementation of c string functions
Implementation of c string functionsImplementation of c string functions
Implementation of c string functions
 
File handling
File handlingFile handling
File handling
 
Computer Programming- Lecture 5
Computer Programming- Lecture 5 Computer Programming- Lecture 5
Computer Programming- Lecture 5
 
file handling c++
file handling c++file handling c++
file handling c++
 
Array in c language
Array in c languageArray in c language
Array in c language
 
INTRODUCTION TO C PROGRAMMING
INTRODUCTION TO C PROGRAMMINGINTRODUCTION TO C PROGRAMMING
INTRODUCTION TO C PROGRAMMING
 
Handling Exceptions In C &amp; C++ [Part B] Ver 2
Handling Exceptions In C &amp; C++ [Part B] Ver 2Handling Exceptions In C &amp; C++ [Part B] Ver 2
Handling Exceptions In C &amp; C++ [Part B] Ver 2
 
05 c++-strings
05 c++-strings05 c++-strings
05 c++-strings
 
7. Multithreading
7. Multithreading7. Multithreading
7. Multithreading
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Structures,pointers and strings in c Programming
Structures,pointers and strings in c ProgrammingStructures,pointers and strings in c Programming
Structures,pointers and strings in c Programming
 
14. Linked List
14. Linked List14. Linked List
14. Linked List
 
Handling Exceptions In C &amp; C++[Part A]
Handling Exceptions In C &amp; C++[Part A]Handling Exceptions In C &amp; C++[Part A]
Handling Exceptions In C &amp; C++[Part A]
 

Similar to Strings

Standard Template Library
Standard Template LibraryStandard Template Library
Standard Template Library
Nilesh Dalvi
 
Input and output in C++
Input and output in C++Input and output in C++
Input and output in C++
Nilesh Dalvi
 
lecture-5 string.pptx
lecture-5 string.pptxlecture-5 string.pptx
lecture-5 string.pptx
DilanAlmsa
 
8. String
8. String8. String
8. String
Nilesh Dalvi
 
C++ Strings.ppt
C++ Strings.pptC++ Strings.ppt
C++ Strings.ppt
DilanAlmsa
 
Java string handling
Java string handlingJava string handling
Java string handling
GaneshKumarKanthiah
 
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
 
Manipulating strings
Manipulating stringsManipulating strings
Manipulating strings
Jancypriya M
 
Manipulation strings in c++
Manipulation strings in c++Manipulation strings in c++
Manipulation strings in c++
SeethaDinesh
 
13 Strings and Text Processing
13 Strings and Text Processing13 Strings and Text Processing
13 Strings and Text Processing
Intro C# Book
 
String predefined functions in C programming
String predefined functions in C  programmingString predefined functions in C  programming
String predefined functions in C programming
ProfSonaliGholveDoif
 
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
Kavita Ganesan
 
(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?Tomasz Wrobel
 
CS101- Introduction to Computing- Lecture 38
CS101- Introduction to Computing- Lecture 38CS101- Introduction to Computing- Lecture 38
CS101- Introduction to Computing- Lecture 38
Bilal Ahmed
 
Scala in Places API
Scala in Places APIScala in Places API
Scala in Places API
Łukasz Bałamut
 
String and string manipulation
String and string manipulationString and string manipulation
String and string manipulationShahjahan Samoon
 
Structured data type
Structured data typeStructured data type
Structured data type
Omkar Majukar
 

Similar to Strings (20)

Standard Template Library
Standard Template LibraryStandard Template Library
Standard Template Library
 
Input and output in C++
Input and output in C++Input and output in C++
Input and output in C++
 
Java Strings
Java StringsJava Strings
Java Strings
 
lecture-5 string.pptx
lecture-5 string.pptxlecture-5 string.pptx
lecture-5 string.pptx
 
8. String
8. String8. String
8. String
 
C++ Strings.ppt
C++ Strings.pptC++ Strings.ppt
C++ Strings.ppt
 
Java string handling
Java string handlingJava string handling
Java string handling
 
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++
 
Manipulating strings
Manipulating stringsManipulating strings
Manipulating strings
 
Manipulation strings in c++
Manipulation strings in c++Manipulation strings in c++
Manipulation strings in c++
 
Data structure
Data structureData structure
Data structure
 
13 Strings and Text Processing
13 Strings and Text Processing13 Strings and Text Processing
13 Strings and Text Processing
 
String predefined functions in C programming
String predefined functions in C  programmingString predefined functions in C  programming
String predefined functions in C programming
 
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
 
(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?
 
CS101- Introduction to Computing- Lecture 38
CS101- Introduction to Computing- Lecture 38CS101- Introduction to Computing- Lecture 38
CS101- Introduction to Computing- Lecture 38
 
Ruby Basics
Ruby BasicsRuby Basics
Ruby Basics
 
Scala in Places API
Scala in Places APIScala in Places API
Scala in Places API
 
String and string manipulation
String and string manipulationString and string manipulation
String and string manipulation
 
Structured data type
Structured data typeStructured data type
Structured data type
 

More from Nilesh Dalvi

13. Queue
13. Queue13. Queue
13. Queue
Nilesh Dalvi
 
12. Stack
12. Stack12. Stack
12. Stack
Nilesh Dalvi
 
11. Arrays
11. Arrays11. Arrays
11. Arrays
Nilesh Dalvi
 
10. Introduction to Datastructure
10. Introduction to Datastructure10. Introduction to Datastructure
10. Introduction to Datastructure
Nilesh Dalvi
 
9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in java
Nilesh Dalvi
 
6. Exception Handling
6. Exception Handling6. Exception Handling
6. Exception Handling
Nilesh Dalvi
 
5. Inheritances, Packages and Intefaces
5. Inheritances, Packages and Intefaces5. Inheritances, Packages and Intefaces
5. Inheritances, Packages and Intefaces
Nilesh Dalvi
 
4. Classes and Methods
4. Classes and Methods4. Classes and Methods
4. Classes and Methods
Nilesh Dalvi
 
3. Data types and Variables
3. Data types and Variables3. Data types and Variables
3. Data types and Variables
Nilesh Dalvi
 
2. Basics of Java
2. Basics of Java2. Basics of Java
2. Basics of Java
Nilesh Dalvi
 
1. Overview of Java
1. Overview of Java1. Overview of Java
1. Overview of Java
Nilesh Dalvi
 
Templates
TemplatesTemplates
Templates
Nilesh Dalvi
 
Inheritance : Extending Classes
Inheritance : Extending ClassesInheritance : Extending Classes
Inheritance : Extending Classes
Nilesh Dalvi
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
Nilesh Dalvi
 
Introduction to cpp
Introduction to cppIntroduction to cpp
Introduction to cpp
Nilesh Dalvi
 
Introduction to oops concepts
Introduction to oops conceptsIntroduction to oops concepts
Introduction to oops concepts
Nilesh Dalvi
 

More from Nilesh Dalvi (16)

13. Queue
13. Queue13. Queue
13. Queue
 
12. Stack
12. Stack12. Stack
12. Stack
 
11. Arrays
11. Arrays11. Arrays
11. Arrays
 
10. Introduction to Datastructure
10. Introduction to Datastructure10. Introduction to Datastructure
10. Introduction to Datastructure
 
9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in java
 
6. Exception Handling
6. Exception Handling6. Exception Handling
6. Exception Handling
 
5. Inheritances, Packages and Intefaces
5. Inheritances, Packages and Intefaces5. Inheritances, Packages and Intefaces
5. Inheritances, Packages and Intefaces
 
4. Classes and Methods
4. Classes and Methods4. Classes and Methods
4. Classes and Methods
 
3. Data types and Variables
3. Data types and Variables3. Data types and Variables
3. Data types and Variables
 
2. Basics of Java
2. Basics of Java2. Basics of Java
2. Basics of Java
 
1. Overview of Java
1. Overview of Java1. Overview of Java
1. Overview of Java
 
Templates
TemplatesTemplates
Templates
 
Inheritance : Extending Classes
Inheritance : Extending ClassesInheritance : Extending Classes
Inheritance : Extending Classes
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
Introduction to cpp
Introduction to cppIntroduction to cpp
Introduction to cpp
 
Introduction to oops concepts
Introduction to oops conceptsIntroduction to oops concepts
Introduction to oops concepts
 

Recently uploaded

The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
Marketing internship report file for MBA
Marketing internship report file for MBAMarketing internship report file for MBA
Marketing internship report file for MBA
gb193092
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
timhan337
 
Chapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdfChapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdf
Kartik Tiwari
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 

Recently uploaded (20)

The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
Marketing internship report file for MBA
Marketing internship report file for MBAMarketing internship report file for MBA
Marketing internship report file for MBA
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
 
Chapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdfChapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdf
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 

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).