c-16
TRANSCRIPT
Lecture 16Lecture 16Version 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 a group of integers can be stored in an integer array an integer array
similarly a group of characters can be similarly a group of characters can be stored in a character array stored in a character array
Character arrays are many a time also Character arrays are many a time also called strings called strings
Character arrays or strings are used Character arrays or strings are used by programming languages to by programming languages to manipulate text such as words and manipulate text such as words and sentences sentences
3Rushdi Shams, Dept of CSE, KUET, Bangladesh
What is StringWhat is String
A string constant is a one-dimensional A string constant is a one-dimensional array of characters terminated by a null ( ‘\array of characters terminated by a null ( ‘\0’ ) 0’ )
char name[ ] = { 'H', 'A', 'E', 'S', 'L', 'E', 'R', '\char name[ ] = { 'H', 'A', 'E', 'S', 'L', 'E', 'R', '\0' } ; 0' } ;
Each character in the array occupies one Each character in the array occupies one byte of memory and the last character is byte of memory and the last character is always ‘\0’ always ‘\0’
4Rushdi Shams, Dept of CSE, KUET, Bangladesh
What is StringWhat is String
Note that ‘\0’ and ‘0’ are not same. ASCII Note that ‘\0’ and ‘0’ are not same. ASCII value of ‘\0’ is 0, whereas ASCII value of ‘0’ value of ‘\0’ is 0, whereas ASCII value of ‘0’ is 48 is 48
a string not terminated by a ‘\0’ is not a string not terminated by a ‘\0’ is not really a string, but merely a collection of really a string, but merely a collection of characters characters
5Rushdi Shams, Dept of CSE, KUET, Bangladesh
What is StringWhat is String
the string used above can also be the string used above can also be initialized as, initialized as,
char name[ ] = "HAESLER" ; char name[ ] = "HAESLER" ;
Note that, in this declaration ‘\0’ is Note that, in this declaration ‘\0’ is not necessary. C inserts the null not necessary. C inserts the null character automatically. character 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 This program doesn’t rely on the length of the string (number of length of the string (number of characters in it) to print out its characters in it) to print out its contents and hence is definitely contents and hence is definitely more general than the earlier one. more general 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
The The %s %s used in used in printf( ) printf( ) is a format is a format specification for printing out a string specification for printing out a string
The same specification can be used The same specification can be used to receive a string from the to receive a string from the keyboard 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 using While entering the string using scanf( ) scanf( ) we must be cautious about we must be cautious about two things two things
1.1. The length of the string should not The length of the string should not exceed the dimension of the character exceed the dimension of the character array array
2.2. scanf( ) scanf( ) is not capable of receiving is not capable of receiving multi-word strings. Therefore names multi-word strings. Therefore names such as ‘Debashish Roy’ would be such as ‘Debashish Roy’ would be unacceptable unacceptable
14Rushdi Shams, Dept of CSE, KUET, Bangladesh
The SolutionThe Solution
15Rushdi Shams, Dept of CSE, KUET, Bangladesh
Standard Library String Standard Library String FunctionsFunctions
we shall discuss the functions we shall discuss the functions strlen( )strlen( ), , strcpy( )strcpy( ), , strcat( ) strcat( ) and and strcmp( )strcmp( ), since these are the most , since these are the most commonly used functions commonly used functions
16Rushdi Shams, Dept of CSE, KUET, Bangladesh
Standard Library String Standard Library String FunctionsFunctions