0% found this document useful (0 votes)
18 views17 pages

Strings

Uploaded by

Baljinder Singh
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)
18 views17 pages

Strings

Uploaded by

Baljinder Singh
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/ 17

... .. . .. -. .

! I

.. ,. ,

• II •,


.
•' ~ I
. ,
•· • J I
,• f jt • I ;

.
.,
~~-=--~.~
' I

., Strillg· •• • •tya ... ,..

•l .. •. In eaI"IiCI'. Ch!lPteis, we. J)ave ex()Iainc:id t!te 'ccirCt?~t._of ~rs. In this cha~ter, We Wj
strings. Let ·us discuss first what a chara~ter 1S whose combmation forms a String. 11 f~
.,
'

I

@ !)~fig10N . , , .. -. '
_ . In._$.lll.lPie, tem1s,[_ch~~ter i~ ~e ~m~l~st .unit of a string or te,i/ !ust like, an in~ - •
fl~~g-~~t:yal~ue, -~ _indi.Yt~~8' ch~c!~~ ~,.e. ~ ~J?h~~et, a p~nctuat1on mark etc.) cgcr-.1
sto~ i,n ~ Y,~~l~: Siirritar~y,we c~ ~o ~!~~ c?m~1?~!1ons ,~f charact~rs (call~ string) in:~\
~Jndi~du al ~h~~ter (f~r ex~I?l~ _.,.~ , .. 7 ~-- ~:/>. :-·. ~~~. 1s ~tor~ •n _a v~able of ch~~ ~
r, '. tbe initialization is ·ven by••, ·:· ,. : ), . , , .•,
c~:fh+:s~}~h~re c~:is _c~iiradter ~ Varj~b•~ _w,~~~~ ~ith- ·s· *i .
r • ' • '. A • '. ••

C ' ~.:. \: ~:·. ~. • •'


t US discuss a ~1,eie ~~~~I~ !O}~l~stt;~J~}l~f- to,. d~_al ~1~ ·-~- l~~v1dual character.
8.1. To read a~~ ~p~y 1~ _ch~~c~r._ .. . .. = .. _, . •. • • •

• . , ~~~natlo ~ The above nm • • • •• •- ·, , · J


~s~~ys.the,m ~ ttie scietin. •-~--~:~~s .~~I l(! B and reads a ~haracter from ke~boafd~
•~ . Next, we 'shall diSCuss how tb' ., . -. - .: • . • ..'.. ~ •~ . .,-••. • .. • .. . •, .
~~ou~ ope~°':18.~.!C ~~=~ tiple characters 1n acharacter variable and whatalC
In C, every c~ter is stored ii • . . . . .•
Code for Infonnation lntercb ) v ~ ~licttly according to its ASCII (American S
is 65, ASCII ~aloe of 'a' is ::i:;
'r.-bich_ 18 an integer value. For example, ASCll valUC~
48• ··To understand it. let us discuss followiD&
:t • ~=~"•-N -· ..
-
._ .: ili ,& lJ ill la ,,.
~l ia -- -- -- -~ -- -- -.

~ s.z. To display ASCD value of any character.


~( ~

~f l()
..

~~ ·~: • .•••. ·~ _ .. ..
. . ; char str [50];.. /r • "' -

e so· ~:~:Jt'Can ~fore t#.xlI!l


111
' , • " ·"
, ' ..
' • •
1r

I •
, .~

lcter·array wi th siz I

•~ ;arlabIC str is~ chan


and ~~st l~atio~ ~hould be leftblaqk j
f~
we should store.49 c~ t~ rs •.~ • .
' • .. . •. • ~ ' .. •
' • •
t . . . ~ ' .'\
• ' : •
,. • I· •• , , .. •
, • , • t
•' (I •
• ;_" ' • t •! j ., ~'

·in_iti_a)j~n 8n.4 using


8
store .string ~-a c ~ r•~¢able: u~ •
• ; I~ ~.,' ~ ... : , .,
I

I
I .'11~ ~ 2 1 ~ -~ -s ~...... J: •• .
: .. .\A lar str[10 ="HARSHIT' ; .
, ' · •
I r.icters and HARSHIT is assig
N In above statement, str is rui arriiy of 10 cha
t no NU LL charac ter has bee n assigne d in. above initialization. Let Us s°ed to villi
al ote tba puter. The rne : how ill) ilb~l
. Phabets and NUUs occupy the contiguous memory locations in com • or y, d~
. . _: . , . .
;18 as follows ~
str[7J str(SJ Slr[BJ
,. . • : . ~ str(0J
s~1J str(~J str(3J str[4J str[5J str(6J_

. . . . . str / H / A / R /
• H /. I • T / 10 / 10 /. ~ ~ j j•
5005 .5006 5007 5008 : 009 .
Mem?ry• 5000 5001 5002 5003 5004
Location
Fig. 8.1. Shows string stored in mem~ry
... , .
R•m is-.
•• .-~~-~of:irmitt'"character (\0)
,:
f
~ ~Jtstant. It is II combination of ab kw -~triiJ~
•. ••• IAffi.!LL ch ar ac te r~ = ialize a string then compiler ac atda~
\0. The key poi nt is tha t whe n we init
and 'r-zero le. string (if any). ~ut when Wea~~~
~~ r (\0) at the rest of the loca tion s of
appends a n~ ch lby cod a~
from k~~boaid usmg ~efs() d scanf( ) functions only one NULL value ts inserted , ~,
• · ·, , •. · · ·
'f,,the poSJtion where s~ g ends; ? . character variable 8~
. - f : ••• According to above
exam e, String "HARSHIT' is assigned to
mo ry loc atio n (ad dre ss) and at ind ex O of str i.e. ·stt[OJ. Since a character an~~•
stored in 5000 me x: ~
is stored at 5001 memory location and at inde 1
.one byte ofcomputer memory, therefore A .6 of str (i.e. str[61) and stored in memory~ ill'.~
th rac ter of HA RS HIT is at ind ex
strf1J and 7 cha
LL character (\0) automatically by c cooc~~
ing loc atio n i.e. 500 7 is fille d wit h NU
5006. Remain have to be appended explicitly by us~
ula te a stri ng then NU LL cha rac ter ma y
_But. if we manip garbllge value in it and string may no t~
rac ter the n Jast loc atio n ma y hav e
miss NULL cha •
accurately. . ., 1

ple showsffr'
C string con stan ts mu st be en~ los ~ within double quotes (" "). Following exam
• ence charaC!cJ
we· 'can sto re alp hab ets, dig its, bla nk spa ces, punctuation characters and escape sequ
• ..
in a stri ng sur rou nde d by dou ble quo tes. Examples are as follows:·
ptc
\0", - "so ods andeep123@gma il.com", "1+5'*'6", " __'\ "S".
,. ~ •p~'N . .. .
iYA TI AN Al' fD" , :"SU MI5 .

i~li r.ation. •
; .. Program 8.3. To demonstrate the valid init # • I •

~ ..
JD8lD
f () ., •, I
' .., •· .
( '•• • I

, , ,, ' ., .
,,,:,~
,
$
, .. ·f
';
.
' : •~1•'
1 , . : • , ·;.,_
•' •, / I '•


• , :.,

•~• .:. ~-~..•~••,.~•. ••, ;::,,. • )' ,1,••; ': tiL ._•
' ' . '
•• \ ,. ,'41''' 'l' . ·,J
• , • ' • f
'-. • -~-
, ' • , ,•I
i • .,., • '
{
1 /*nlill ~haracter ls'must ~h~n- no'·size. is ~yen*/
•. • ✓ ciw•~rr~ C J~''N IYA TN> ";' •. t\ • :_- :;; :
the string*/ · •:, ~ - 2< .·
\/char str2[ J="NIY\OATI\O"; _" · . t• stor es • NIY as first \0 terminates !•

_numbers aJso· allowed~/~ '_,, , \ ·_ · -'·' -


-" .• -••.. • . : •!
•.✓,' char str3 [7J= t1 ','2' /s\'p ','S ':,'0 ','\0 'J; l*,
all~wed m this way_
ar str4[ J=( f' .. 'J, {'@ '},{ '$~} ,{' 'J,(' %'J,{'~'J,{'\O'JJ;I* special characters also
./ch
.i,'printf("String 1: ~s\n",strl); . . .
• ..·. : •. ... 1
• ~- ..

'. • • • .- ·.·, /
~printt("Stiing 2: iis\n",str2);. : • ·
1 ~tput: .
printf(''String 3; "s\n",s~J); :·: . • , .
• .. • .

.. String 1: NIYATI. ·
printf("String 4: ~s\n",str4);_ . ..
· String 2: NIY
etch()· . - slrfug 3: faspSO
1 •· ' ••• · • •• • • · • • • • ·• · ' ·• .
. ' . " ' • •
•. ' ,,
String 4: ·-@$ %<
• • • :r ... •••.

JBD '
e invalid initializations of a string..
~~111
don Reason .. ,
,.1~ ·' • ' , • Correct example
••""''
IP" ,NJ)'~J., ' Enclosed within stn • •
• g1e quotes
1 • •• ~
. • char str[7]="NIYATI"
.~~
r; ~uj\'.a.a ... , • No double quotes used• • h [] ·"SUMIT'·, •
c ar str =
,~ {{''Jl''},(I},(~\~'}}; Onl~ s~~l~ q_uot~s are allowed. • charstr[]=( ('H'},('1'},('\0')}~
,~ ~,~", s2[ ]=sl, Strtng 1Dltialization s2□ = sl is not correct. char sl□="ss"; char s20="ss";
i,1stl ~ (' , 'b', 'c', 'd', 'e'} Too many initializers • '. har tr[4]={ 'a' 'b'; 'c', 'd')
, _,{4]::: 8 , ·~~ • • C S • ,

X~ffi~@\((N~r_A.SritiNq USING BUILT-IN


...,,,.'ILTsi· ....,..~~,, .J .,.~,. -~-~\-t·~
ki.♦ ·~.,..~;:-\•~---~*·:'¥.~u:
·t ... , ~
.. -.. ..-::~
,.
~·-1.1..._., :::.> ". .- , ·.., ,
'.," -~, • •• •• , ...... " • •❖·; ';,-.;_; .,) · \ l :.,. I)(. -~- \, "'~ l'. .··--s .It, / /
r,;m,.;;_ _....,.r.llill~:..W..t<•~··;•J~~~~~t;.~~/4~;,~_i-Ji_.;~~-..:~~ilf;•.:~~)'t ! ~.,. .. ,

string can be r~ad from ~eyboard using two built-in functions: scanf( Land gets( ) where
. aformatted mput function and gets( ) is an unformatted input functionjWe already know
)
)~on€anf can rt:ad ev~ry type of input related to integers, reals, characters or strings whereas
~fuOCfunction d~als with strings only. On the other hand, printf() ~nction is a fom1:3tted output
f _} used to display output on the screen and can be used to display data of any data type, and
j,.il~on is an unformatted output function; to display only string on the scree!!) Let us di~ss
~~owiJlg example to d~monstrate_ the ho~ a string is read and displayed ~n the screen us~g
~ jpput output functions. We will also discuss what is the drawback of usmg scanf( ) function
alternate for reading string in the explanation. • •· ·. ,·
- ~ s.4. To read a string given by user from keyword and ~lay it on the scrfflL .••
-
{ .
'()·
i.

• .. char str[IO];. ,
., ,
' ;

' ' '


••
..
' II I \ •

•.. ••.... ,.
. •
,-
••
..
.~ ..

•••·
..
<\

1
1

"'

• •.
~ ,

~
\

.. ..
\

• • ,··
.

:' r

,.
'

• ,.

• '\r
.,•·-,

-~ ~ '·,

ctrscr( );
_, printf("\nEnter a string: ");
. scanf("%s", str); Output: •• - •
printf("String i_s: %s", str);_·
• I
Enter a ·string : HARSH
getch( ); string is: ~sH . • ..•
t I • •

)
Explanation. The scanf("%s", str); reads a string· and printf() function displays the same~
~ screen. Note that scanf tenninate~ the cw:rent input if space is f~und. Therefore, only characters
diat are entered before the first blank space are stored in variable. .• . .. • ·:
However,~ string with blank spaces can be read using unformatted input function gets( }JLet
~ the following example who gets( ) function works. I •

. str[O] str[1] str[2] str[3] str[4] str[5] str[6] str[7] str[8] str[9]

_ str H A R 5 HI
l I I I I I I • I • I I scant 10 3 $
.t ' '

Fig. 8.2. Shows how string Is stored In memory when it is read using
Above figure shows that ·when we read ·a string using scanf( ) function then only ~ne NULL
~ is inserted by compiler at the position where string ends and operating remaining posilioos
e_&llbage values. . .

~ '.".::::-;:◄.:.
~ :::
- - ------ l
•• =' - - - - ~ ~ - - - - ..
+ , . , . - . , ..........•••••·•••..........,__"' '< ..,.....,..._ ___ ,
<>•=-~----- -~ ... Hll'<ft .. >ht
"'•-·-- :~ t ....
>ltttl t F I ft
··-1 :1199 t-~:
-'-'~J",y' ,·..;-t: 11
• _ _,.,, 1 ')•'
"'-<:' ' ~.
~k,
......
✓Program S.S. To read string entered by user from keyword_ and_ display
00
I I•• , •••

l ,.,, , "< • •
, 11 ,, "•, I f •• '
Dl.31.D • ) 1~,.,.,r
,. \ , \
• •I • \ , • •• '-"· t•,
.. , •" k t. I ' • ••
•• I 1 ( t •••
•. • •'
• _.
•-•
, • •
1
• l"' 1 ,-
., • • • •• • l'
L
' the~

.. ... .... • • • • • • - .. •

.
' "- ,_ '
' ._ • • 4 I • I • f t < I
.
{ .. . . .• I
..
char str[20]; .' \

clrscr( ); •
• p~tsf~nt~ r text: "); ..
•• gets(s~);· /* unformatte~ input fun~tion */ • Outpllt:
' . ; •' • puts(''\nText is:"); /* unformatted output function */ ~ •; E_nter te
. · I• • •

S.an.~~ps·
Xt·
'"' Puts(str) • ,. :.i • - • ••• - ·."' . .•ll -•·
- . •
' ••• ,. •' • •
. .
I • t ,•, C.J ., '1 •• ~ • .~

. getch( ); • . • •. • • : . • • .... •. . Text is· u~


. . r , . . . , . , ,.. :~ -~·l' . ~ < .. ..
• f ' • I. .,,
4 ,
; :-;.

I: '. ~. rl ; .. • • • .••. ' sancteep
.• Su .
· . Explanatio~; Unlike scanf function, gets fun~tion can read a string including sp _liiit
. example a striiig is read using gets() and displa}'ed usin'g puts(.). .• .Js· !n¾

\ •) • t ,. ( ... \' ,, l I .' , ; "' ~· ! .., l .-\ ., : : - t
• p • • • .. j ' : ... ' . . ., .. ,. fl .
• ~ t • ,, • , ,

• puts( )function .automatically inserts a new line ·character at the end of the string. ~;. •
' .
• ~ previous examp~es, ~e p~y~ ~,~cqss~ ~~~ t~ ~~~ formatted and u~onnatted f u ~
. i ~owmg how. ~ di.splays .the text stored In a· string. As we know, every string is ternµnatect\½~
; ~~ character failing which ma~ stores spme garbage values at u~used locationi. C ~,
: ~ows• us to traverse a string by. accessing each char~cter one by one until a NULL character1.an~
: and this;can be performed Without using any built-in function. This can be done using a
: understand·ir,-1et·us clemoilstrate the following example. . _ .. .. OOp.\
r~
;_ .,,.Progra m s:c;. To ~la stiiiig ~re~ bY il5er;:1'1~ ~!ay it using for loop.• ; . ~ •. ;

Explanation. user to Program asks


·a string which dtsplayeclby ~cces'sing each ch- e~ter is
one by on using for loop, that runs from i=O until NULL is found. : ••. 1
.. -'. • •••
t "I £ I \,'•
41
f • I 1 I ~• •, t, 1 •, •' ; ' . • ' •
l ;r'J • • '', •. • • .\ • :; • ! ••
f Try Yourself f ' • ..J
,, ,' ,. , • • • t •
I • • •
I \

◄, '
,, •
\,}

' ... ,
•I

;:/ ,.:,, ,~~~ ~,;;~ :;~~~-~~ ~~~~r :Dbil.: grarnu ns~

(
'': f~ ~ -
g p~~
J!
ndlin .
djscUSS a few character ha
1

lowing program : •
~.'

k w ~ ~ :: on s in fol
; JI' !JS S.S. Program to chec r Is achal'lder, digit, punctuation ma
rk
or tab stop etc), ll'acte
• ~ en te r key
~ -- ~ ;; -- -- :- -: -- -. :- -= = = ·= ,· ~ ~
/~ II J( lc < C ~ ~ .
~ () • ' •

l cbst.cb; . I •
t , r I

fi-scr( ); ...
c . tf(''\nEnter an input: ");
~getche();
.
if(isaJpha(ch))
. prin~("~%c is an alphabet", ch
); Output!:
Jse if(isdigtt(ch)) Enter an input: a
e ,printf(''°%c is a digit",ch); I
'
1 ;
.
• • a is an alp~a~t ..

; eJse if(ispunct(ch)) Output2: ~ .,
rk etc" ch)·
printf("\n%c is a punctuation ma '· . ' .. Enter an input:' 1
1'"11 if (isspace(ch
)) • • .
e..,., .,printf(''\nfJ,oc 1s a space",ch);
I.. • I is a digit
' .
. eJse . . . ' • ·, Output3: •
cter"); · Enter an input: $ .••.
.·. printf("\nAny special chara
• flCb( ); . • Any special character
. .
•'G:·FUNcrioNs.
J ..,.
f

i
cJer file string.h must be
..........(, •
'1,~-~~
~~.
• .~ - - ~

hea
I string {unctions ar~ used to han<!1e o~rations related to strings. A str .. _- , . ..: .:•: :
various functions to h30dle ings
.I

~pded for the::Jun~on:i} Let us


discuss I •
• • ' ; , \,t ' •


' f t I f ~ JJ,
• o

• • ( )

::tt~... fN

the length of string pa s~ to it as argumen~.: ~n~ o~ ~trjng ~s


lfbe strlen( ) function returns r (\O l]n ie sy nta us : . . ••'. ; . : · ,
e null c~ te .. · •· · • • ·
,unJ,cr of characters befor s1z e_t strlen ( ch ar *str ); : '
. •· • •
er to char an d fun cti on return s size_t i.e. integer value
where str is po int • •

stnng

m 8.9b. To ftnd lengt~_. ~~_a. ... ) .. :-



' •
I

~ of a ~ .- ,. . ~r og ra
,.. ,.I ll ~ To find le • • • • without using in-built functi
on.
»l,dt fun cdo n strlen ( ). ·, •
main()
~.h> { " ,: I'••
. • t
:
j

I
• ,
t,
f



Iii() char str[20J;


. int i,count=O;
f • •' '' ' , ,, ' ' • ' •
• , ' clncr( ); •
I

- ,(20); • :'); : • . . · · .:
• •. •pri~tf("\nEnter any string:
cm( ~ • ••· • . . .. ,' . .;· . , ,~ ..
pimt"'1f.nter any string: "); , gets(str); . • . : ·. •
for(is(); str{i]!='\O': i++) • . , . ••.
,
11111(1): • •
racters of string•/ . count++;. _: . , . ,.
/t lien() n,tums' number of cha printf(''Length of string is %d",
count);
string is 'id", strJen(s));
~ of , ,; ., · : . . ,. · .- ·
1etch( ); -- -- .
lld( k .- J • --1 Contd.
1 _ _____
. . ; . _ ,_ _ _ Il l t-r
.:... .--~-i-..;..
~----~--.. ~f ._ __ _~
. , ..... ,
.,.._,, ..,..,.. .._ _ _ ,......... ....... _
_._,_, ~_..., .....,..
...... ..,_,,. ..,._
. . .... · - "' ... "'" ., . . . . Y\UO
.,~.... ....,.......,., • ....t..
...~.. ...-... - ......._ ...
.. ..,.jo,b ,....--.,:.a
J-e•e :.a.,.,w ,........_

..
% ttttt) _,t,#$ 1 C
·- #
*..,,
_.-.e ullu
~-- ,_
---
... -
,_ _ -_ _ ...._......,____.<a dtstt ••-T
........ ....~ -
,,
~

I
i
I

Output. • r,,
••-' •7., :: :• '
:1. ~ " ; "•~ : ,,1,. ~ l ~" '; I • •
I
0 4

Enter any string: Sumit Kumar· · . ;· ;•··, j- . ~ i' : •·~ : ~ ; , , : •. .

Original string is Sumit Kwnar: ·:::r• .:-:·r« .. -' :,-., . ~- ...... )

Copied string is Sumit Kumar O --::.· • '• • • •. '


1
,w :-~,'.· • •• • ~ . :·· :- ~ • . •,. < • ' , ''· > •. )I

· ~.~~ .8~)~. ·.~iCs -~ ~~ng: i~~ ·~~ ~~g us~~·in-btillt funCO~-'


8.1 Ob. does the same without using .~-built function.
·:·w• • t(.) . • .. . •. ·,' .. :... •
:: j
1

'; • 4

.•
. .
1

• •
s ~ us~g
To compare t w o
.
'. ,: ..
'
•• .
f .. - ·•,.
• I
• ". - .

,.

main()
-
..._ I
- ... -- .. '

{ •• • .~ ' • t
., t char strl [20], str2[20];
char strf[20],str2[20]; • •• • • • I
inti, j,flag=O,L l, ~2; •
1
'

printf("\nEnte r s~g• l : "); f


clrscr( ); .• t • • • : ••

. printf('4\oEota: Siring!:':);. . ! • •. \ gets(strl)


• • ' ~:
printf(''Enter string2: ");
gets(str I);
gets(str2)
' printfr\nF.nter string2: j; .: .
.. rtind length of strl, str2 & store in LI,l
gets(str2);. . 2 8
if(strcmp(strl,str2)==()) ,. compare 2 strin~•/ for(L 1=O;str1[L1] !='\O';L1++); ~ Pectivc~
printf(~trings \~%s\" &\"%s\" are eq~"~ I, . ); str st2 for(L2::0;str2 [L2]!='\0';L2+ +); 'I
else if(Ll=L2) /* compare length of strings*/
{ •
• printf('astrings \"%s\" &\"%s\" are N~ equal", str~, ~~).;
getch( );.. • ' •• -. • • - flag= 1; /*initialized to 1*/ • .
). for(i=O;str1[i] !='\O';i++)
/* c~mpare str1, str2 character by h . •

...
,'. . '· . ....
if(strl(i]!=str2[i])
.
. • ' . ·. •• • .flg=O a_ ;_ .
· · C ~ ' / ..

.. , .
,
' '.. \ • :, I break; ,~ quits from for loo~~/ •
} . • . . .. '
.• ' } • <

if(flag) ' .
....
printf('~Strings \;'%sY' &\"%-s\" 'are ~ual" •

else':.: · . . t
• . . . .. • . . ,Strl, nil),
. . ·. , .; . ~.". ,
• .. . . . . ,,
,: -~ ' • . t.,_· \"' ..... ,
•: printf("Strings \"%s\" &\"%s\" are NOT "·
getch( ); .• equal '~1•~
• . ,. ·: ~. '. ,·, ·.} <· ... . .....
,,. \:' (.. t'" . • '

•Ou~tl:'. '•;: :: . . .r ~ .•: ~- •• ·:-· • .... ·-~ • t .: '. ·:·.~ _' ·.': •utp~t2: ·. •. -'
I

.. .. .. ... '"' . . . .. .
Enter stringl: rose -,·: • . : · _. < ,. • Enter ~~gl: Sumit r • • •
• I
Enter string2: rose , ••• · Enter string2: Sumeet
Strings "rose" & "rose" are equal Strings "Sumit" & "Sumeet" are not equal

I

, Explanation: Program 8.12a: compar~ two string using in-built function where as program
8.12b. com!:.::e,,,~~o strings without using in-built functi[n . ._I
ij ·k
. I ~v( )·• ,
•·r~:1~~-,.. , . . . ,
• · ·· · · • l ., I •
. ' • ,. J • • • ........._

·' The strrev() fun~tion reverse a, sl!'ini}~e s)'lltax: • • • •


• ····' • .••. :· . ' •char *strrev(char *s);
where s is poin~. . character
.. . .. . ··~~
,
~o
function returns a pointer to char.
' , ...

Program. 8.13a. to r,ev~fle: a string using in-. ~ . 8.13b. To reverse a string withld
built function stmt'( ). . . _: • . •. . using in-built function strrev( ),
#incl~t ring.~ main( ) r •

m~<> ....
e

. "
t
J . char str[20];
char s[20]; • - inti; :
... . ... . ,
. ,!r5c_r( ); ,·• -~. ·;. ! • ·.·•' . • • ,.
I •""'' • "4 ,. ~.
clrscr(); I

,-intf(''\nEnter JlDY string:~~'): •• '1 \ ;


J

• J ·' : . •.'
~tf("\nEn
. .
ter string: ");
gets(str);
• for(i=O•str[')I
1 ·='\O';i++)· 1•-,,,.,.batendofstring "/ •
• '
1--· .,. decre ' ·-
Pl'in~(;l\_h me?15 i by 1, ~ skip NULL ~/ •· •
\IIACVCJ'Se IS '')• ,
While(i>::() • ' • •• , t

{ ) / loop continues upto_O*/ • ,,


~ ~- .. •"

"' .
. ... . . ~rintf("%c",str[i]); •• ••• •1 •• • ••~ •
1-- -·
' .. ,

( . . .
• > I
}. I I • •,~ I
< " I" ti • • - ", J .. • f •. ,,

I • ' I.• ' .' .. r ..


getch( ); •
} ..
• • # •
•• 4 ••

'
,•.
' •.
·,,

. .
. .

: sumit kumar [email protected] •. -


: SUMITKUMAR77@G~.COM
: Program 8.14a. converts· a String into upper case using strupr() While program
l'Clta each character into upper c~ one by one without using built-in function and displays
.......a:.n. • . . , . ,..
I - - ....
~is. ,-str lwt( l · ' ,. · ' ·· •• ••• ' ·
• i

~
{, • .~ff";;-;;"v-;· s~g·in ;o fu~e ~ :.:
v : . - , : , .... t ',", .char·*stt , h * )·
upr(c ar s '
.. , . . . .' '
.· .... . . .
.. . ••• ~ • •• •• :, ••• . ~ · ' turns inter to char.· . 1
- where s 1s pointer to character; and function re po
••

.
· · .· . · · .• ;;-: to·l~wer Program 8.1Sb. To convert a Sh-:- ••
8.1Sa. To convert a string ID , . • •• .,} case without using in-built r,.,._·• 111g ill
case using in-built function. UflCtion. to lo'.)
1
..

#include<string.h>~ #include<string.h> . . . . . ~
main() main()
{ {

Output:· . ··-·"'- .... · ,t·, •··! , ,: ;! , '·_.1 •-. 1, _••• ,,,, • • ,l ~ ,, •· ; t .•

Enter string: sanDEEP , ,'.


_ . . ,. . .
String-inloweFcase. ;sandeep.. -:-, -~ . ' \, l . -• • • • - -. -
'•
, - ... • ,, J..., • •.• '" .. 4, ,. , 'f,1,"',~··--..~-..... -·. •
, • • ',
~,-....,,.1i ·;-.
I . • .- ,:- , • ., .. ., ,. • . , ~ .. .. •·~•••_, "
0
·~
. • ,_
J ... ..... • .
,. ~- 11. ?Exph\Dation:. Program 8.15a. _c;qnveJ
!s ~ ~tring-into lp~erc"8e _using st;rupr( .) whut~~'l
~th·~~---~othnverts eac~ cJiai:acter into ~o\V~ ~ P! one without ~sing built-in funCtiolland~;
. em on e screen. - , . •
;~!~~cpr
t
~ t •, . ' ;. • ,,, ... " ...; ..: ,.. ~""; - .

.
. .

t , , ~ - - ; t .•.J?..
,h
.. ' • .t • •

• ..... ~ -~-
'

i Like strcpy( ), this function alsocdpies:one string into another string except it copies maximum
~n characters of string org_str to cop_sti Tlie·syn~ is. ... .~ •.. •;- , .
; char *strncpy(chat *cop__2str, const char *org_str, size_t n) .
} . where org_str i's' l)Ointer to const;. cop;..Str ·ls pointer to char and n is maximum· number d
:characters. This function returns pointer to :char of cop_str. • ·, . •
.,. • • - .J ' .. I • ~

Program ~.16a. To copy n ·chara~r ~~~o ne Program 8.16b. To copy n char,cterfrom.~


-~. o.q- ano~ e.-,~ l,~~~UJ .~~tion. . string on to another without using built•m
,~- ••':,:•-:1• • J ':.''~·. . fun~on. ,,f·, : _ I-'•, • ';:. C 1
, ' •
1
' •

main()
- { ' •,, ...

c~ar strl[20], str2[20];


str(20], cop_str(20];
int i,n; . J I t, J I
.. '
'

clrscr( );
printf(''\nEnter any strin~: ");
r any string: '');
. gets(strl); •ed7'' •
'; • printf("Total charaCters to be cop•
... . . .. . _ scanf("%d",&n); • ••
l

I I

' -'
~ string: In dependence
l "' I 1

'" ' ·:
f.lliet an~ters to be copied? 6 ., . ·,, , ,
~ c! string is In dependence . . . •_._., ·:· ' __': •.
()l'lgi!l ....ng is In dep ' '
.ed S1.1J..1,• -. .' • •.• • •• .
• ) ' •
I • •.

' •
• •

-
.. •

.
/ , ' r .,. I . .-· • I

~i~
I •

~
Col'1 The =~-8.1~ ·re&k;•strir{g and s~y() fu.nctio~ at m,os~ ~-c~
org..str JJ1tO cop_ll g . ~e _Program 8,16b. does the same process without stmcpy() funct!,~n.
t..11111 ,>~t(' ) •• • ' :: ' •
,'•#{~)•~ • ' l

. .
I • • • • •
P".... i,: • • i• •

~ :.·'·•~~,..:'~';..~ -'1' \ ,.) •; .-.) .~t t • :- It

fl Like strcirt( ), this· func~on also combines One string into another string except it combines
. uJl1 n cbafaCters of strin; s1 to s2. :T!:ie. s~tax is. _ .
~ . char s~cat(char *s2; const char *sl, size_t n) '
-. IVbefe sl is po~ter to const, s2is pointer to char and ti iS maximum. Ji.umber of-charac~.i~S
t....rtlOD• re•nffl~ pointer to char of s2.
1,UAUU
., ;,· · ~-.· . . . 'l' . . . . , • . I •
r 1

.

• .... •
~ • ..t.

i++;
}
T
(
strl[i]='\O'; ·/~\Oto terminate th
printf("Stringl is %s", strl); e 8tring,
1
printf(''\nString2 is %s", Str2);
getch( );
}

Output:
Enter string I : no
Enter string2: where
Total characters to be combined are? 4
Stringl is nowhere • •
-string2 is where
. Explanation. 'fhe program 8.17a._ reads two strings and sm:icat( ) function comb.
characters of str1 with str2 while program 8.17b. combines. the two string upto n charmes at lll0st
. · ,: acter uh 11~
.using strncat( )funcaon. . . _.i- "dh~
c >q ~
'

~f sl t r~ch I....m:t,~
-._,
1 _. _ . . .• "IP- ,' ',
This function
displays all the remaining characters when it finds specific charact .
. . er tn a .
stnng. Its syntax 1s .. : . _. . :· . -~ . • . ~ven
·... ,·
•• ' •• -~ - . . . . . • char *strchr(const char *str~ int_ c);
' ·.:wheres is i,ointer' to char, C is an integer. This function returns a pointer to s.when first oc
of th~ character c, is found in str. But if character c. d9t1s. not occur in string str then this::;:
returns NULL.:: ·- . _. .- • •..- :.. _ · :. _ ·. - , ... , , . , .
\ • . . . ..,. ;, • ' / :.:; '° • -or t 'J ••.i • , .., : ..., : " "' • j. J • J ~ • 'I' .i, ,, .,. , • •
'
<\• t • •, , ( ,i
.
,

Piogr'ani 8.188: To display .r~nia~nlng Progr a~ . 8.18bo' To display •remainiq I

.chara~te~, ~e~ a_~c ter is_matches ~ ' ~ r wh~na ~~rismatcheswi11M,j


built-in function • • , ·using built-in function. . . . .• . • •
#include<string.h> ,,.header file for sl!chr( )*/ ' main( ) • . .
Dl8ID() . , >
i
'

' i: • '

• •
• ':
. ~ ' ''
.•
.,,. l
{ _· •
, '

• ' , '. ' • I '

{ . _ :. ' • . ..:, _;,'. · •,'; • • char str1[20], str2[20],ch;


-_ char sir[~],· ch; • . _,.; . _ .. int. i, j=O, flag=O~ count=1;
clrscr(-); ·• •. 1 • • • • • • . clrscr( ); -. ,-..
. printf('t\nEoter ariy string:· ");. • ., . • ·: .•. •
. • printf(''\nEnter s~gl: ");
. ·. gets(str);. • -_, .. _, , . : ::.·:: •• •,· .- ~, , . ·-. •,: ' ... ·:
gets(strl);
.• J'D'tf("Eottz any character: "); ·i :~: • •• printf(''Enter any character ");
'.''. • ff~" &ch\• ' . ·.: - . -~- •• '. scanfi("%c'',&ch); •' ~< .. ,, ...
sc;ap '-. ' • Ill.•' l -·'1 •. • •
'. -~

/t if1m ct... mnaioing ~ - mm ch•: ~ for(i=O;strl [i] !='\O';i++)


<•· ch) )
I • •

~ •. if( • • •' ~ '. • :' ( •• , I


tant string is ·%s~', strchr(stt, ch)); 1 • • if(strl[i]!=ch &&-flag=O)
.
. ' ' '.

continue;
is not ii llrin8 ~s", ch, Sb'); : ·flag=l;
. str2U++]=strl[i];
}•
if(flag)
{ .• . • ' .
.. • r • ,. I
str2U]='\Q'; . . . ,, s\" ''.su2): ~
printf("Resultanl string IS \ 'l, :::;a}
)
I • • else
'. printf(" '%c' •
_getch( ); IS _not in the string \"%s\" ",ch,sttl):
)

. .
~ t: stn·ng: Su.mit Kumar .
(!)~_ stJY I

~ ~~~m •· • • .
,er:{ string is mit Kumar
8.1 8a . reads a stri n
·
with
~e5ul ,nadon. Pro gra m character. Then replaces that cha racter
pro g and a
~ using stmcat ( ) fun ctio n wh ile
nt in ~ S.lSb. ~eads a string and a character.
Then if
then co
sttiDI. ide for
~ t ~to oelse flag assign
loo p, if it is true
ed 1 and str
rac t
1(
. ·.
statement 1s executed till strl Dnot equal to ch and
cop y ~har~cter by charcter intQ str20.0uts yed
ide for t
8'8 iS equ • equal to 1 the n NU LL cha al string is displa
s tha t cha rac ter is no t in th ~r ~s. Inse!fed at Jth location and fin. . . .
1..nn, if t1agdi1s lay e sbing.
,,.,i •se sp
t, -· . '~ , • • t I

• \~'<.";,{''~'' ., ( )
-i,u :v- . .
• • t
h a specifii charac.ter: It's
·~~~ ... ~

s. all. the ch ~~ ter s (inclu din g blank spaces) wit


.
1biS (unction repla~ .
. • ...•

~- is . . . . ._
cha r *strset(char *s, int c); . t if 'user
1r

nte~ to cha r, c is an inte ger . Th is function returns a pointer to s. Note tha g is


• here s is poi then first NULL .character is found . ,
and nothin
er any stn ng and pre ss ent er key
wot ent . . . . . ..
(kdD . , ..
,iprayed on the screen.
ce all _ a s~ wi ~ ~ Pro gra m 8.19b. To ~place·a11 ~ string with a
,. .. - 8. 1~ To re p~
eru sin g. ~o ilM ~ ~~ r~ te r, ed _b y~ ~w i~ ~~ ~~ 7
_,. cte r e~tered. by us ;- .,_ .- ,~ .: ...,- ~ :.-. . m function. · . . . , . .. . . , _ . .
tr•=·:·· • -• . • · • ..ti. •·;"'. ·~ •►
, • • : • .- . .. , .,
~,;.,. '.

et( _)*./ ~ .. •>::;: ·i - main( ) _.


~ . b > _ /* h~ r f~e for strs • : • I . ' ' • { • .
_,.
• .
;,,.., •····
• .
!\' ,, .. ,..
•. ,: •

.
char str[20],ch;· • •
. • •. • • •! .. , .
• .·', •
.
-)
• • • • •• ,
f .• . ·. • .• , ..·
inti·'
tk str(20], ch; .. ' . ,.
,

.• clrscr( ); ,-. •. .. •• . : .• ~
>;<_;I:·
; •printf("Enter string: "); .
·J
,•.
dncr(); , r. ._~. ' '
"

JIDfimF.nt« string: j; . ' . . . ·~ ...


\
. '

. . _ . •: •
gets(str);
.' ; , . ,, ~.-·.' '~ .·~ •' ..._,~ ' .
gdl(str); . : printf("Enter any character: '?; ;_ .' ';
···. ; ~ - ·.• }, · ,, ·•. . • .
~ -I D Y character: t')~ . . . ch=getche( ); ... . . . .
.' :, • •:· .. •-.·: . • ·:'. ~
cJr,ddle( ); ; : _. . . • , for(i=O;str[i]!='\O'~++) .' .._•.
str
t( , .ch)); str[i]=ch: /$overwrite ch onto str[ J*/. ' •-·. -
pidC'Resultant string is \"~s\'~ ."• ,s~ •
. '
?>: • • ''.
. '·
; .• ·~ tf (~ ia nt ml g -~ ''.'%s\" ":&!
. t • (' ~ • • • ~ ... '

~ , .,,
... ' . . . •

"' • . .,.. . .. . . •.·
, '

. . ,. • , ,
(
~ ,
41, • • ... ..
.
' "
..•. . ...
... ""
.
... • ')
~
(

' .
.'. . getcb( ); ,_ . • •• • .. •
,,, ( '.

.. . ' .... ' \...

'-r llring: Sumit n Sa n~ p


•y character : $ ·• •
haracter ch using string function while
in
"$$$$$$$$$$ $$$$ $'' •·· •'th
llring is ~
Program 8.198. replaces a s~ g WI functi9n.- . . . , . , ' '{ I ...,.
th
• t usi ng in-bu ilt ;::':::::::t :::: ::: :: :::: : : II J-
• ,..:. ¥¢ »4 ':;A U'= :==== :::::
~ does the same W1 OU,..,.,,. ... t
~ ~ p ( >= •• • • • . .. .
, It compares two string and ignore case i.e. cha
racter wntten m upperc~ clrld . .
equivalent The syntax is:
• int stricmp(con~t char *s1, const char *,s2) Ill lo~
This function accepts two arguments: s1 and s2 1 er,
both pointer ·to const and • • \
value. H sl < s2 then it returns negative i.e: < 0,
if sleq
returns positive value. Let us discuss followmg prog ual to s2 then it returns ~eturns ~,
ram , cllld sl ~ 1~
. ~ ~~- :i:'~ compare tw o~ ~4 ign~re ~~
1
#include<string.h> .
,
'1\
., • f • ) -
Outputt:
...ID81D(- •• • • ... ••. •• ~
' • • • "

4
•!: ~. Enterstringl: JAi --•·i ~- •• .·.~, •

• { • •
~ •• '~

Enterstring2:jal
,·-_,'.~ \·. ·,

• • ; :·char str1[20],str2[20]; ··, . • • • • • Strings "JAr' & ".al''


. , • • • - 1ft'i -.l )· • • . ··-
, ,... , .. r;~ :::: ;:~
. , Cu"' -'&\ ,. • -· •• - • ,...
. . ~J~ are ~~ U. a}
- .. , . .
.
'.·• . '··• ·pnn··tf(''\nEn•·er stnn ·gt·" )·· .••.• t. • ••• ... I••' .•,.- 1. ~tp u~
"
:. ·• . ••) .
► l :··-, gets(strl); ;.. ~- ·-•~: . : _, : .•.·.: -~ ..- . , i'-·;~ ..
•· ,

-~i:it~rL-s~~g(:.S~~t : . l !

printf("\nEnter string2: "); ·Enter string2: Sunieet


. . gets(str2); , __ . .· . Strings "Sumit" & "Sumeet"a[t'niit, .
•. ~-i":i ...,}f (s~ ~s ~t, ~~
)~ ) ;~_tt~~~:~~~~~~~;i~~~cue*/
gS are ~ . ... , ··- ~
• 0

~ ~
tf("Both strin 1 1

\., 1~ • ~,
ual")·
1 1
•1se . ~
, " • •
\. _,;..:·~- .,-., !~~ \:• · - .• - • ,.. v • • ! •.:
~ :,_!. t • ..! ;- ' . ,
• •

e
~ ~
. "'"':
,.,.:,t,..
1•
._ Y,1..i
l,\_.,.,·1' ·

., •
• ,,
'- ♦ t
.•, •.

l
tr
t :t.
'I ""·•

l
~ 0 .,, • !": ' , , .. ,'-~,•~
""'4;.1 ;,.,..'•
'f" ti~;~·~
,.,.~ ~~:
J'
- ~
,•
1 '\· " ' , .,.,
~ I (
,
"'
1/,
'
t,,4'

,
._
w 1~:
,t,,4
,, , '
-·,

tJ
).

I,,:.
\,
,- .(,,,ii;
:-.:, • •
~
•••

"i
1 'j
• , r
..
, . .• . _.'. . . ptjntfC~B~~. s~gs'. ~ ~o~· _eq~")_; .. . . ·,,,,, :'. •'
~:· ,,

. getch( ); . • ,,·-· .. ••
. ,. . •.. . ! • .--: • : ... 'l •• ,

t :N
. OTT ;
.: o • ,. .._. • ~• • ',I

• .,... ,,~ ......... ,,,.........;'""I .. ..,..,_


-t I
If I

.. .
,...,

...

,..
• J The strcmpi( ) function ~ ~ macro which . ,J ~ f ..
is equivalent to stricmp. It's prototype in string.I
:.,,_.., .., •t .....,. • •~#• ••-•: -~I

! Try Yourself
• '
_, • I~ I~•• I •

::. • • ! , • :• 1, I

lI ··1 oc om pa re ·~ o~ ~~ 1~ ~~ th
~-) .. 'j,l ~' •
.

ou ~u sin g~ p( -~ _·_· : •. ,:,


- .. '·

, ' >. ; ,1 • ! I • i. ; .' : 7 .. ~ _. • /_ •. - 1' I ,•• ', ; • • I .. ' . ~- . ' • ' I 1


.- This function finds the first occurrence of substrin
displays rest string. g
s2 in string s1·and if it in the string sl Ila
•· 1!:: , . . • .. ,
1
" •. :_. ; ;.'" 1
.:

*
,, • .- ._ . .,
; char *strstr(const char *sl, const char *s2); ;
• • • . cl, +. • • : • •

'. • Returns a P991~.l0. ~* ~~ nt ins~ ~.con~~


.-

both~~~ const.~retumS ~ ~
•• •
:. , -.: 'Y ~ ~<>_n ~ .~ o ~ : : s , 10

urm• s1 . . (· , . ' . ,. ' t l ' • . ' ,-, tam.5 ~~ (points to s2 in sl), or ~ if_s2&ies
• • • • '• . •• • • , • -• •
# •

8.21. To find the-first occlimnce of String ·and dmp . i


lay rest string iuiD& blll
• l I
-1 ••

,.-
' ; il 1 ~, , t
- ..
t i'"1,\•
;, • ~ \I • •• ,. •

strI [20}, str2[20]; •· ! • ' :.


I -, •••
Program ~1. T~ imert a character In a string at any location given by
···maiJic'> - ,. . ' '
J•,.. ...\ • - ' ' ' '

._. _ .....
• I ••
t."•_;.• .J- •
• •••• I'
•,
' •
, ....
f ••
·.:...,
', t·
-j·• ;·,., -·.
.._t,, . ·
•~ ..., • • • t •
I .. .. '.' • . •
J ,_ t I
"'
.. 9
- , ""' •
-,
~ •

{ '
• • " • .. •t ,, \ . p ) t • .,. l.,: ii~ ,• .t" •
• ••
I ••
• t

char st([50], ch;


inti, loc; ..
1, " '" I

. '• "

t Try Yourself t· . •
I


• I

• ~
• • ••

► • ~- t
• • • t ♦ .,. "'" • . , ,- • I •• a

WAP To delete ~ ~ r from a ~ from ll particular toc'at1on given by user.


- • • ': "' _, • • • • ·• -


• ..... ~f ~-, • ~- ••~ .... ~ - . - .... , ~
~ilM""'-"'l".'1t: <Jl1'":'V'g"'':.,IU'"•--, ..,,, •• .. •••• ' •• ·- • • • • .,..

, ·':()l:STRINGS': , .,
• • • .
• •

:· .
I
• ; •

1
• 7 •. i ,:._ • •. !~
L.'r'Mlii.el,i
,_ •
-• f\:-.,... ,..'..,. .' '
- - ~ -._...--~.-~ . . . 'f'-'.,.,,.,,__.',(w.\" -\••
. .
• - .,....,,., {
.... • ••
j
-• • ,,..
'
..,.,.._r•
'
...
' • I , . •• 11it ,

~ string is an ~Y_Of.~us ~ like,; ~e Sto~. ~ ~!Cger in·an indeiof 1-d array; astringail
1
l

sto,es ·a clw:acter in a,n index of 1-.d array. On the other handE' ~y of strings can be declalulai
~tores a string it) !l 2d arra~ Lep~s discuss the dec~tion of array; ofstring. • • .. ,. • •
-~o r:a ira • of . . . . .• ·
-1
j 5 ~ - " ' " ~ , . , ' y strings . ,, ' •slrill
• • Array of string declaration is similar to array of char declaration except 10 array~ •
1> ; . •, .,• ·, '

~Jaration another pair of.square bracket& is· used. Let us discuss the following_ syntaX. •
l 'yib eS) 'Jl~ r~.~..1.•(~ is;,,; -· · ·' swi; ;•,: ·.
storage_clas~ d.lta_ ~ arrayJll~ [l~ I ii• I •
~~ ~,. r,-, •.
f;.~_?'~"'
.,~ --~
.,
.....
•1 ~
_./I
__...;

,·,·
~
,..,r
( tern, '"'tat
~ cJass ex

..

:
\
: .... f'\

,. •• ,_,~ i r.: .,.. ~ 1


1
, . c, auto) lie&iers to the•• • · .:. •, •.
, , , l ,
-.1:r, 1...
••

~• •
l~t,
,.

• · ..
i .... ,, ,
JJ.;,
J .. .
i.
"t
~·l,,....• i i i , . . • ......

C • ),
..

'1~
t •
:~ . ,
.•

. .... ., !
~.
1
ora&"'- is opti<?.~al,
, . char CSCope. of array variable which . ·r.
st hJ11e refers to the kind of values i.e
1 ts. ,,,,- m this case), , ••~
- naJJle is the name of the array • •
1 dB ,
,rraY- . • ... -: ·; ,.-.. (,
n
I JiinensioD SJZe refers to the tota} Stri ••• •• • ' ~, .,. ••
h gs per row and
, 1~ ..i~111ension size refers to the total· C aracters r · . ·· , , ,' ·
• pe String m a column .. , -, .
~
• .
,
Zlf'> u.µ -
cfin1ens10n size can be a•sv mboli tan , ..
d fi d b . . -·t c con stant or c ~i 1t1v e
·-IP: t1tat :·aJ~~~~~s_ul~_ng i~ pos A

c8!1 ~~~ ~ -~. m~ .· ~-u ~io i'~e fln e preprocessoornds.trectttve .


..-~':cb • • •. •
f!iv •• • •
i! ,str[3][ I Oj; · • .•: •. . •
~ ;. • • : - · • , . •. • . _ , c~l

~~!::~:~~~ y. of ~tring~· whose data ·tyi,e· i Chai-'•. Thehich


Ist
~1 a:- o( , tbe arra
_ 1P ,i,o~e ·:..:i• ·10· rr·
•• .:· • ~- -- • '· • .:-, • ••• • : ngs can be storedand2nd..d·unens1·on SIL.C JS . om w
·oos jZe._ i' .'
~A.aSI ... ' have maximum· 9 chara'c ers. Last character, in every string; is left ·for the null
pr:; pS can .
,et! (\O). .
►" :__ :_~ ...:. :_~
~ _..: ..;_ ~•~ -~
:..•.: "L.:. .•_::••.•~ •a~ ·-:·• ;/•~ '•~- _:_ - :

-:~- ~- --- --- .:• .:. :.. '•.: _·'.


• •
..

e any di . • . • . . •~ . . ?• , •• ,, f

iwa tiJD
6
e of dec lara tion ~ we can not giv
mens1o n stze to O or less than 0.
}.t"'" thods • ~.1D • · -. liz · n
to sto re strings in array of st.: -g Th ~o ~~. ~ ...~ _: ~s1~ 1tia atJo
discu ss bow ~ .• ~~ s:
1
. e3e
\ ,• •.. ~
~ ~
_LetU S . . • • h former
1
tho d . • • •tial · · 1 1 ,.

functio ns. We sha ll first beg m wit


built-ID
-~~. i:¥n%, ')\··~.,.n-i'.?~o"";<.F ·s····TRINmeGI.S
e. usmg 1D1; !~t ion .' :
-- g ' /~r m· J
~ · -~~~-~ , ~... • ...... -<.✓ />.". ,

:~,~~~,.~)«~~>.-J>,~..;.l,~~-:,;_.,.~,

of array" ofstiing: :,·.- ··· · r:. :· ·-~.,- ; ·,.J ~; .\ - ~


~~~~\ ·~l',)

'fbe followi ng are the exa mp les of init iaiiia'ti oii


~-fstring*/
chat str[ 3][1 0]= { "Su mit ", "An d", "Sandee~;~_ f; /*3 ~strings·, 9· cliaracte~·
·,.
{"Su mit "}, {"A nd" }, rsa nde ep" } } ; fl' I st dimension size iS dptiddal and
Jfchal' str[ J[ IO]={ */ .
,
string can be e1:1 closed in_ c~r ly bra ces
'f 't'·} · ··{'A ~ ..' '.'d'
.,, . }.
. . , {'S
. : ''..a''
,. ., .n~ ,'d'·
_:, ' e~;. -i .••
' .:;}·_. ·~,
' ' e':~P . -
{~S ' 'u' 'm'·
. . { • •, . , , •,. •
• - SQ'.[][10]= _n
... 3,•• char
• 1 , ••• , -~ . ,.
_r~1:"• ..i.t _i
1:

be wri tt~n sing le quotes*/ '


. . '·· /$.Each cha rac ter of string c·an
,,,,.:r'? , ~...~... ~J
zations is shown below:,..•;:.:;..._,..'· . :-. . ~'!.
Memory representation of above three initiali
_l ••
I-?\..• ii '
.-. l ,\ ............• • ~ • >1
~
f Ill .,. )I.
I
"If ., •
l- t ~l ..

2 3 6 7 8 9•:;..,•· 1
::-1~;.t:.J.~i~:~~~~-..b-,;,>-·•l
0 1 4 5
. J,, .-
'<ft
••
,,:
•,
1-•:' .-·,
.~. I •
I
..
,· •
•.4 ♦
Q
,
s~ -: •u:··· '
....... m , t • •
I•
i ::. ·~ ·r ,· '
:"~0 •~0~: ·,o. \0· •J \D"
,; ,.1'.'1i,.~' 1 '"'"" , _.
.,.,·11 _ ....
-11 ~~"' ·~
r.,
- . ' ." J.

·.. •. \0·
VJ
'
n d \0 \0 \0 \0 .,,·' .. ,__,:\O'.
··. :~,\O
~1 A I - •~. ',L

~
-S1, .a .n ~· d.-,. ..•.8,.·,· .e.. . .;... P~-. .\0 ...\0,.~ \0,
~
(.;.\.
~ .. ,,. t .;I
•~: -' J •"

•• • ,- ••• , .-, ~ '·• ,


• ,#- ·•";.~· '
: I ,. •• • to, I

You might also like