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

PPT
Lec 11. One Dimensional Arrays
Rushdi Shams
 
PDF
Source Code Quality
Alberto Simões
 
PPT
Java căn bản - Chapter9
Vince Vo
 
PPT
125 5.3
Jeneva Clark
 
PPTX
7 tips for smart communicators
Aurelie Couvreur
 
DOCX
CSE 220 Assignment 3 Hints and Tips Some hints for approa.docx
faithxdunce63732
 
PDF
[ITP - Lecture 17] Strings in C/C++
Muhammad Hammad Waseem
 
PPT
Chapter 9 - Characters and Strings
Eduardo Bergavera
 
Lec 11. One Dimensional Arrays
Rushdi Shams
 
Source Code Quality
Alberto Simões
 
Java căn bản - Chapter9
Vince Vo
 
125 5.3
Jeneva Clark
 
7 tips for smart communicators
Aurelie Couvreur
 
CSE 220 Assignment 3 Hints and Tips Some hints for approa.docx
faithxdunce63732
 
[ITP - Lecture 17] Strings in C/C++
Muhammad Hammad Waseem
 
Chapter 9 - Characters and Strings
Eduardo Bergavera
 

Similar to Lec 16. Strings (20)

PPT
Strings(2007)
svit vasad
 
PPTX
COm1407: Character & Strings
Hemantha Kulathilake
 
PPT
Lec 08. C Data Types
Rushdi Shams
 
PPT
Strings in c
vampugani
 
PPT
Lec 12. Multidimensional Arrays / Passing Arrays to Functions
Rushdi Shams
 
ODP
String interpolation
Knoldus Inc.
 
PPT
Lecture 17 - Strings
Md. Imran Hossain Showrov
 
PPTX
introduction to strings in c programming
mikeymanjiro2090
 
PPT
M C6java7
mbruggen
 
PDF
3 character strings and formatted input output
MomenMostafa
 
PDF
String notes
Prasadu Peddi
 
PDF
String handling(string class)
Ravi Kant Sahu
 
PDF
String handling(string class)
Ravi_Kant_Sahu
 
PPT
Cfbcgdhfghdfhghggfhghghgfhgfhgfhhapter11.PPT
JITENDER773791
 
PPT
Chapterabcdefghijklmnopqrdstuvwxydanniipo
abritip
 
PDF
Strings IN C
yndaravind
 
PPT
Lec 15. Pointers and Arrays
Rushdi Shams
 
PPT
THE FORMAT AND USAGE OF STRINGS IN C.PPT
shanthabalaji2013
 
DOC
String in c
Suneel Dogra
 
Strings(2007)
svit vasad
 
COm1407: Character & Strings
Hemantha Kulathilake
 
Lec 08. C Data Types
Rushdi Shams
 
Strings in c
vampugani
 
Lec 12. Multidimensional Arrays / Passing Arrays to Functions
Rushdi Shams
 
String interpolation
Knoldus Inc.
 
Lecture 17 - Strings
Md. Imran Hossain Showrov
 
introduction to strings in c programming
mikeymanjiro2090
 
M C6java7
mbruggen
 
3 character strings and formatted input output
MomenMostafa
 
String notes
Prasadu Peddi
 
String handling(string class)
Ravi Kant Sahu
 
String handling(string class)
Ravi_Kant_Sahu
 
Cfbcgdhfghdfhghggfhghghgfhgfhgfhhapter11.PPT
JITENDER773791
 
Chapterabcdefghijklmnopqrdstuvwxydanniipo
abritip
 
Strings IN C
yndaravind
 
Lec 15. Pointers and Arrays
Rushdi Shams
 
THE FORMAT AND USAGE OF STRINGS IN C.PPT
shanthabalaji2013
 
String in c
Suneel Dogra
 
Ad

More from Rushdi Shams (20)

PDF
Research Methodology and Tips on Better Research
Rushdi Shams
 
PPTX
Common evaluation measures in NLP and IR
Rushdi Shams
 
PPTX
Machine learning with nlp 101
Rushdi Shams
 
PPTX
Semi-supervised classification for natural language processing
Rushdi Shams
 
PPTX
Natural Language Processing: Parsing
Rushdi Shams
 
PPT
Types of machine translation
Rushdi Shams
 
PDF
L1 l2 l3 introduction to machine translation
Rushdi Shams
 
PPT
Syntax and semantics
Rushdi Shams
 
PPTX
Propositional logic
Rushdi Shams
 
PPTX
Probabilistic logic
Rushdi Shams
 
PPT
L15 fuzzy logic
Rushdi Shams
 
PPT
Knowledge structure
Rushdi Shams
 
PPT
Knowledge representation
Rushdi Shams
 
PPT
First order logic
Rushdi Shams
 
PPTX
Belief function
Rushdi Shams
 
PPT
L5 understanding hacking
Rushdi Shams
 
PPT
L4 vpn
Rushdi Shams
 
PPT
L3 defense
Rushdi Shams
 
PPT
L2 Intrusion Detection System (IDS)
Rushdi Shams
 
PPT
L1 phishing
Rushdi Shams
 
Research Methodology and Tips on Better Research
Rushdi Shams
 
Common evaluation measures in NLP and IR
Rushdi Shams
 
Machine learning with nlp 101
Rushdi Shams
 
Semi-supervised classification for natural language processing
Rushdi Shams
 
Natural Language Processing: Parsing
Rushdi Shams
 
Types of machine translation
Rushdi Shams
 
L1 l2 l3 introduction to machine translation
Rushdi Shams
 
Syntax and semantics
Rushdi Shams
 
Propositional logic
Rushdi Shams
 
Probabilistic logic
Rushdi Shams
 
L15 fuzzy logic
Rushdi Shams
 
Knowledge structure
Rushdi Shams
 
Knowledge representation
Rushdi Shams
 
First order logic
Rushdi Shams
 
Belief function
Rushdi Shams
 
L5 understanding hacking
Rushdi Shams
 
L4 vpn
Rushdi Shams
 
L3 defense
Rushdi Shams
 
L2 Intrusion Detection System (IDS)
Rushdi Shams
 
L1 phishing
Rushdi Shams
 
Ad

Recently uploaded (20)

PDF
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
PPTX
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
PPTX
HEALTH CARE DELIVERY SYSTEM - UNIT 2 - GNM 3RD YEAR.pptx
Priyanshu Anand
 
PDF
Biological Classification Class 11th NCERT CBSE NEET.pdf
NehaRohtagi1
 
PDF
RA 12028_ARAL_Orientation_Day-2-Sessions_v2.pdf
Seven De Los Reyes
 
PPTX
TEF & EA Bsc Nursing 5th sem.....BBBpptx
AneetaSharma15
 
PPTX
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
PPTX
BASICS IN COMPUTER APPLICATIONS - UNIT I
suganthim28
 
PPTX
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
PPTX
Artificial Intelligence in Gastroentrology: Advancements and Future Presprec...
AyanHossain
 
PPTX
Kanban Cards _ Mass Action in Odoo 18.2 - Odoo Slides
Celine George
 
PPTX
How to Apply for a Job From Odoo 18 Website
Celine George
 
PPTX
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
PPTX
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
DOCX
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
PPTX
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
PDF
2.Reshaping-Indias-Political-Map.ppt/pdf/8th class social science Exploring S...
Sandeep Swamy
 
PPTX
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
 
PPTX
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
PPTX
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
HEALTH CARE DELIVERY SYSTEM - UNIT 2 - GNM 3RD YEAR.pptx
Priyanshu Anand
 
Biological Classification Class 11th NCERT CBSE NEET.pdf
NehaRohtagi1
 
RA 12028_ARAL_Orientation_Day-2-Sessions_v2.pdf
Seven De Los Reyes
 
TEF & EA Bsc Nursing 5th sem.....BBBpptx
AneetaSharma15
 
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
BASICS IN COMPUTER APPLICATIONS - UNIT I
suganthim28
 
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
Artificial Intelligence in Gastroentrology: Advancements and Future Presprec...
AyanHossain
 
Kanban Cards _ Mass Action in Odoo 18.2 - Odoo Slides
Celine George
 
How to Apply for a Job From Odoo 18 Website
Celine George
 
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
2.Reshaping-Indias-Political-Map.ppt/pdf/8th class social science Exploring S...
Sandeep Swamy
 
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
 
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 

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