SlideShare a Scribd company logo
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

String C Programming
String C ProgrammingString C Programming
String C Programming
Prionto Abdullah
 
Character Array and String
Character Array and StringCharacter Array and String
Character Array and String
Tasnima Hamid
 
C string
C stringC string
Strings in C
Strings in CStrings in C
Strings in C
Kamal Acharya
 
string in C
string in Cstring in C
String in c programming
String in c programmingString in c programming
String in c programming
Devan Thakur
 
String c
String cString c
Handling of character strings C programming
Handling of character strings C programmingHandling of character strings C programming
Handling of character strings C programming
Appili Vamsi Krishna
 
Strings
StringsStrings
Strings
Nilesh Dalvi
 
Strings
StringsStrings
Strings
Dhiviya Rose
 
Manipulating strings
Manipulating stringsManipulating strings
Manipulating strings
Jancypriya M
 
String (Computer programming and utilization)
String (Computer programming and utilization)String (Computer programming and utilization)
String (Computer programming and utilization)
Digvijaysinh Gohil
 
strings
stringsstrings
strings
teach4uin
 
Operation on string presentation
Operation on string presentationOperation on string presentation
Operation on string presentation
Aliul Kadir Akib
 
C programming - String
C programming - StringC programming - String
C programming - String
Achyut Devkota
 

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++

String notes
String notesString notes
String notes
Prasadu Peddi
 
[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++
Muhammad Hammad Waseem
 
Unitii string
Unitii stringUnitii string
Unitii string
Sowri Rajan
 
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
JamesChristianGadian
 
Week6_P_String.pptx
Week6_P_String.pptxWeek6_P_String.pptx
Week6_P_String.pptx
OluwafolakeOjo
 
C Programming Strings.docx
C Programming Strings.docxC Programming Strings.docx
C Programming Strings.docx
8759000398
 
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.pdf
SowmyaJyothi3
 
lecture-5 string.pptx
lecture-5 string.pptxlecture-5 string.pptx
lecture-5 string.pptx
DilanAlmsa
 
introduction to strings in c programming
introduction to strings in c programmingintroduction to strings in c programming
introduction to strings in c programming
mikeymanjiro2090
 
lecture5.ppt
lecture5.pptlecture5.ppt
lecture5.ppt
KarthiKeyan462713
 
Strings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothiStrings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothi
Sowmya Jyothi
 
COm1407: Character & Strings
COm1407: Character & StringsCOm1407: Character & Strings
COm1407: Character & Strings
Hemantha Kulathilake
 
Python data handling
Python data handlingPython data handling
Python data handling
Prof. Dr. K. Adisesha
 
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
AbhimanyuChaure
 
Strings part2
Strings part2Strings part2
Strings part2
yndaravind
 
C++ Strings.ppt
C++ Strings.pptC++ Strings.ppt
C++ Strings.ppt
DilanAlmsa
 

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 Proposal
Azeemaj101
 
Registers and its type DLD
Registers and its type DLDRegisters and its type DLD
Registers and its type DLD
Azeemaj101
 
Hospital Management system Final presentation
Hospital Management system Final presentationHospital Management system Final presentation
Hospital Management system Final presentation
Azeemaj101
 
File Handling in Java Oop presentation
File Handling in Java Oop presentationFile Handling in Java Oop presentation
File Handling in Java Oop presentation
Azeemaj101
 
Sampling Statistic presentation
Sampling Statistic presentationSampling Statistic presentation
Sampling Statistic presentation
Azeemaj101
 
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 Type
Azeemaj101
 
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

How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
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
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
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
 
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
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
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
 
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
 
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
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
chanes7
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
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
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
Mohammed Sikander
 

Recently uploaded (20)

How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
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...
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
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
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
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
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
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
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Digital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion DesignsDigital Artifact 2 - Investigating Pavilion Designs
Digital Artifact 2 - Investigating Pavilion Designs
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
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
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
 

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.