Pds PB 2019 Strings
Pds PB 2019 Strings
Pds PB 2019 Strings
Spring 2018-2019
String (1)
#include <stdio.h>
int main(){ // finds the largest element
int a[100], n, i;
printf("Enter number of elements (at most 100): ");
scanf("%d", &n);
printf("Enter the elements: ");
for(i=0; i<n; i++)
scanf("%d", &a[i]);
max = a[0];
for(i=1; i<n; i++)
if(a[i]>max) max = a[i];
printf("Largest element = %d.\n", max);
return 1;
}
#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";
printf("a: %s\nb: %s\nc: %s\n*cP: %s\n",
a, b, c, cP);
printf("a[0]=%c, b[1]=%c, c[2]=%c, cP[3]=%c\n",
a[0], b[1], c[2], cP[3]);
return 0;}
Note
Space is allocated in main memory for a[],
b[], c[] to store the strings.
The constant string pointed by cP is stored
in the read-only segment of main memory.
The pointer cP directly points to the
beginning of that string in the memory. Any
attempt to change that location results in
segmentation violation in GCC.
(See stringInit1.c in the next slide.)
#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";
a[0] = ’b’, b[0] = ’b’, c[0] = ’q’; // OK
cP[0] = ’A’; // segmentation fault
return 0;}
#include <stdio.h>
#define MAXLEN 100
int main(){ // stringRead1.c
char a[MAXLEN], b[MAXLEN], c[MAXLEN];
printf("Enter the 1st string: ");
scanf("%s",a); printf("a: %s\n",a);
printf("Enter the 2nd string: ");
//read all characters until \n (or EOF)
scanf("%[^\n]",b); printf("b: %s\n",b);
Non-recursive Function
Recursive Definition
0 if s = NULL,
length(s) =
1 + length(tail(s)) otherwise.
Recursive Function
#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);
printf("length(%s) = %d\n",b,strlen(b));
return 0;}
Non-recursive Function
#define NULL (’\0’)
int strCopy(char *dst, const char *src){
int count=-1;
do{
++count;
dst[count] = src[count];
} while(src[count] != NULL);
return count;
} // stringCopyI.c
Recursive Function
#define NULL (’\0’)
int strCopy(char *dst, const char *src){
dst[0] = src[0];
if(src[0] == NULL) return 0;
return strCopy(dst+1, src+1) + 1;
} // stringCopyR.c
Example
Let s="IIT", t="Kharagpur".
After concatenation, s="IITKharagpur".
Non-recursive Function
#define NULL (’\0’)
int concat(char *dst, const char *sec){
int count=0, i=0;
while(dst[count] != NULL) ++count;
do dst[count++] = sec[i];
while(sec[i++] != NULL);
return count-1;
} // concatI.c
Non-recursive Function
#define NULL (’\0’)
int strComp(const char *s1, const char *s2){
int i=0;
while(s1[i]==s2[i]
&& s1[i] != NULL
&& s2[i] != NULL) ++i;
if(s1[i] == s2[i]) return 0;
return (int)(s1[i] - s2[i]);
} // strCompI.c
Pattern
A pattern p[0 · · · m − 1] is a prefix of a text
t[0 · · · n − 1] if m 6 n and p[0 · · · m − 1] is same
as t[0 · · · m − 1].
Example
"I" or "II" or "IIT" or "IIT " or "IIT K" or ...
is a prefix of "IIT Kanpur", but "IIIT" is not.
Write a non-recursive and a recursive C Function
to test whether a pattern string is the prefix of a
text string.
PB | CSE IITKGP | Spring 2018-2019 PDS
Plan Comp Str Data Opr Expr/Stmt Iter int floatarray
Functions
init scan
Characters
length &
copy
Strings
compare
Searching
prefixStructu
substr
Inductive definition
True, if p = NULL
False, if p 6= NULL and t = NULL
prefix(p, t) = False, if head(p) 6= head(t)
prefix(tail(p), tail(t)),
if head(p) = head(t)
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;
if(m > n) return FALSE;
for(i=0; i<m; ++i)
if(p[i] != t[i]) return FALSE;
return TRUE;} // isPrefixI.c
static int length(const char *s) {
int len=0;
while(s[len]) ++len;
return len;}
Recursive Function
#define TRUE 1
#define FALSE 0
#define NULL (’\0’)
int isPrefix(const char *t,
const char *p) {
if(*p == NULL) return TRUE;
if(*p != *t) return FALSE;
return isPrefix(t+1, p+1);
} // isPrefixR.c
Substring
A pattern p[0 · · · m − 1] is a substring of a text
t[0 · · · n − 1] if m 6 n and p[0 · · · m − 1] is same
as t[k · · · k + m − 1] for some k > 0.
Example
"muni" is a substring of "communism" with k = 3.
k
0 1 2 3 4 5 6 7 8
c o m m u n i s m
Non-recursive Function
#define NULL (’\0’)
int isSubString(const char *t, const char *p){
int index = 0; const char *pP, *tP;
while (*t != NULL) { //p cannot be a substring
tP = t; pP = p;
do {
if(*pP == NULL) return index; //p is a substring
if(*tP == NULL) return -1; //p not a substring
} while (*pP++ == *tP++); //test next character
++index; ++t; } //end while
return NOTSUBSTR;} //subStringI.c
Recursive Function
#define NULL (’\0’)
#define TRUE 1
#define FALSE 0
int isSubString(const char *t, const char *p){
int n;
if(length(t) < length(p)) return -1;
if(isPrefix(t, p)) return 0;
n = isSubString(t+1, p);
if(n == -1) return -1;
else return n + 1;} // subStringR.c