SlideShare a Scribd company logo
1 of 37
START
STRING
<BSCS-G-SECTION>
Presented by
 Muhammad Azeem
 Muhammad Maaz Usman
 Osman Asgher
 Umer Farooq
TEACHER: MA’AM SARAH
Generally speaking, a string is a sequence of characters
• Examples: “hello”, “high school”, “H2O”.
DEFINITION OF STRINGS
“Hello"
“High school"
DECLARATION OF STRINGS
The following instructions are all equivalent.
They declare x to be an object of type string,
and assign the string “high school” to it:
string x(“high school”);
DECLARATION OF STRINGS
string x= “high school”;
DECLARATION OF STRINGS
string x;
x=“high school”;
TYPE OF STRING
C++ provides following two types of string representations,
 The C-style character string (#include<string.h> Or #include<cstring>).
 The string class type introduced with Standard C++ (#include<string>).
 When we use C-string Functions so, we use this library.
(#include<string.h> Or #include<cstring>)
 When we use String Class Functions so, we use this library.
(#include<string>)
LIBRARY FOR C-STRING &
STRING CLASS
C-STRING
 Strings are arrays of chars. String literals are words surrounded by double quotation marks.
 The string in C programming language is actually a one-dimensional array of characters which is
terminated by a null character ‘0’. Thus a null-terminated string contains the characters that comprise
the string followed by a null.
 A string can be declared as a character array or with a string pointer.
 The following declaration and initialization create a string consisting of the word "Hello". To hold the
null character at the end of the array, the size of the character array containing the string is one more
than the number of characters in the word "Hello”.
OR
"This is a static string"
char aj[6]={‘H’,’E’,’L’,’L’,’O’,’0’} char aj[ ]={“HELLO”}
char *aj={“HELLO”}
 Following is the memory presentation of above defined string in C/C++:
 It's important to remember that there will be an extra character on the end on a string, literally a '0'
character, just like there is always a period at the end of a sentence. Since this string terminator is
unprintable, it is not counted as a letter, but it still takes up a space. Technically, in a fifty char array
you could only hold 49 letters and one null character at the end to terminate the string.
 Actually, you do not place the null character at the end of a string constant. The C compiler
automatically places the '0' at the end of the string when it initializes the array.
STRING MEMORY
INDEX 0 1 2 3 4 5
VARIABLE ‘H’ ‘A’ ‘L’ ‘L’ ‘O’ ‘0’
ADDRESS 0*23451 0*23452 0*23453 0*23454 0*23455 0*23456
Five char array you could only hold 4
letters and one null character at the end to
terminate the string.
Six char array you could only hold
6 letters and one null character at the end
to terminate the string.
C++ OPERATORS ARE NOT USE IN
C-STRING
 C++ Operator can’t be applied to them:
OPERATORS
Sr. No Function & Purpose
strcpy(s1, s2);
Copies string s2 into string s1.
strcat(s1, s2);
Concatenates string s2 onto the end of string s1.
strlen(s1);
Returns the length of string s1.
strcmp(s1, s2);
Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2.
C++ strncpy()
The strncpy() function in C++ copies a specified bytes of characters from source to destination.
strrev(s1, s2)
Reverse the string
C-STRING FUNCTIONS
strcpy(S1, S2);
COPIES STRING S2 INTO STRING S1.
C++ STRNCPY()
The strncpy() function in C++ copies a
specified bytes of characters from
source to destination.
strcat(S1, S2);
CONCATENATES(LINKS THINGS TOGETHER)
STRING S2 ONTO THE END OF STRING S1.
strlen(S1);
RETURNS THE LENGTH OF STRING S1.
String Comparison
This function compare 2 strings its returns
the ASCII difference of the first to non
matching char in both the strings.
Int strcmp(string1, string2)
If difference is equal to zero => srting1=string2
If difference is equal to +ve => srting1>string2
If difference is equal to -ve => srting1<string2
strrev(S1, S2)
Reverse the string
Without using strrev( )
To solve this problem, two standard
library functions strlen() and strcpy() are
used to calculate length and to copy string
respectively.
 The string class is a specialization of a more general template class called
basic_string (Pre-define Class)[Child class string, wstring).
 String is another container class (STL).
 To use string class, you have to include string header. - #include<string>
 Since defining a class in C++ is creating a new data type, string is derived data type.
STRING-CLASS
 Careless programmer can overrun the end of an array that holds a null terminated
string.
 For example, using strcpy()
 String class handle such issues
STRING IS SAFE THAN CHAR ARRAY
OPERATORS
WE USE ALL OPERATORS
USEFUL METHODS
assign( )
append( )
insert( )
replace( )
erase( )
find( )
rfind( )
compare( )
c_str( )
size( )
STRING CLASS FUNCTIONS
ASSIGN FUNCTION
Assigning a value to s1
APPEND TO STRING
Extends the string by appending
additional characters at the end of its
current value
INSERT INTO STRING
Inserts additional characters into the string
right before the character
Replace value in range
Assigns new_value to all the elements in the
range [first,last) that compare equal to
old_value.
ERASE CHARACTERS
FROM STRING
Erases part of the string, reducing its
length:
FIND CONTENT IN STRING
Searches the string for the first
occurrence of the sequence specified
by its arguments.
FIND LAST OCCURRENCE OF
CONTENT IN STRING
Searches the string for the last
occurrence of the sequence
specified by its arguments.
String Comparison
This function compare 2 strings its returns
the ASCII difference of the first to non
matching char in both the strings.
If difference is equal to 0 => srting1=string2
If difference is equal to +ve => srting1>string2
If difference is equal to -ve => srting1<string2
RETURN LENGTH OF STRING
Returns the length of the string, in terms
of bytes.
String in programming language in c or c++

More Related Content

What's hot (20)

Strings
StringsStrings
Strings
 
Strings
StringsStrings
Strings
 
Strings
StringsStrings
Strings
 
String C Programming
String C ProgrammingString C Programming
String C Programming
 
14 strings
14 strings14 strings
14 strings
 
Character Array and String
Character Array and StringCharacter Array and String
Character Array and String
 
C string
C stringC string
C string
 
Strings in C
Strings in CStrings in C
Strings in C
 
string in C
string in Cstring in C
string in C
 
Strings in c++
Strings in c++Strings in c++
Strings in c++
 
String in c programming
String in c programmingString in c programming
String in c programming
 
String c
String cString c
String c
 
Handling of character strings C programming
Handling of character strings C programmingHandling of character strings C programming
Handling of character strings C programming
 
Strings
StringsStrings
Strings
 
Strings
StringsStrings
Strings
 
Manipulating strings
Manipulating stringsManipulating strings
Manipulating strings
 
String (Computer programming and utilization)
String (Computer programming and utilization)String (Computer programming and utilization)
String (Computer programming and utilization)
 
strings
stringsstrings
strings
 
Operation on string presentation
Operation on string presentationOperation on string presentation
Operation on string presentation
 
C programming - String
C programming - StringC programming - String
C programming - String
 

Similar to String in programming language in c or c++

Lesson in Strings for C Programming Lessons
Lesson in Strings for C Programming LessonsLesson in Strings for C Programming Lessons
Lesson in Strings for C Programming LessonsJamesChristianGadian
 
C Programming Strings.docx
C Programming Strings.docxC Programming Strings.docx
C Programming Strings.docx8759000398
 
String & its application
String & its applicationString & its application
String & its applicationTech_MX
 
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
STRINGS IN C MRS.SOWMYA JYOTHI.pdfSTRINGS IN C MRS.SOWMYA JYOTHI.pdf
STRINGS IN C MRS.SOWMYA JYOTHI.pdfSowmyaJyothi3
 
lecture-5 string.pptx
lecture-5 string.pptxlecture-5 string.pptx
lecture-5 string.pptxDilanAlmsa
 
introduction to strings in c programming
introduction to strings in c programmingintroduction to strings in c programming
introduction to strings in c programmingmikeymanjiro2090
 
Strings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothiStrings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothiSowmya Jyothi
 
INDIAN INSTITUTE OF TECHNOLOGY KANPURESC 111M Lec13.pptx
INDIAN INSTITUTE OF TECHNOLOGY KANPURESC 111M Lec13.pptxINDIAN INSTITUTE OF TECHNOLOGY KANPURESC 111M Lec13.pptx
INDIAN INSTITUTE OF TECHNOLOGY KANPURESC 111M Lec13.pptxAbhimanyuChaure
 
C++ Strings.ppt
C++ Strings.pptC++ Strings.ppt
C++ Strings.pptDilanAlmsa
 

Similar to String in programming language in c or c++ (20)

String notes
String notesString notes
String notes
 
[ITP - Lecture 17] Strings in C/C++
[ITP - Lecture 17] Strings in C/C++[ITP - Lecture 17] Strings in C/C++
[ITP - Lecture 17] Strings in C/C++
 
Unitii string
Unitii stringUnitii string
Unitii string
 
Lesson in Strings for C Programming Lessons
Lesson in Strings for C Programming LessonsLesson in Strings for C Programming Lessons
Lesson in Strings for C Programming Lessons
 
Week6_P_String.pptx
Week6_P_String.pptxWeek6_P_String.pptx
Week6_P_String.pptx
 
C Programming Strings.docx
C Programming Strings.docxC Programming Strings.docx
C Programming Strings.docx
 
String & its application
String & its applicationString & its application
String & its application
 
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
STRINGS IN C MRS.SOWMYA JYOTHI.pdfSTRINGS IN C MRS.SOWMYA JYOTHI.pdf
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
 
M C6java7
M C6java7M C6java7
M C6java7
 
lecture-5 string.pptx
lecture-5 string.pptxlecture-5 string.pptx
lecture-5 string.pptx
 
introduction to strings in c programming
introduction to strings in c programmingintroduction to strings in c programming
introduction to strings in c programming
 
lecture5.ppt
lecture5.pptlecture5.ppt
lecture5.ppt
 
Strings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothiStrings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothi
 
COm1407: Character & Strings
COm1407: Character & StringsCOm1407: Character & Strings
COm1407: Character & Strings
 
Python data handling
Python data handlingPython data handling
Python data handling
 
INDIAN INSTITUTE OF TECHNOLOGY KANPURESC 111M Lec13.pptx
INDIAN INSTITUTE OF TECHNOLOGY KANPURESC 111M Lec13.pptxINDIAN INSTITUTE OF TECHNOLOGY KANPURESC 111M Lec13.pptx
INDIAN INSTITUTE OF TECHNOLOGY KANPURESC 111M Lec13.pptx
 
Strings part2
Strings part2Strings part2
Strings part2
 
C++ Strings.ppt
C++ Strings.pptC++ Strings.ppt
C++ Strings.ppt
 
Strings1
Strings1Strings1
Strings1
 
string
stringstring
string
 

More from Azeemaj101

Hospital Managment System Project Proposal
Hospital Managment System Project ProposalHospital Managment System Project Proposal
Hospital Managment System Project ProposalAzeemaj101
 
Registers and its type DLD
Registers and its type DLDRegisters and its type DLD
Registers and its type DLDAzeemaj101
 
Hospital Management system Final presentation
Hospital Management system Final presentationHospital Management system Final presentation
Hospital Management system Final presentationAzeemaj101
 
File Handling in Java Oop presentation
File Handling in Java Oop presentationFile Handling in Java Oop presentation
File Handling in Java Oop presentationAzeemaj101
 
Sampling Statistic presentation
Sampling Statistic presentationSampling Statistic presentation
Sampling Statistic presentationAzeemaj101
 
Hospital Management System Documentation Java
Hospital Management System Documentation Java Hospital Management System Documentation Java
Hospital Management System Documentation Java Azeemaj101
 
Essay and its Type
Essay and its TypeEssay and its Type
Essay and its TypeAzeemaj101
 
Operating System (OS)
Operating System (OS)Operating System (OS)
Operating System (OS)Azeemaj101
 

More from Azeemaj101 (8)

Hospital Managment System Project Proposal
Hospital Managment System Project ProposalHospital Managment System Project Proposal
Hospital Managment System Project Proposal
 
Registers and its type DLD
Registers and its type DLDRegisters and its type DLD
Registers and its type DLD
 
Hospital Management system Final presentation
Hospital Management system Final presentationHospital Management system Final presentation
Hospital Management system Final presentation
 
File Handling in Java Oop presentation
File Handling in Java Oop presentationFile Handling in Java Oop presentation
File Handling in Java Oop presentation
 
Sampling Statistic presentation
Sampling Statistic presentationSampling Statistic presentation
Sampling Statistic presentation
 
Hospital Management System Documentation Java
Hospital Management System Documentation Java Hospital Management System Documentation Java
Hospital Management System Documentation Java
 
Essay and its Type
Essay and its TypeEssay and its Type
Essay and its Type
 
Operating System (OS)
Operating System (OS)Operating System (OS)
Operating System (OS)
 

Recently uploaded

Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIShubhangi Sonawane
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfChris Hunter
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 

Recently uploaded (20)

Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 

String in programming language in c or c++

  • 2.
  • 3. STRING <BSCS-G-SECTION> Presented by  Muhammad Azeem  Muhammad Maaz Usman  Osman Asgher  Umer Farooq TEACHER: MA’AM SARAH
  • 4. Generally speaking, a string is a sequence of characters • Examples: “hello”, “high school”, “H2O”. DEFINITION OF STRINGS “Hello" “High school"
  • 5. DECLARATION OF STRINGS The following instructions are all equivalent. They declare x to be an object of type string, and assign the string “high school” to it: string x(“high school”);
  • 6. DECLARATION OF STRINGS string x= “high school”;
  • 7. DECLARATION OF STRINGS string x; x=“high school”;
  • 8. TYPE OF STRING C++ provides following two types of string representations,  The C-style character string (#include<string.h> Or #include<cstring>).  The string class type introduced with Standard C++ (#include<string>).
  • 9.  When we use C-string Functions so, we use this library. (#include<string.h> Or #include<cstring>)  When we use String Class Functions so, we use this library. (#include<string>) LIBRARY FOR C-STRING & STRING CLASS
  • 10. C-STRING  Strings are arrays of chars. String literals are words surrounded by double quotation marks.  The string in C programming language is actually a one-dimensional array of characters which is terminated by a null character ‘0’. Thus a null-terminated string contains the characters that comprise the string followed by a null.  A string can be declared as a character array or with a string pointer.  The following declaration and initialization create a string consisting of the word "Hello". To hold the null character at the end of the array, the size of the character array containing the string is one more than the number of characters in the word "Hello”. OR "This is a static string" char aj[6]={‘H’,’E’,’L’,’L’,’O’,’0’} char aj[ ]={“HELLO”} char *aj={“HELLO”}
  • 11.  Following is the memory presentation of above defined string in C/C++:  It's important to remember that there will be an extra character on the end on a string, literally a '0' character, just like there is always a period at the end of a sentence. Since this string terminator is unprintable, it is not counted as a letter, but it still takes up a space. Technically, in a fifty char array you could only hold 49 letters and one null character at the end to terminate the string.  Actually, you do not place the null character at the end of a string constant. The C compiler automatically places the '0' at the end of the string when it initializes the array. STRING MEMORY INDEX 0 1 2 3 4 5 VARIABLE ‘H’ ‘A’ ‘L’ ‘L’ ‘O’ ‘0’ ADDRESS 0*23451 0*23452 0*23453 0*23454 0*23455 0*23456
  • 12. Five char array you could only hold 4 letters and one null character at the end to terminate the string.
  • 13. Six char array you could only hold 6 letters and one null character at the end to terminate the string.
  • 14. C++ OPERATORS ARE NOT USE IN C-STRING
  • 15.  C++ Operator can’t be applied to them: OPERATORS
  • 16. Sr. No Function & Purpose strcpy(s1, s2); Copies string s2 into string s1. strcat(s1, s2); Concatenates string s2 onto the end of string s1. strlen(s1); Returns the length of string s1. strcmp(s1, s2); Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2. C++ strncpy() The strncpy() function in C++ copies a specified bytes of characters from source to destination. strrev(s1, s2) Reverse the string C-STRING FUNCTIONS
  • 17. strcpy(S1, S2); COPIES STRING S2 INTO STRING S1.
  • 18. C++ STRNCPY() The strncpy() function in C++ copies a specified bytes of characters from source to destination.
  • 19. strcat(S1, S2); CONCATENATES(LINKS THINGS TOGETHER) STRING S2 ONTO THE END OF STRING S1.
  • 21. String Comparison This function compare 2 strings its returns the ASCII difference of the first to non matching char in both the strings. Int strcmp(string1, string2) If difference is equal to zero => srting1=string2 If difference is equal to +ve => srting1>string2 If difference is equal to -ve => srting1<string2
  • 23. Without using strrev( ) To solve this problem, two standard library functions strlen() and strcpy() are used to calculate length and to copy string respectively.
  • 24.  The string class is a specialization of a more general template class called basic_string (Pre-define Class)[Child class string, wstring).  String is another container class (STL).  To use string class, you have to include string header. - #include<string>  Since defining a class in C++ is creating a new data type, string is derived data type. STRING-CLASS
  • 25.  Careless programmer can overrun the end of an array that holds a null terminated string.  For example, using strcpy()  String class handle such issues STRING IS SAFE THAN CHAR ARRAY
  • 26. OPERATORS WE USE ALL OPERATORS
  • 27. USEFUL METHODS assign( ) append( ) insert( ) replace( ) erase( ) find( ) rfind( ) compare( ) c_str( ) size( ) STRING CLASS FUNCTIONS
  • 29. APPEND TO STRING Extends the string by appending additional characters at the end of its current value
  • 30. INSERT INTO STRING Inserts additional characters into the string right before the character
  • 31. Replace value in range Assigns new_value to all the elements in the range [first,last) that compare equal to old_value.
  • 32. ERASE CHARACTERS FROM STRING Erases part of the string, reducing its length:
  • 33. FIND CONTENT IN STRING Searches the string for the first occurrence of the sequence specified by its arguments.
  • 34. FIND LAST OCCURRENCE OF CONTENT IN STRING Searches the string for the last occurrence of the sequence specified by its arguments.
  • 35. String Comparison This function compare 2 strings its returns the ASCII difference of the first to non matching char in both the strings. If difference is equal to 0 => srting1=string2 If difference is equal to +ve => srting1>string2 If difference is equal to -ve => srting1<string2
  • 36. RETURN LENGTH OF STRING Returns the length of the string, in terms of bytes.

Editor's Notes

  1. Delete this slide when you finish preparing the other slides.