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

Strings

The document provides an overview of strings as a data type, explaining their definition, storage methods (fixed, variable, and linked), and associated operations. It details various string operations such as length calculation, concatenation, copying, and comparison, along with examples in C programming. Additionally, it discusses the advantages and disadvantages of different string storage methods and their implementations.

Uploaded by

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

Strings

The document provides an overview of strings as a data type, explaining their definition, storage methods (fixed, variable, and linked), and associated operations. It details various string operations such as length calculation, concatenation, copying, and comparison, along with examples in C programming. Additionally, it discusses the advantages and disadvantages of different string storage methods and their implementations.

Uploaded by

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

~H A PT ER

. ~ .
Defi ition ·. • , ·.• ' · : i; : th t is treated as a s1.ngle data 1tern. Each ch
,, ~

•••0 •• n • •
of characters t af the array in contiguous memory locations.
ti j t .,, ln ,

. .h
ence an eIemen o
A string 1s•a se•qutored as Th e strin g w1t zero character is
is s . haracter '\O'.
within the stnng m 11
a nu c
the strings are ter inated with
ing. , ~ -· _ . ,, ., _, .~ ., "f""'."
Empty string or Null str p•• rese nt at io n (?f1§ .~!.V.98 . ~;rt;:;_~....~' ,~ .
• str·,=ngQs"'"'<t.t'e ~~
'"'r1·~·ng I
··o
'm' ~'R e~ )'i•ifilit ♦ «W-~~, .._ r; ~
~-• •

>
0 rd NL) $➔re,-
' ,-; ~W
Th
C• ';:H ), t tt
ar.e·• •
three typ es. ey
Stnngs are generally stored in
• Fixed length storage
• Variable length storage
• Linked storage
(~ ~ ~ !. '~ h ti !d L :: '. ~ -= = ==-
~ F l! e d ~ g th stDrag8 aware of the storage
e of sto rag e wh ere len gth of string is fixed. ~ e are
• {It is a typ
of str ing cann ot be ch an ge d, once define9! ._!4or example;
n$rirement. Length
/ char country[20]; /
otted for the string co un tr y
~
20 m em ory loc ati on s are all
Here,
ea te a str in g co nsistin g of th e word "INDIA''. To bo!d
/ The following initialization cr size of the ch arac ter array containing the stnng JS
array, the
~-character at the end of the rs in ·the word "INDIA".
than the nu m be r of ch ara cte
one more
', 'I', 'A', '\ O '} )
char countryf6] = ( 'I', 'N', 'D
t as
arr ay in itializa tio n the n yo u can write the above statemen
If you follow the rule of
follows:
char country[ J="INDIA";
(!onow ing is the memory representation of the above defined string in C.

[ Index I 0 I 1 I 2 I 3 I 4 I 5

[ Variable I N D I A \0

[ Address I0XlOOO I 0XlOOl I 0X1002 I 0X1003 I 0X1004 j 0X1005 l)


~ant ages :
• Easy to accessing data from any given record.
_ / • Easy to updating data in any given record.
/Disad vantag es:
• • Time is wasted reading an entire record, if most of the storage consists of
inessential blank spaces.
• Certain records may require more space than available.
• When the correction consists of more or fewer characters than the original text,
changing a misspelled word requires the entire record to be changed.

(!t is a type of st.ring whose length is unknown as the name suggests. Storage container
contracts and expands according to requirement of string length. For example:

Ich~ *ci~y; J)
city. _
This means that the memory locations are allotted based on input string for the string
_..._ --·---
~dvan tages : These ways of storing strings will obviously save space and are sometimes
used in secondary Il!emory when records are relatively permanent.
/f)isadvantages : Storage are usually inefficient when the strings and their lengths are
frequently being changed.
2,2..
( Strings may be stored in linked list Each memory cell is assigned one character or a
fixed _n~mber of characters, and a link contained in the cell giv~s the address of the cell
contaimng the next character or group of characters in the strin~
( Example:
Consider the string "BSC AND BCA'' stored in memory as follows:
fil3-.tsH ►lcH ►I H ►IAl•I ►INl•I ►lol-l ►I 1-1 ~el-I ►lcl•I ►IAI-I ►,
/4v ant age s: . .
• Modification, Inse rtion and Dele tion oper ation .
s are easi er.
• Storage representation is efficient.
/ois adv anta ges :
• Extr a mem ory is required for the link field.
• One cannot directly access a character in the mid
dle of the list.
~ ti •"&S'AoT" ·~:::
~ g_~, ~ ...
~' • ;
n : ··=·-: .-~ ~- ·' l
t> "....,
• -~~ - :-• ' • ~ • • ~ • ,
• - - • lWHr>c ••• S' "--''.... ...t....i_ ......,.;. ......._.

[Th e string Abstract Data Type (ADT) consists


of a set of valu es, a defined set of
properties of these values, and a set of operations
for proc essin g the values. The string
ADT values are all sequences of characters up to a specified
length. The re are various string
operations as follows:
• Return the n th character in a string.
• Set the nth character in a string to c.
• Find the length of a string.
• Concatenate two strings.
• Copy a string.
• Delete part of a string )
• Modify and compare strings in other w ~
( " The following is a set of operations we might want
to do on strings:
L[i"ength(str) Returns the number of characters in the string.
pos(strl,str2) Returns the position of the first occurrence of str2 ~
d•
0 1'f no mac
t h. oun 1n str1, or
concat(strl,str2) Reruns a new s~ing consisting of characters in strlfollo
wed by str .
substring(strl,i,n) Returns a substnng of length n starting at pos'ti . . 2
1 .
delete(str,i,n) , Deletes n characters from str starting at positionon i.
z 10 string str.
instfrt(stlli, str2, i)Changes strl into a new string with str2 inse t d .
compare.(strl,str2) Returns an integer indicating whether strl>str r e 10 posi.tion i
~~ ::; .., ,.. ,;, ,c _~ --- --- -.. ..: .:. :..2.:. . •
Example: .~ --- _J ~
Let STR be a string. There are a number of ways in whic ,.,
h this string c be . "
as shown in below: an 1mplemented
Suppose STR= "COMPUTER"
(i) In this case as the length is known, it can be implemented
'
the first element denotes the length of the strmg. h as a fixed length array, where
as s own below:
[8, C,O,M,P,U,T,E,R] .
(ii) If the length is not known, it can be implemented as an
array but With the end of th
string indicated using a special "NULL'' character de~o
ted by '\O' as shown below~
Memory can then be dynamically allocated for the string one we know its length
.
[C,O,M,P,U,T,E,R,\0]

I
The various string operations are:
I/

• String length, /
l
l

l 'I
J•
• String concatenation
., -I
• String copy
I
j I
I

'
• String Comparison v
- l
I
• String reverse {_,,,/
I

,I
!
• I I •. ., • I

• Substring

we have a library
•!Th e number of characters in string is called its length. In C language,
ftmction called strlen() and the general form for strlen() function is:
Istrlen (string); y _ __
~or ithm :
- - - - - - I•,
- - - - - - -

Step 1: [Initialize], strl, length, i;


- - - - - - -

Step 2: Repeat step 3 while ( *str != '\0').


Step 3: Set str=str+l and i=i+l
.,
Step 4: Set len=i;
Step 5: Return len

/* Length of the string using pointer */


#include<stdio.h>
#include<conio.h>
int length(char *str);
void main()
{
char str[20];
int n;
clrscr();
printf(" String Length \n ");
printf(" ----------------- \n");
printf(''\n Enter a string \n");
gets(str);
_n=length(str2;
printf("\n String Length=O/o~",n);
getch();
}
int length(char *str) '
{
int i=O; J
while(*str != '\0 ')
{
str++;
i++;
}
return (i);
}
Th e above program prod
uces th e following resu
lt:
String Le ng th
-----------------
En te r a string
College
String Length =7

rA pp en di ng on e string
to th e en d of another
C~ gu ag e, w e have a string is ca lle d string
library function called concatenation. In
function is: strcat() an d th e ge ne ra
l form for str
cat()

Example:
Istrcat (string-1,string-2~
Le t str1= "Computer" an
d str2= "Science,,.
If we concatenate st rl w
ith str2, then we get the
string "C om pu te rS ci en
~ o r it h m : ce ,,.

Step 1: [Initialize], str l,


str2 , ~ ...
Step 2:Repeat step 3 whi
le(*strl !='\O').
Step 3:Set strl =strl + 1
Step 4:Repeat step 5 whi
le (*str2!='\0').
Step 5: Set *strl=*str2
Set st rl= st rl + 1
Set str2=str2+ 1
Step 6: Set *s trl = '\O'
Step 7: Return str1

/* Concatenation of two
strings using pointer */
#include<stdio.h>
#include<conio.h>
char concatenate(char *s
trl , char *str2);
void main()
{
char strl[40],str2[20];
int n;
clrscr();
printf(" String Concatenation \n");
printf(" ----------------- \n");
. printf( "Enter First string \n"); •• .
gets(str1);
printf( "Enter Second string \n");
gets(str2);
.~oncatenate(str l ,str2);
printf("Concatenated String is 0/os",strl);
getch();
}
_char concatenate(char *strl, char *str2)
·t
while( *strl !='\0')
strl++ ;
while(*str2 !='\0')
{
*strl=* str2;
strl++ ;
str2++;

~~-
}
*strl= '\0';
return(0); }
J) ., .

The above progra m produc es the following result:


String Conca tenatio n

Enter First string


Compu ter
Enter Second string
Scienc e
Conca tenated String is ComputerScience

s· Copying one string to anothe r string charac ter by character is known as string copy. The
InizeCof the destination
• string
• should be greater than equal
to the size
• of the source string.

1
fun .an~age , we have a library functio n called strcpy( ) and the general form for strcpy()
Ction is:
I

Istrcpy (string? ,string-j); I


Step 1: [Initialize], str1,str2
Step 2: Repeat step 3 while (*strl !='\0').
Step 3: Set *str2=*strj_
Set strl =str l + 1 I

Set str2=str2+ 1
Step 4: Set *str2='\0'
Step 5: Return str2 I

••

/* Cop y one strin g to ano ther · using pointer */


#include<stdio.h> • I

#include<conio.h>
char copy(char *str l, char *str2); . .'

void main() ,
{
char strl[20],str2[20]; •
int n;
cksc r();
printf(" Strin g Cop y \n");
printf(" -------------- \n ");
prin tf(''\ n Ente r a string \n") ;
gets (strl );
copy (strl ,str2 );
p~n tf(:'\ n Giv~n Stri~g is 0/os",strl);
prm tf( '\n Cop ied String is O/os",str2);
getch();
}
char copy(char *str l, char *str2) ·
{
whil e(*s trl !='\O')
{
*str 2=* strl;
strl+ +;
str2++;
}
*str 2='\ 0' '•
,
return (0);
}
{ .
(iNTRODUCTION TO STRINGS

The above program produces the.following result:


String Copy .
---------------
Enter a string
College
Given String is College
Copied String is College
_....,........___..........,.._______-"'":"'.~
. .4 String C·omparison . '. •' i .

/This function compares two strings to find out whether they are sa_m_e_o·r- different. In
c);nguage, we have a library function called strcmp() and the general form for strcmp()
function is:

C strcmp (string-1,string-2);
The strcmp() function returns one of the following value:
0 - both strings are equal
-1 - string-I less than string-2
+ 1 - string- I greater than string-2
~ample:
~ char strl[lO]= "India";
char str2[10]= "India"; \

01 strcmp (strl,str2); /
the aboVe example, returns the value 0, since both strings are equak'
,orithm:
Step 1: [Initialize], strl, str2
Step 2: Repeat step 3 and step 4 while (*sl == *s2).
Step 3: if (*sl=='\O' && *s2=='\0')
Return (1)
Step 4: Set sl=sl+I
Set s2=s2+1
Step 5: Return
Program:
/* String Comparison using pointers */
#include <stdio.h>
int compare(char *sl, char *s2);
void main()
{
char sl [20],s2[20];
int x=O;
clrscr();
printf(" String Comparison \n");
• &(" ------------------ \n")·,
pnnti,
printf("Enter the first string \n");
scanf("O/os",s 1); •
printf("Enter the second string \n");
scanf("O/os",s2);
x=compare(s l ,s2);
if (x==l)
printf(" 1\vo strings are identical");
else
printf(" 1\vo strings are not identical");
getch();
}
int compare(char *sl, char *s2)
{
while (*s 1 == *s2)
{
if( *s1==' \0' && *s2=='\0')
retum( l);
s1++;
s2++;
}
return (0);
}
The above program produce s the following result:

String Comparison
-----------------------
Enter the first string
India
Enter the second string
India
Two strings are identical

It is used to find the reverse of the given string. In C language, we have a library functio
. . n
ailed strrev() and the general form for strrev() function is:

[strlen (strin g);~


Step 1: [Initialize], str1, str2, i
Step 2: Repeat step 3while (strl [i] !='\O').
Step 3: Set i = i + 1
Step 4: Repeat step 5 for j=i-1 too.
Step 5: Set *str2=*(strl +j)
Set str2 = str2+ 1
Step 6: Set *str2='\0'
Step 7: Return

/* Reverse of a String using C */


#include<stdio.h>
#include<conio.h>
char reverse (char *strl, char *str2, inti);
void main()
{
char str1[25], str2[25]; I t
int i=O;
clrscr();
printf (" String Reverse \n");
printf(" ------------------ \n");
printf("Enter a string \n");
gets(str1);
while(strl [i] !='\O')
i++;
reverse (str1, str2, i);
printf("\n Reverse string is 0/os" ,str2);
getch();
}
char reverse (char *strl, char *str2, inti)
{
intj;
for(j=i-1 ;j>=O;j--)
{
*str2=*(strl +j);
str2++;
}
*str2='\0';
return (0);
}
The above program produc es the following result:
String Reverse

Enter a string
Colleg e
Reverse string is
egello C

a
I'This function is used to extrac t a part of string from the given string. Accessing
s ~ g from a given string require s three pieces of information: .
• The name of the string
• The positio n of the first charac ter of the substring in ·the given s~ng.
• The length of the substri ng or the position of the last character of the substring.
We call this operat ion SUBS TRING , and we write
SUBS TRING (string, initial, length);
·/
Step 1: [Initialize], strl, str2, s, n
Step 2:Repeat step 3 for i=s to s+n.
Step 3: Set *str2=*(strl +i)
Set str2=str2+ 1
Step 4: Set *str?='\O';
Step 5: Return

/* Substr ing operation using C */


#include<stdio.h>
#include<conio.h> .
char substr(char *strl, char *str2, inti, intj);
void main()
{
char strl [25],str2[25];
int s,n;
• clrscr();
printf (" Substring \n ");
printf(" ----------- \n ");
printf("Enter a string \n");
gets(strl);
printf("Enter starting position \n");
scanf("O/od",&s);
printf("Enter number of character· \n");
scanf("O/od",&n);
I
substr(strl ,str2,s,n);
----
printf(''\n Substrin._g is 0/os" ,str2);
·getch();
.}
,ohar substr(char *s·tr1, char *str2,int s, -int a)
I{
inti;
for(i=s;i<=s+n-1 ;i++)
:{
*str2=*(strl +i);
6tr2++;
J
*str2= '\()'.;
·re.tuvn ,(0); J
The .above JJrqgram produces ~the frillow:ing ,result:
; SubstriQg
l
' ----·------"-
;· Enter a stiiing
~College
; iEnter ,staning \positien
3
? Enter -number,of!character
3
StibstriQg is :leg

( A word processor needs to keep formatting information as well as the text sequence
i&elf. This formatting information can be ·kept as a tree where the leaves of the tree are
pieces\ A word in bold face would be kept as a s~parate piece so it .could be pointed to by a
"boll format node in the format tree.
(Given. some printed text, the operations usually associated with word _prooessing,are the
fofrowing:

:Insertion Inserting a string,in the middle of.the•text.


Deletion : Deleting a •stliing :from .the·text.
·Replacement ! ·Replacing one string in the text ·by another.

A ·suppose in a given text T -we want itO insert a string S so that ~s begins ,in ·position K.
~ sllsume the storage position of the string begins with index O. Werdenote this operation as
10 ows:
l1--_ _IN_S_E_RT__;.(T_,_K,_S_)_ __,,

INSERT ("FIRSTPMCS", 5, "YE AR" ) ="FIRSTYEARPMCS"


-,
INSERT ("PMCSFIRST", 9, ~'YEAR") ="PMCSFIRSTYEAR"

Supp ose in a given text T, we want to delet e the substring whic


h begins at position K
has lengt h L. we deno te this operation as follows:

DELETE(T, K, L)
I
Example:
DELETE ("FIRSTPMCS", 5, 4) = "FIR ST"
DELETE ("PMCSFIRSTYEAR", 0,4) = "FIR STY EAR "

Suppose in a given text T, we want to replace the first occu rrenc


e of a patte rn P by a
pattern P2. We will denote this operation as follows: 1

l REPLACE(T, P , P ) •
1 2 1
Example:
REPL ACE ("FIRSTPMCS", "FIRST", "BSC ")=" BSC PMC S"
REPL ACE ("FIRSTBCA", "YEAR", "BSC ") ="FI RST BCA "
l
In the first case, the patte rn FIRST is replaced by BSC. In the
. seco nd case th
YEA R does not occur, and henc e there 1n no change. .
e patte•rn
'

-~~-
it is also calle d Indexing, refers to finding the posit. .ion wher ! l -
e a stri
appers in a given string text T. we call this operation INDE X
and wrtie, ng patte rn P first
I INDE X(T, P) I
String matching consists of searching a query string (or pattern)
Pin a given text T
• Generally, the size of the pattern to be searched is smaller than
. the g·tven •
• Ther e may be more than one occurrences of the pattern Pin the text
text T s .•
we have to find all the occurrences of the pattern in the text.
• omet unes
• There ar~ several applications of the string matching. Some of
. al applicati these ~ su h
Text editors, Search engines, Biologic ons C as
,
• Since string-matching algorithms are used extensively, these
should be e~ . nt
uic1e
. in terms of time and space
)
/
Text : STRl = "FIRST YEAR CS"
Pattern: STR2 = "CS"
0 1 2 .3 4 5 6 7 8 9 10 11 12
[FI I IR Is IT I IYIEIAIR I -

There is a match STR2 with the index 11 in the text STRl

Step 1: [Initialize], str 1, str2, n, max


Step 2: Set max=strlen(strl) -strlen(str2) + 1
Step 3: Repeat step 4 through step 6 for i=O to max.
Step 4: Set found=l •
Step 5: Repeat step 6 for j=O to strlen(str2) && found.
Step 6: if (*(strl +i+j) != *(stt2+j)) •
Set found=O
Step 7: if (found)
Return (i);
Step 8~Set2str2=!\Q '--
Step 9: Return


/*String Pattern Matching */
#include<stdio.h>
#include<conio.h>
int search(char *strl, char *str2);
void main()
{
char strl [30],str2[15];
int n;
clrscr();
printf (" Pattern Matching \n");
printf(" --------------------- \n");
printf("Enter a Text \n");
gets( str 1);
printf("Enter a Pattern \n");
gets(str2);
if (strlen(strl)<strlen(str2))
{
printf("\n Pattern length must be less than Text");


return;
}
n=search(strl, str2);
• ..
if (n==O)
Test");
printf("\n Pattern is not found in the
else
d",'n);
printf(''\n Pattern is found at Index 0/o
getch();
}
int search (Char *strl, char *str2)
{
int i,j, max, .found;
max=strlen(strl)-strlen(str2) +1.;
for (i=O; i<max;i++)
{
found=l;
~or(j==O;j <strlen(str2) && found; j+
+)

• if (*(strl +i+j)!== *(str2+j))


found=O;
}
if (found)
return (i);
}
return (O); •
}

lowing result:
The above program produces the fol
Pattern Matching,
--------------------- t •
• Enter a Text I

:
First Year cs
·Enter a Pattern '

cs !
i
• Pattern is found at In ~ 11 I .
r
i

You might also like