SlideShare a Scribd company logo
Lecture 16Lecture 16
Version 1.0Version 1.0
StringsStrings
2Rushdi Shams, Dept of CSE, KUET, Bangladesh
What is StringWhat is String
 a group of integers can be stored in an integera group of integers can be stored in an integer
arrayarray
 similarly a group of characters can be stored in asimilarly a group of characters can be stored in a
character arraycharacter array
 Character arrays are many a time also calledCharacter arrays are many a time also called
stringsstrings
 Character arrays or strings are used byCharacter arrays or strings are used by
programming languages to manipulate text suchprogramming languages to manipulate text such
as words and sentencesas words and sentences
3Rushdi Shams, Dept of CSE, KUET, Bangladesh
What is StringWhat is String
 A string constant is a one-dimensional array ofA string constant is a one-dimensional array of
characters terminated by a null ( ‘0’ )characters terminated by a null ( ‘0’ )
char name[ ] = { 'H', 'A', 'E', 'S', 'L', 'E', 'R', '0' } ;char name[ ] = { 'H', 'A', 'E', 'S', 'L', 'E', 'R', '0' } ;
 Each character in the array occupies one byte ofEach character in the array occupies one byte of
memory and the last character is always ‘0’memory and the last character is always ‘0’
4Rushdi Shams, Dept of CSE, KUET, Bangladesh
What is StringWhat is String
 Note that ‘0’ and ‘0’ are not same. ASCII value of ‘0’ isNote that ‘0’ and ‘0’ are not same. ASCII value of ‘0’ is
0, whereas ASCII value of ‘0’ is 480, whereas ASCII value of ‘0’ is 48
 a string not terminated by a ‘0’ is not really a string, buta string not terminated by a ‘0’ is not really a string, but
merely a collection of charactersmerely a collection of characters
5Rushdi Shams, Dept of CSE, KUET, Bangladesh
What is StringWhat is String
 the string used above can also be initialized as,the string used above can also be initialized as,
char name[ ] = "HAESLER" ;char name[ ] = "HAESLER" ;
 Note that, in this declaration ‘0’ is notNote that, in this declaration ‘0’ is not
necessary. C inserts the null characternecessary. C inserts the null character
automatically.automatically.
6Rushdi Shams, Dept of CSE, KUET, Bangladesh
ExampleExample
7Rushdi Shams, Dept of CSE, KUET, Bangladesh
ExampleExample
8Rushdi Shams, Dept of CSE, KUET, Bangladesh
See the difference?See the difference?
 This program doesn’t rely on the length of theThis program doesn’t rely on the length of the
string (number of characters in it) to print out itsstring (number of characters in it) to print out its
contents and hence is definitely more generalcontents and hence is definitely more general
than the earlier one.than the earlier one.
9Rushdi Shams, Dept of CSE, KUET, Bangladesh
ExampleExample
10Rushdi Shams, Dept of CSE, KUET, Bangladesh
And the most simple oneAnd the most simple one
11Rushdi Shams, Dept of CSE, KUET, Bangladesh
 TheThe %s%s used inused in printf( )printf( ) is a formatis a format
specification for printing out a stringspecification for printing out a string
 The same specification can be used to receive aThe same specification can be used to receive a
string from the keyboardstring from the keyboard
12Rushdi Shams, Dept of CSE, KUET, Bangladesh
13Rushdi Shams, Dept of CSE, KUET, Bangladesh
String through scanf ( )String through scanf ( )
 While entering the string usingWhile entering the string using scanf( )scanf( ) wewe
must be cautious about two thingsmust be cautious about two things
1.1. The length of the string should not exceed theThe length of the string should not exceed the
dimension of the character arraydimension of the character array
2.2. scanf( )scanf( ) is not capable of receiving multi-wordis not capable of receiving multi-word
strings. Therefore names such as ‘Debashishstrings. Therefore names such as ‘Debashish
Roy’ would be unacceptableRoy’ would be unacceptable
14Rushdi Shams, Dept of CSE, KUET, Bangladesh
The SolutionThe Solution
15Rushdi Shams, Dept of CSE, KUET, Bangladesh
Standard Library String FunctionsStandard Library String Functions
 we shall discuss the functionswe shall discuss the functions strlen( )strlen( ),,
strcpy( )strcpy( ),, strcat( )strcat( ) andand strcmp( )strcmp( ), since these, since these
are the most commonly used functionsare the most commonly used functions
16Rushdi Shams, Dept of CSE, KUET, Bangladesh
Standard Library String FunctionsStandard Library String Functions

More Related Content

Similar to Lec 16. Strings

Strings(2007)
Strings(2007)Strings(2007)
Strings(2007)
svit vasad
 
COm1407: Character & Strings
COm1407: Character & StringsCOm1407: Character & Strings
COm1407: Character & Strings
Hemantha Kulathilake
 
Lec 08. C Data Types
Lec 08. C Data TypesLec 08. C Data Types
Lec 08. C Data TypesRushdi Shams
 
Strings in c
Strings in cStrings in c
Strings in c
vampugani
 
Lec 12. Multidimensional Arrays / Passing Arrays to Functions
Lec 12. Multidimensional Arrays / Passing Arrays to FunctionsLec 12. Multidimensional Arrays / Passing Arrays to Functions
Lec 12. Multidimensional Arrays / Passing Arrays to FunctionsRushdi Shams
 
String interpolation
String interpolationString interpolation
String interpolation
Knoldus Inc.
 
Lecture 17 - Strings
Lecture 17 - StringsLecture 17 - Strings
Lecture 17 - Strings
Md. Imran Hossain Showrov
 
introduction to strings in c programming
introduction to strings in c programmingintroduction to strings in c programming
introduction to strings in c programming
mikeymanjiro2090
 
3 character strings and formatted input output
3  character strings and formatted input output3  character strings and formatted input output
3 character strings and formatted input output
MomenMostafa
 
String notes
String notesString notes
String notes
Prasadu Peddi
 
String handling(string class)
String handling(string class)String handling(string class)
String handling(string class)Ravi_Kant_Sahu
 
String handling(string class)
String handling(string class)String handling(string class)
String handling(string class)Ravi Kant Sahu
 
Strings IN C
Strings IN CStrings IN C
Strings IN C
yndaravind
 
Lec 15. Pointers and Arrays
Lec 15. Pointers and ArraysLec 15. Pointers and Arrays
Lec 15. Pointers and ArraysRushdi Shams
 
String in c
String in cString in c
String in c
Suneel Dogra
 
Strings-Computer programming
Strings-Computer programmingStrings-Computer programming
Strings-Computer programming
nmahi96
 
Regex lecture
Regex lectureRegex lecture
Regex lecture
Jun Shimizu
 
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdfFUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
Bryan Alejos
 

Similar to Lec 16. Strings (20)

Strings(2007)
Strings(2007)Strings(2007)
Strings(2007)
 
COm1407: Character & Strings
COm1407: Character & StringsCOm1407: Character & Strings
COm1407: Character & Strings
 
Lec 08. C Data Types
Lec 08. C Data TypesLec 08. C Data Types
Lec 08. C Data Types
 
Strings in c
Strings in cStrings in c
Strings in c
 
Lec 12. Multidimensional Arrays / Passing Arrays to Functions
Lec 12. Multidimensional Arrays / Passing Arrays to FunctionsLec 12. Multidimensional Arrays / Passing Arrays to Functions
Lec 12. Multidimensional Arrays / Passing Arrays to Functions
 
Ch09
Ch09Ch09
Ch09
 
String interpolation
String interpolationString interpolation
String interpolation
 
Lecture 17 - Strings
Lecture 17 - StringsLecture 17 - Strings
Lecture 17 - Strings
 
introduction to strings in c programming
introduction to strings in c programmingintroduction to strings in c programming
introduction to strings in c programming
 
M C6java7
M C6java7M C6java7
M C6java7
 
3 character strings and formatted input output
3  character strings and formatted input output3  character strings and formatted input output
3 character strings and formatted input output
 
String notes
String notesString notes
String notes
 
String handling(string class)
String handling(string class)String handling(string class)
String handling(string class)
 
String handling(string class)
String handling(string class)String handling(string class)
String handling(string class)
 
Strings IN C
Strings IN CStrings IN C
Strings IN C
 
Lec 15. Pointers and Arrays
Lec 15. Pointers and ArraysLec 15. Pointers and Arrays
Lec 15. Pointers and Arrays
 
String in c
String in cString in c
String in c
 
Strings-Computer programming
Strings-Computer programmingStrings-Computer programming
Strings-Computer programming
 
Regex lecture
Regex lectureRegex lecture
Regex lecture
 
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdfFUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
 

More from Rushdi Shams

Research Methodology and Tips on Better Research
Research Methodology and Tips on Better ResearchResearch Methodology and Tips on Better Research
Research Methodology and Tips on Better Research
Rushdi Shams
 
Common evaluation measures in NLP and IR
Common evaluation measures in NLP and IRCommon evaluation measures in NLP and IR
Common evaluation measures in NLP and IR
Rushdi Shams
 
Machine learning with nlp 101
Machine learning with nlp 101Machine learning with nlp 101
Machine learning with nlp 101
Rushdi Shams
 
Semi-supervised classification for natural language processing
Semi-supervised classification for natural language processingSemi-supervised classification for natural language processing
Semi-supervised classification for natural language processing
Rushdi Shams
 
Natural Language Processing: Parsing
Natural Language Processing: ParsingNatural Language Processing: Parsing
Natural Language Processing: Parsing
Rushdi Shams
 
Types of machine translation
Types of machine translationTypes of machine translation
Types of machine translationRushdi Shams
 
L1 l2 l3 introduction to machine translation
L1 l2 l3  introduction to machine translationL1 l2 l3  introduction to machine translation
L1 l2 l3 introduction to machine translationRushdi Shams
 
Syntax and semantics
Syntax and semanticsSyntax and semantics
Syntax and semanticsRushdi Shams
 
Propositional logic
Propositional logicPropositional logic
Propositional logicRushdi Shams
 
Probabilistic logic
Probabilistic logicProbabilistic logic
Probabilistic logicRushdi Shams
 
Knowledge structure
Knowledge structureKnowledge structure
Knowledge structureRushdi Shams
 
Knowledge representation
Knowledge representationKnowledge representation
Knowledge representationRushdi Shams
 
L5 understanding hacking
L5  understanding hackingL5  understanding hacking
L5 understanding hackingRushdi Shams
 
L2 Intrusion Detection System (IDS)
L2  Intrusion Detection System (IDS)L2  Intrusion Detection System (IDS)
L2 Intrusion Detection System (IDS)Rushdi Shams
 

More from Rushdi Shams (20)

Research Methodology and Tips on Better Research
Research Methodology and Tips on Better ResearchResearch Methodology and Tips on Better Research
Research Methodology and Tips on Better Research
 
Common evaluation measures in NLP and IR
Common evaluation measures in NLP and IRCommon evaluation measures in NLP and IR
Common evaluation measures in NLP and IR
 
Machine learning with nlp 101
Machine learning with nlp 101Machine learning with nlp 101
Machine learning with nlp 101
 
Semi-supervised classification for natural language processing
Semi-supervised classification for natural language processingSemi-supervised classification for natural language processing
Semi-supervised classification for natural language processing
 
Natural Language Processing: Parsing
Natural Language Processing: ParsingNatural Language Processing: Parsing
Natural Language Processing: Parsing
 
Types of machine translation
Types of machine translationTypes of machine translation
Types of machine translation
 
L1 l2 l3 introduction to machine translation
L1 l2 l3  introduction to machine translationL1 l2 l3  introduction to machine translation
L1 l2 l3 introduction to machine translation
 
Syntax and semantics
Syntax and semanticsSyntax and semantics
Syntax and semantics
 
Propositional logic
Propositional logicPropositional logic
Propositional logic
 
Probabilistic logic
Probabilistic logicProbabilistic logic
Probabilistic logic
 
L15 fuzzy logic
L15  fuzzy logicL15  fuzzy logic
L15 fuzzy logic
 
Knowledge structure
Knowledge structureKnowledge structure
Knowledge structure
 
Knowledge representation
Knowledge representationKnowledge representation
Knowledge representation
 
First order logic
First order logicFirst order logic
First order logic
 
Belief function
Belief functionBelief function
Belief function
 
L5 understanding hacking
L5  understanding hackingL5  understanding hacking
L5 understanding hacking
 
L4 vpn
L4  vpnL4  vpn
L4 vpn
 
L3 defense
L3  defenseL3  defense
L3 defense
 
L2 Intrusion Detection System (IDS)
L2  Intrusion Detection System (IDS)L2  Intrusion Detection System (IDS)
L2 Intrusion Detection System (IDS)
 
L1 phishing
L1  phishingL1  phishing
L1 phishing
 

Recently uploaded

Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
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
 
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
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 
The Diamond Necklace by Guy De Maupassant.pptx
The Diamond Necklace by Guy De Maupassant.pptxThe Diamond Necklace by Guy De Maupassant.pptx
The Diamond Necklace by Guy De Maupassant.pptx
DhatriParmar
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
deeptiverma2406
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
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
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
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
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdfMASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
goswamiyash170123
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 

Recently uploaded (20)

Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
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
 
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
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 
The Diamond Necklace by Guy De Maupassant.pptx
The Diamond Necklace by Guy De Maupassant.pptxThe Diamond Necklace by Guy De Maupassant.pptx
The Diamond Necklace by Guy De Maupassant.pptx
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
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
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
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...
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdfMASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
MASS MEDIA STUDIES-835-CLASS XI Resource Material.pdf
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 

Lec 16. Strings

  • 1. Lecture 16Lecture 16 Version 1.0Version 1.0 StringsStrings
  • 2. 2Rushdi Shams, Dept of CSE, KUET, Bangladesh What is StringWhat is String  a group of integers can be stored in an integera group of integers can be stored in an integer arrayarray  similarly a group of characters can be stored in asimilarly a group of characters can be stored in a character arraycharacter array  Character arrays are many a time also calledCharacter arrays are many a time also called stringsstrings  Character arrays or strings are used byCharacter arrays or strings are used by programming languages to manipulate text suchprogramming languages to manipulate text such as words and sentencesas words and sentences
  • 3. 3Rushdi Shams, Dept of CSE, KUET, Bangladesh What is StringWhat is String  A string constant is a one-dimensional array ofA string constant is a one-dimensional array of characters terminated by a null ( ‘0’ )characters terminated by a null ( ‘0’ ) char name[ ] = { 'H', 'A', 'E', 'S', 'L', 'E', 'R', '0' } ;char name[ ] = { 'H', 'A', 'E', 'S', 'L', 'E', 'R', '0' } ;  Each character in the array occupies one byte ofEach character in the array occupies one byte of memory and the last character is always ‘0’memory and the last character is always ‘0’
  • 4. 4Rushdi Shams, Dept of CSE, KUET, Bangladesh What is StringWhat is String  Note that ‘0’ and ‘0’ are not same. ASCII value of ‘0’ isNote that ‘0’ and ‘0’ are not same. ASCII value of ‘0’ is 0, whereas ASCII value of ‘0’ is 480, whereas ASCII value of ‘0’ is 48  a string not terminated by a ‘0’ is not really a string, buta string not terminated by a ‘0’ is not really a string, but merely a collection of charactersmerely a collection of characters
  • 5. 5Rushdi Shams, Dept of CSE, KUET, Bangladesh What is StringWhat is String  the string used above can also be initialized as,the string used above can also be initialized as, char name[ ] = "HAESLER" ;char name[ ] = "HAESLER" ;  Note that, in this declaration ‘0’ is notNote that, in this declaration ‘0’ is not necessary. C inserts the null characternecessary. C inserts the null character automatically.automatically.
  • 6. 6Rushdi Shams, Dept of CSE, KUET, Bangladesh ExampleExample
  • 7. 7Rushdi Shams, Dept of CSE, KUET, Bangladesh ExampleExample
  • 8. 8Rushdi Shams, Dept of CSE, KUET, Bangladesh See the difference?See the difference?  This program doesn’t rely on the length of theThis program doesn’t rely on the length of the string (number of characters in it) to print out itsstring (number of characters in it) to print out its contents and hence is definitely more generalcontents and hence is definitely more general than the earlier one.than the earlier one.
  • 9. 9Rushdi Shams, Dept of CSE, KUET, Bangladesh ExampleExample
  • 10. 10Rushdi Shams, Dept of CSE, KUET, Bangladesh And the most simple oneAnd the most simple one
  • 11. 11Rushdi Shams, Dept of CSE, KUET, Bangladesh  TheThe %s%s used inused in printf( )printf( ) is a formatis a format specification for printing out a stringspecification for printing out a string  The same specification can be used to receive aThe same specification can be used to receive a string from the keyboardstring from the keyboard
  • 12. 12Rushdi Shams, Dept of CSE, KUET, Bangladesh
  • 13. 13Rushdi Shams, Dept of CSE, KUET, Bangladesh String through scanf ( )String through scanf ( )  While entering the string usingWhile entering the string using scanf( )scanf( ) wewe must be cautious about two thingsmust be cautious about two things 1.1. The length of the string should not exceed theThe length of the string should not exceed the dimension of the character arraydimension of the character array 2.2. scanf( )scanf( ) is not capable of receiving multi-wordis not capable of receiving multi-word strings. Therefore names such as ‘Debashishstrings. Therefore names such as ‘Debashish Roy’ would be unacceptableRoy’ would be unacceptable
  • 14. 14Rushdi Shams, Dept of CSE, KUET, Bangladesh The SolutionThe Solution
  • 15. 15Rushdi Shams, Dept of CSE, KUET, Bangladesh Standard Library String FunctionsStandard Library String Functions  we shall discuss the functionswe shall discuss the functions strlen( )strlen( ),, strcpy( )strcpy( ),, strcat( )strcat( ) andand strcmp( )strcmp( ), since these, since these are the most commonly used functionsare the most commonly used functions
  • 16. 16Rushdi Shams, Dept of CSE, KUET, Bangladesh Standard Library String FunctionsStandard Library String Functions