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.
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.
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
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
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