Lect 20
Lect 20
Lect 20
& %
Lect 20 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 2
A string of Characters
& %
Lect 20 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 3
An Example · · ·
0 13
I I T K h a r a g p u r \0 ···
73 73 84 32 ··· 0 ···
& %
Lect 20 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 4
Character Array
& %
Lect 20 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 5
Initialization of String
#include <stdio.h>
#define MAXLEN 100
int main() // stringInit.c
{
char a[MAXLEN]={’B’,’i’,’g’,’ ’,
’B’,’a’,’n’,’g’,’\0’},
b[MAXLEN]="Black Hole",
c[]="Quantum Gravity",
*cP="Super String" ;
& %
Lect 20 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 6
& %
Lect 20 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 7
Output
$ cc -Wall stringInit.c
$ a.out
a: Big Bang
b: Black Hole
c: Quantum Gravity
*cP: Super String
a[0] = B, b[1] = l, c[2] = a, cP[3] = e
& %
Lect 20 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 8
Note
Initialization of String
#include <stdio.h>
#define MAXLEN 100
int main() // stringInit1.c
{
char a[MAXLEN]={’B’,’i’,’g’,’ ’,
’B’,’a’,’n’,’g’,’\0’},
b[MAXLEN]="Black Hole",
c[]="Quantum Gravity",
*cP="Super String" ;
& %
Lect 20 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 10
& %
Lect 20 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 11
Reading a String
& %
Lect 20 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 12
#include <stdio.h>
#define MAXLEN 100
int main() // stringRead1.c
{
char a[MAXLEN], b[MAXLEN], c[MAXLEN] ;
Output
$ cc -Wall stringRead1.c
$ a.out
Enter the 1st string of char
The world is made of facts
a: The
Enter the 2nd string of char
b: world is made of facts
Enter the 3rd string of char
and not of matter.
c: and not of matter.
& %
Lect 20 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 14
Problem Solving
& %
Lect 20 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 15
0 if s = null,
length(s) =
1 + length(tail(s)) otherwise.
The tail(s) is the string after removal of the 0th
character of s.
& %
Lect 20 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 16
Non-recursive length()
while(s[len]) ++len ;
return len;
} // lengthI.c
& %
Lect 20 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 17
Recursive length()
& %
Lect 20 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 18
string.h
& %
Lect 20 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 19
strlen()
#include <stdio.h>
#include <string.h>
#define MAXLEN 100
int length(char *);
int main() // lengthL.c
{
char b[MAXLEN] ;
printf("Enter a string of char\n") ;
scanf("%[^\n]", b) ;
& %
Lect 20 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 20
printf("length(%s) = %d\n",b,strlen(b));
return 0;
} // lengthL.c
& %
Lect 20 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 21
Problem Solving
& %
Lect 20 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 22
Non-recursive Function
Recursive Function
& %
Lect 20 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 24
strcpy()
The function
char *strcpy(char *dest, const char
*src); copies the source string to the
destination string. It also returns the
destination string pointer.
& %
Lect 20 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 25
Problem Solving
& %
Lect 20 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 26
Non-recursive Function
strcat()
The function
char *strcat(char *dest, const char
*src); concatenates the source string to the
destination string. It also returns the
destination string pointer.
& %
Lect 20 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 28
Problem Solving
& %
Lect 20 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 29
Non-recursive Function
Problem Solving
& %
Lect 20 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 31
Inductive definition
true, if p = Nil
false, if p 6= Nil and t = Nil
prefix(p, t) = false, if head(p) 6= head(t)
prefix(tail(p), tail(t)),
if head(p) = head(t)
& %
Lect 20 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 32
Steps
& %
Lect 20 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 33
Non-recursive Function
#define TRUE 1
#define FALSE 0
static int length(const char *) ;
int isPrefix(const char *t, const char *p) {
int m = length(p), n = length(t), i;
return TRUE;
} // isPrefixI.c
static int length(const char *s) {
int len=0;
while(s[len]) ++len ;
return len;
}
& %
Lect 20 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 35
Recursive Function
#define TRUE 1
#define FALSE 0
#define NIL (’\0’)
int isPrefix(const char *t, const char *p) {
if(*p == NIL) return TRUE;
if(*p != *t) return FALSE;
return isPrefix(t+1, p+1);
} // isPrefixR.c
& %
Lect 20 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 36
Problem Solving
& %
Lect 20 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 37
Non-recursive C Function
& %
Lect 20 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 39
Recursive C Function
strstr()
& %
Lect 20 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 41
Problem Solving
& %
Lect 20 Goutam Biswas
' $
PDS: CS 11002 Computer Sc & Engg: IIT Kharagpur 42
what()
1st call: a 4 + a
not but 1
Return3
2nd call: b 3 +
1 b
not but
Return2
3rd call: c 2 +
1
c
not but
Return1
4th call: \n 1 +
\n
1 Print:
not but
5th call: EOF
not but Return0
& %
Lect 20 Goutam Biswas