c-16

16
Lecture 16 Lecture 16 Version 1.0 Version 1.0 Strings Strings

Upload: rushdi-shams

Post on 10-May-2015

63 views

Category:

Education


3 download

TRANSCRIPT

Page 1: C-16

Lecture 16Lecture 16Version 1.0Version 1.0

StringsStrings

Page 2: C-16

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

Page 3: C-16

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’

Page 4: C-16

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

Page 5: C-16

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.

Page 6: C-16

6Rushdi Shams, Dept of CSE, KUET, Bangladesh

ExampleExample

Page 7: C-16

7Rushdi Shams, Dept of CSE, KUET, Bangladesh

ExampleExample

Page 8: C-16

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.

Page 9: C-16

9Rushdi Shams, Dept of CSE, KUET, Bangladesh

ExampleExample

Page 10: C-16

10Rushdi Shams, Dept of CSE, KUET, Bangladesh

And the most simple oneAnd the most simple one

Page 11: C-16

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

Page 12: C-16

12Rushdi Shams, Dept of CSE, KUET, Bangladesh

Page 13: C-16

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

Page 14: C-16

14Rushdi Shams, Dept of CSE, KUET, Bangladesh

The SolutionThe Solution

Page 15: C-16

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

Page 16: C-16

16Rushdi Shams, Dept of CSE, KUET, Bangladesh

Standard Library String Standard Library String FunctionsFunctions