Strings
Strings
. ~ .
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
(!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_ ......,.;. ......._.
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•,
- - - - - - -
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 ,,.
/* 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) ., .
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
Set str2=str2+ 1
Step 4: Set *str2='\0'
Step 5: Return str2 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
/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:
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
( 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:
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_)_ __,,
DELETE(T, K, L)
I
Example:
DELETE ("FIRSTPMCS", 5, 4) = "FIR ST"
DELETE ("PMCSFIRSTYEAR", 0,4) = "FIR STY EAR "
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 -
•
/*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+
+)
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