SlideShare ist ein Scribd-Unternehmen logo
1 von 16
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

Weitere ähnliche Inhalte

Ähnlich wie Lec 16. Strings

Lec 08. C Data Types
Lec 08. C Data TypesLec 08. C Data Types
Lec 08. C Data Types
Rushdi Shams
 
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
Rushdi Shams
 
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
 
Lec 15. Pointers and Arrays
Lec 15. Pointers and ArraysLec 15. Pointers and Arrays
Lec 15. Pointers and Arrays
Rushdi Shams
 

Ähnlich wie 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
 

Mehr von Rushdi Shams

Types of machine translation
Types of machine translationTypes of machine translation
Types of machine translation
Rushdi 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 translation
Rushdi Shams
 
Syntax and semantics
Syntax and semanticsSyntax and semantics
Syntax and semantics
Rushdi Shams
 
Propositional logic
Propositional logicPropositional logic
Propositional logic
Rushdi Shams
 
Probabilistic logic
Probabilistic logicProbabilistic logic
Probabilistic logic
Rushdi Shams
 
Knowledge structure
Knowledge structureKnowledge structure
Knowledge structure
Rushdi Shams
 
Knowledge representation
Knowledge representationKnowledge representation
Knowledge representation
Rushdi Shams
 
L5 understanding hacking
L5  understanding hackingL5  understanding hacking
L5 understanding hacking
Rushdi Shams
 
L2 Intrusion Detection System (IDS)
L2  Intrusion Detection System (IDS)L2  Intrusion Detection System (IDS)
L2 Intrusion Detection System (IDS)
Rushdi Shams
 

Mehr von 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
 

Kürzlich hochgeladen

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
negromaestrong
 
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
QucHHunhnh
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 

Kürzlich hochgeladen (20)

microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
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
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
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
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
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
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.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...
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
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
 
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
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 

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