0% found this document useful (0 votes)
9 views

DS Module 1_Strings

Data Structure BCS304 Module 1 ppt Strings

Uploaded by

ashwiniiseait
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

DS Module 1_Strings

Data Structure BCS304 Module 1 ppt Strings

Uploaded by

ashwiniiseait
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Data Structures and Its

Applications
Module 1
Strings
THE STRING ABSTRACT DATA TYPE
• In C, we represent strings as character arrays
terminated with the null character \0.

• As an ADT, we define a string to have the form,


S = S0, .. . ,Sn-1 where Si are characters taken
from the character set of the programming
language. If n = 0, then S is an empty or null
string.
• In C, we represent strings as character arrays terminated with the null
character \0. For instance, suppose we had the strings:
#define MAX_SIZE 100 /*maximum size of string */
char s[MAX_SIZE] ={"School"};
char t[MAX-SIZE] = {"Building"};

• Notice that we have included array bounds for the two strings.
Technically, we could have declared the arrays with the statements:
• char s[] ={"School"};
• char t[] = {"Building"};
• Using these declarations, the C compiler would have allocated just
enough space to hold each word including the null character.

• Now suppose we want to concatenate these strings together to produce


the new string, " SchoolBuilding“. To do this we use the C function strcat.
Two strings are joined together by strcat {s, t}, which stores the result in s.
• Figure shows how these strings would be
represented internally in memory.

s[0] s[1] s[2] s[3] s[4] s[5] s[6]


S c h o o l \0

t[0] t[1] t[2] t[3] t[4] t[5] t[6] t[7] t[8]


B u i l d i n g \0
• Example 2.2 [String insertion]: Assume that we have two
strings, say string 1 and string 2, and that we want to insert
string 2 into string 1 starting at the ith position of string 1.
We begin with the declarations:
Pattern Matching

• Assume that we have two strings, string and pat, where pat is a pattern to be searched for

in string. The easiest way to determine if pat is in string is to use the built-in function

strstr. If we have the following declarations:

char pat[MAX—SIZE], string[MAX—SIZE], *t ;

• then we use the following statements to determine if pat is in string:

if (t = strstr(string,pat))

printf("The string from strstr is: %s\n",t);

else

printf("The pattern was not found with strstr\n");


• Although strstr seems ideally suited to pattern matching, there are two
reasons why we may want to develop our own pattern matching function:
1. The function strstr is new to ANSI C. Therefore, it may not be available
with the compiler we are using.
2. There are several different methods for implementing a pattern
matching function. The easiest but least efficient method sequentially
examines each character of the string until it finds the pattern or it
reaches the end of the string. If pat is not in string, this method has a
computing time of O(n.m) where n is the length of pat and w is the
length of string. We can do much better than this, if we create our own
pattern matching function.
Knuth, Morris, Pratt pattern matching
algorithm
#include <stdio.h>
#include <string.h>
#define max_string_size 100
#define max_pattern_size 100
int pmatch();
void fail();
int failure[max_pattern_size];
char string[max_string_size];
char pat[max_pattern_size];

You might also like