Upcoming SlideShare
×

# C-16

187 views

Published on

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
187
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
6
0
Likes
0
Embeds 0
No embeds

No notes for slide

### C-16

1. 1. Lecture 16Lecture 16 Version 1.0Version 1.0 StringsStrings
2. 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. 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. 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. 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. 6. 6Rushdi Shams, Dept of CSE, KUET, Bangladesh ExampleExample
7. 7. 7Rushdi Shams, Dept of CSE, KUET, Bangladesh ExampleExample
8. 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. 9. 9Rushdi Shams, Dept of CSE, KUET, Bangladesh ExampleExample
10. 10. 10Rushdi Shams, Dept of CSE, KUET, Bangladesh And the most simple oneAnd the most simple one
11. 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. 12. 12Rushdi Shams, Dept of CSE, KUET, Bangladesh
13. 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. 14. 14Rushdi Shams, Dept of CSE, KUET, Bangladesh The SolutionThe Solution
15. 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. 16. 16Rushdi Shams, Dept of CSE, KUET, Bangladesh Standard Library String FunctionsStandard Library String Functions