Вы находитесь на странице: 1из 29

ARRAYS

The fundamental data types, namely char, int, float, double are used to store only one value at any given time. Hence these fundamental data types can handle limited amounts of data. In some cases we need to handle large volume of data in terms of reading, processing and printing. T a process such large amounts of data, we need a powerful data type that would facilitate efficient storing, accessing and manipulation of data items . c supports a derived data type !nown as array that can be used for such applications. An array is a fi"ed si#e se$uenced collection of elements of the same data type It is simply grouping of li!e type data sl%ch as list of numbers, list of names etc

Some examples where arrays can be used are %& 'ist of temperatures recorded every hour in a day, or a month, or a year. (& 'ist of employees in an organi#ation. )& 'ist of products and their cost sold by a store. *& Test scores of a class of students +& 'ist of customers and their telephone numbers. ARRAY: An array is collection of same data type elements in a single entity. ,r An array is collection of homogeneous elements in a single variable. It allocates se$uential memory locations. Individual values are called as elements

Types of Arrays: - .e can use arrays to represent not only simple lists of values but also tables of data in two or three or more dimensions. ,ne / dimensional arrays. Two / dimensional arrays 0ultidimensional arrays

ONE - DI ENSIONA! ARRAY: A list of items can be given one variable name using only one subscript and such a variable is called a single / subscripted variable or a one / dimensional array. "Declara#$on of One-D$mens$onal Arrays : A 'i!e any other variables, arrays must he declared before they are used. The general form of array declaration is Synta"1 2datatype3 2array4name3 5si#e of array6 7 The data type specifies the type of element that will be contained in the array such as int, float, or char. The si#e indicates the ma"imum number of elements that can be stored inside the array. The si#e of array should be a constant value. 8"amples1 float height 5+967 :eclares the height to be an array containing +9 real elements. Any subscripts 9 to *; are valid. int group5%967 :eclares the group as an array to contain a ma"imum of %9 integer constants. char name5%967 :eclares the name as a character array <string& variable that can hold a ma"imum of %9 characters. 8"1 int number5%967 The subscript should begin with number 9 . i.e. "5,6. To represent a set of five numbers, say <)+, *9, (9, +=, i ;& by an array variable >number>. .e may declare the variable >number> as int number5+67 Hence the computer reserves five storage locations as shown

?umber 596 ?umber 5%6 ?umber 5(6 ?umber 5)6 ?umber 5*6

The values to the array elements can be assigned as number 596 @ )+7 number5%6 @ *97 number5(6 @ (97 number5)6 @ +=7 number5*6 @ %;7

This would cause the array number to store the values as number 596 number5%6 number5(6 number5)6 number5*6 )+ *9 (9 += %;

%al$d S#a#emen#s: a @ number 596 A %97 number5*6 @ number596 A number5(67 number5(6 @ "5+6 A y5I,67 value5B6 @ nul%%ber5i6 -.)1

Example-l: .rite a program to print bytes reserved for various types of data and space re$uired for storing them in memory using array. C include2stdio.h3 C include2conio.h3 main < & D int a5%967 char c5%967 float b5%967 clrscr< &7 printf<>the type ,int re$uires Ed bytes>, si#eof<int&&7 pirntf<> Fn The type char re$uires Ed bytes>, si#eof<char&&7 printf<> Fn The type float re$uires Ed bytes>, si#eof<float&&7 printf<> Fn Ed memory locations are reserved for ten oint elements>, si#eof<a&&7 printf <> Fn Ed memory locations are reserved for ten char elements>,si#eof< c&&7 printf <> Fn Ed memory locations are reserved for ten float elements>,si#eof<b&&7 getch< &7 G

Ou#pu#: The type int re$uires ( bytes The type char re$uires % bytes The type float re$uires * bytes (9 memory locations are reserved for ten Hint elements %9 memory locations are reserved for ten char elements *9 memory locations are reserved for/ten float elements. Example-&: .rite a program using a single subscripted variable to read and display the array elements C include2stdio.h3 C include2conio.h3 0ain< & D int i7 float "5%967 printf<>8nter I 9 real numbers1 Fn &7 I- reading values into Array -I for <i @ 97 i 2%97 iAA& D scanf<> Ef>, J"5i&&7 G I- printing of "li6 values -I printf<>The anay elements are1>&7 for<i@,7 i 2 %97 iAA&
D

printf<>Ed FF "li6&1 D geteh< &1


G

,utput 1 %.% %.% 8nter %9 real numbers 1 (.( ).) *.* The array elements are1 (.( ).) *.* +.+ +.+ B.B B.B =.= =.= K.K K.K ;.; ;.; %9.%9 %9.%9

Da#a #ype and #he$r re'u$red by#es Da#a #ype char int float long double S$(e % byte ( bytes * bytes * bytes K bytes

In$#$al$(a#$on of One - D$mens$onal Arrays: After an array is declared, its elements must be initiali#ed. ,therwise they will contain >garbage>. An array can be initiali#ed at either of the following stages .

<i& <ii&

At compile time At run time.

)*+ ,omp$le T$me In$#$al$(a#$on: .e can initiali#e the elements of arrays in the same way as the ordinary variables when they are declared. The general form of initiali#ation of array is :ata type array4name 5si#e6 @ D list of values G7 The values in the list are separated by commas. Example: int number5)6 @ D9,9,9G7 i.e., we will declare the variable number as an array of si#e ) and will assign #ero to each element. If the number of values in the list is less than the number of elements, then only that mar elements will be initiali#ed. The remaining elements will be set to #ero automatically. 8"1 floa# #o#al-./ 0 1 2324*.35.4 -*267 The si#e may be omitted. In such cases, the compiler allocates enough space for all initiali#ed elements.

8"1 $n# coun#er- / 0 1*4l4l4l67 This will declare the counter array to contain four elements with initial values %. Lharacter arrays may be initiali#ed in a similar manner. char name- / 0 1898 8o:4 ;h:4 ;n:4 ;<2:67 This declares the name to be an array of five characters initiali#ed with the string >MohnN ending with a null character. Alternative declaration is char name 5 6 @ >,Mohn>7 Lompile time initiali#ation may be partial. i.e., the number of initiali#ers may be less than the declared si#e. In such cases the remaining elements are initiali#ed to #ero, if the array type is numeric and ?O'' if the type is char. 8"1 int number5+6 @ D%9,(9G7 .ill initiali#e the first two elements to %9 J (9 respectively and the remaining elements to #ero. Similarly char city5+& @@ D P G7

will initiali#e the first element to P and the remaining four to ?O''. If we have more initiali#ers than the declared si#e, the compiler will produce an error. int number5)6 @ DI 9,(9,)9,*9G7 will not wor!. It is illegal in L.
)&+ Run T$me In$#$al$(a#$on:

An array can be e"plicitly initiali#ed at run time . This approach is usually applied for initiali#ing long arrays.

8"1 ///////// ///////// for<i@97 i2%997 iAA& D if <i2+9& sum 5i6 @ 9.97 else sum 5i6@ %.97 G ---------- Here the first +9 elements of the array >sum> are initiali#ed to #ero. .hile remaining +9 elements are initiali#ed to %.9 at run time. .e can also use a read function such as scanfQ to initiali#e an array. 8"1 int "5)67 scanf<> Ed Ed Ed >, J"596, J"5(&, J"5(6&7 This will initiali#e array elements with the values entered through the !ey board. Lharacter arrays are called strings. There is a slight difference between an integer array and a character array. In character array ?O'' < F9 & character is automatically added at the end. In other types of arrays no character is placed at the end. Hence by using ?O'' character compilerGdetects the end of the character array.

Example-l: .rite a program to display character array with their address. C include2stdio,h3 C include2 ,conto.hI> void main<& D char name5%96 @ DA , R , R , A , Y G7 int i @ 97 . clrscr< & 7 printf<> Fn character memory location Fn &7

while<name5i6 R @ F9 & D printf<> Fn 5Ec6 Ft 5Eu6>, name5i6, Jname5i6&7 iAA 7 G G Ou#pu#: Lharacter 0emory 'ocation 5A6 *9+* 5R6 *9++ 5R6 *9+B 5A6 *9+= 5Y6 *9+K Example-&: Srogram to find out the largest and smallest element in an array. C include2stdio.h3 Cinclude2conio.h3 0aine< & D int i,n7 float a5+96, large, small7 printf<>si#e of vectorIvalue1>&7 scanf<>Ed>, Jn&7 printf<> Fn vector elements are Fn>&7 for<i@,7 i2n7 iAA& . scanf<> Ef>, Ja5 i 6&7 large @ a5,67 small @ a5,67 for<i@l7 i2n7 iAA& D if<a5 i 6 3 large& large @ a5 i 67 else if<a5 i 6 2 small& small @ a5 i 67 G printf<>Fn 'argest element in vector is EK.(f In>, large&7 printfL Fn smallest element in vector is 9I9K.(fFI%>. small&7 getch< &7

Ou# pu#: Si#e of vector 1 = Tector elements )*.99 /;.99 %(.99 %9.99 /B.99 9.99 )B.99 'argest element in vector is )B.99 Smallest element in vector is U ;.99 Example-=: Srogram to sort the vector elements in ascending order1

C include2stdio.h3 C include2conio.h3 0ain<& D int i,V,!,n7 float a5+96, temp7 printf<>si#e of vector1>&7 scanf<>Ed>, Jn&1 printf<> Fn vector elements are 1 Fn>&7 for< i@,7 i 2 n7 iAA& scanf<> Ef >, Ja5 i 6&7 for< i@,7 i 2 n/l7 iAA& for< V@iA % 7V 2n7 VAA& D if< a5 i 6 3 a5 V 6& D temp@a5i67 a5 i 6 @ a5 V 67 a5V6 @ temp7 G printf<>Fn vector elements in ascending order1 Fn>&7 for< i@,7 i2n7 iAA& printf<> EK.(f>, a5 i6&7 getch< &7 G Ou#pu#: Si#e of Tector 1 K Tector elements are ;%.99 (9.99 %.99 =.99 )*.99 %%.99 /(.99 B.99 Tector elements in ascending order7 /(.99 %.99 B.99 =.9..W %%.99 (9.99 )*.99 ;%.99 T>O DI ENSIONA! ARRAYS: There could be situations where a table at values will lave to be stored. Lonsider a student table with mar!s in ) subVects. Student 0at Shy Lhem StudentC% K; == K* StudentC( ;K K; K9 StudentC) =+ =9 K( StudentC* B9 =+ K9 StudentC+ K* K9 =+ The above table contains a total of %+ values. .e can thin! this table as a matri" consisting of + rows J ) columns. 8ach row represents mar!s of student C % in all <different& subVects. 8ach column represents the subVect wise mar!s of all students. In mathematics we represent a particular value in a matri" by using two subscripts such as TiV. Here T denotes the entire matri" TiV refers to the value in> i >th row and> V >th column.

E?A @!E: In the above table T() refers to the value K9 . L allows us to define such tables of itemX by using two/dimensional arrays.
Def$n$#$on: A list of items can be given one variable name using two subscripts and such a variable is called a two / subscripted variable or a two / dimensional array. Two - D$mens$onal arrays can be declared as3 Ada#a #ypeB Aarray nameB - row s$(e / -column s$(e/ 7

The above table can be defmed in L as $n# %)./-=/7

Representation of two :imensional array in memory. Lolumn U 9 596 596 K; Row.9 5%6 ;K Row.% Initiali#in( Two/ :imensional Arravs1 'i!e the one U dimensional arrayas, two dimensional arrays may be initiali#ed by foolowing their declaration with a list of initial values enclosed in braces. $n# #able-&/ -=/ 0 1242424l4l4l67 This initiali#es the elements of first row to #ero and the second row to one. This initiali#ation is done row by row. The above statement can be e$uivalently written as $n# #able-&/-=/ 04 DD9,9,9G, DI,I,IGG7 we can also initiali#e a two / dimensional array in the form of a matri" as shown. $n# #able-&/ -=/ 0 1 12424264 1l4l4l 6 67 Lommas are re$uired after each brace that doses of a row, e"cept in case of last row3 If the values are missing in an illitiali#er, they are automatically set to #ero. 8"1 $n# #able -&/ -=/ 0 1 K; K9 column /% 596 5%6 == column/( 596 5%6 K*

1*4*64 1&6 67

**2 &22

This will initiali#e the first two elements. ,f the first row to one. The first clement of the second row to two and all other Llements to #ero. .hen all the elements are to be initiali#ed to #ero, the following Sh,li/cut method may be used, Int m5)65+6 @ D D9G,D9G,D9G G1 The first element of each row is e"plicitly initiali#ed to #ero while the other elements are automatically initiali#ed to #ero. The following statement will also achieve the same result In ma 5)6 5+6 @ D9,9G )*+>r$#e a proCram #oD d$splay #he elemen#s of #wo d$mens$onal array3 C include2stdio.h3 C include2conio.h3 void main<& D int i,V 7 int a5)M5)M @ D D %,(,)G, D*,+,BG, D=,K,;GG7 clrscr< &7 printf<>elements of an array Fn Fn>&7 fore i@,7 i2)7 iAA& D for <V@,7 V2)7 VAA& printf <>EdFt>, a5 i 6 5V 6&7 G I- end of inner for loop -I printf<>Fn>&7 G I- end of outer for loop -I getch< &7 G I- end of main, function - I Ou#pu#: 8lements of an Array % ( ) * + B = K ; )&+ .rite a program to display (/:imensional array elements together with their addresses. C include2stdio.h3 Cinclude2conio.h3 void main<& D int i,V7 int a5)M5)6 @@ {{1,1,3{, D*,+,BG, D=,K,;GG7 clrscr< &7 printf<>Anay 8lements and address Fn Fn>&7 printf<>col/, coR /lcol/( F.n >&7 prinf<> ///////4 //////// ////// n >&1 printf< >RoFF 9 n forI V 1>,7 V///)7 iAA&

D fore V@@,7 V2)7 MAA& printtD>27>od 5E,O6 >7 a5 i 65V6, JaO65V 6&7 printf<> Fn Row Ed>, iAI&7 G getch< &7 G Ou#pu#: /Array 8lements and address Lol / 9 col/ I Row 9 % 5*9+(6 (5*9+*6 Row % * 5*9+K6 +5*9B96 Row ( = 5*9B*6 K 5*9BB6

col /( )5*9+B6 B5*9B(6 ;5*9BK6

)=+ Srogram to read the matri" of the order upto l," %9 elements and display the same in matri" form. C include2stdio.h3 C include2conio.h3 void main<& D int i,V, row, col, a5l965l967 clrscr< & 7 printf<>Fn 8nter 0atri" ,rder upto <l9" %9& A1>&7 scanf<> Ed Ed >, Jrow, Jcol&7 printf<>Fn 8nter 8lements of matri" A1 Fn>&7 fore i@97 i2row 7 iAA& D for< V@ 97 V2col7 VAA& D scanf <YEdN, J a 5i65V6&7 G G printf<> Fn The matri" is1 Fn>&7 fore <i@97 i2row7 iAA&/ fore V@97 V2col7 VAA& D printf<> Ed>, a5i6 5V6& G printf<>Fn>&7 G getch< &7 Ou# pu#: 8nter order of matri" upto <%9 " %9& A1 ) ) 8nter 8lements of a matri" A1 ) + K * K + K + * The matri" is ) + K * K + K + * )E+Srogram read the elements of the matri" of the order upto l," %9 J transpose its elements, C include2stdio.h3 C include2conio.h3 void main<& D

int i,V, row, col, a5l9&5l96&, b5l9&5l9&7 clrscr< &7 printf<> Fn 8nter order of matti" upto <l9" %9& A1>&7 scanf<> Ed Ed>, Jrow, Jcol&7 printf<>Fn 8nter 8lements of matri" A1 Fn>&7 fore i@97 i 2 row7 iAA& D fore V@97 V2col7 VAA& scanf<> Ed>, Ja5 i &5V &&7 G I- transposing logic simply copying one matri" elements to another in reverse order -I fore i@97 i 2 row7 iAA& D for<V@97 V 2 col7 VAA& b5 V 6 5 i 6@a5 i 6 5V 67 G Srint <YFn The 0atri" Transpose is FnN&7 Zor <i@97 i2row7I AA& D
fore V@,7 V2col7 VAA& printf<>Ed>, b5 i65 V &&7 prin tf <> F n>&7 } getch< &7

Ou#pu#: 8nter order of matri" upto <l 9 - %9& A 1 ) ) 8nter 8lements of matri" A1 ) + K * K + K + * The 0atri" Transpose is ) * K + B + K + * ).+ program to perform addition and subtraction of two matrices. .hose orders are upto %9"%9. Cinclude 2 stdio.h3 Cinclude2conio.h3 0aine < & D int iV,rl,cl, a5%965%96, b5%965%967 clrscr< &7 printf<>8nter ,rder of 0atri" A J P up to %9 " %91>&7 scanf<>Ed Ed>, Jrl, Jc%&7 printf<>8nter 8lements of 0atri" of A1 Fn>&7 fore i@,7 i 2 rl7 iAA& D for<V@,7 V2cl7 VAA& scanf<> Ed >, Ja5 i 65 V 6&7 G

printf<>8nter 8lements of 0atri" of P1 F n>&7 fore i@,7 i 2 rl7 iAA& D fore V@,7 V 2 c%7 VAA& scanf<> Ed>, Jb5 i 65V6&7 G printf<>Fn 0atri" Addition Fn>&7 fore i@,7 i 2 r%7 iAA& D fore V@,7 V 2 c%7 VAA&. printf<>EdFt>, a5 i 65V6 A b5 i 65V6&7 pritf <YnIN& G printf<>n 0atri" SubtractionI:ifference Fn>&7 fore i@,7 i 2 rl7 iAA& D for<V@,7 V 2 c%7 VAA& printf<>EdFt>, a5 i ,65 V 6 / b5 i 65 V 6&7 printf<>Fn>& 7 G getch< &7 G Ou# pu#: 8nter order of 0atri" aJP upto %9"%91) ) 8nter 8lements of 0atri" of A1 *+K * + K ( ; K ( ; * 8nter 8lements of 0atri" of P1 % ) + 9 + * B = ( 0atri" Addition + K %) ( %* %( K %B B 0atri" Subtraction ) ( ) ( * * /* ( ( )F+.rite a L program to multiply A matri" of order rn"n with P matri" of order n"' Solu#$on: Lonsider two matrices A and P of order ( " (. 0ultiply them to get the resultant matri" L i.e.Am"n [ Pn"% produces the resultant matri" L m " l I- program for matri" multiplication - I C include2stdio.h3 C include2conio.h3

C include2math.h3 0aine < & D int a5%965%96, b5l,65%96, c5%,65%96, m, n, i,V ,%, !7 clrscr< & 7 printf<> Fn 8nter ,rder of A matri" 1>&7 scanf<>Ed Ed>, Jm, Jn&7 . I- loop to read values of A matri" - I Srint f <Y8nter a matri" FnN&7 Zor <i@97 i2m7iAA& for <V@,7 V2n7 VAA& scanf<>Ed>, Ja5 i 6 5V 6&7 printf<>Fn 8nter order of P matri"1>&7 scanf< Ed Ed>, Jn, J%&7 I- loop to read values of P matri" - I printf<>8nter P matri" X>&7 fore i@,7 i2n7 iAA& for <V@,7 V2l7 VAA& scanf<>Ed>, Jb5 i 65V 6&7 I- loop to multiply two matrices -I for i@,7 i2m7 iAA& D for <V@,7 V2l7 VAA& D c5 i M 5V 6 @c 97 for< !@,7 ! 2 n1 !AA& c5 i 65V 6 @ c5 i65 V 6 Aa5 i65 !6 - b5 !65V 67 G G 1* loop to print resultant matri" *1 Srint f <YFn Resultant matri" is Fn& Zor <i@97 %2m7 %AA& D fore V@@,7 V2l7 VAA& printtr EBd>, c5 i 65 V &&7. printf<> Fn >&7 G getch < & 7 G Explana#$on: .hen this program is e"ecuted, the user has to first enter the order <min& of A matri" and its values and its values. The innermost loop for <%2@@97 !2n7 !AA& c5 i 65 V 6 @@ c5 i 65 V 6 A a5 i 65 ! 6 - b5 ! 65 V 67 is used to multiply row elements of A matri" with respective column elements of P matri" and add the result to get an element for L matri". This is repeated in the outer loops to get the other elements in the resultant matri". Ou#pu#: 8nter order of A matri"1 ( (

8nter A matri" ( /( o * 8nter order of P matri" 1 ( 8nter P matri" B ( * /+

Resultant matri" is * %* %B /(9 )G+.rite a L program to fmd the trace of a given s$uare matri" of order rn"m. Solu#$on .e !now that the trace of a matri" is defined as the sum of the leading diagonal elements. - ?ote that trace is possible only for a s$uare matri". Trace of A matri" @ A%%AA((AA))@)A%A(@B Row I and columm V are e$ual for a diagonal element. I- program to find trace of s$uare matri"-I Cinclud 2stdio.h3 Cinclude 2conio.h3 0ain < & D int a5l,65l,6, m,i,V, sum7 clrscr< &7 printf <>Fn 8nter order of the s$uare matri" 1>& 7 scanf <>Ed>, Jm&7 I- loop to read values of A matri" - I printf <> Fn 8nter the matri" Fn>&7 fore i@,7 i2m7iAA& for <V@97 V2m7 VAA& scanf <>Ed>, Ja< i 6< V 6&7 I- loop to fmd trace of the matri" - I sum @ 97 for <i@,7 i2m7 iAA& sum @ sum A a< i 65 i 67 printf <>Fn trace of the matri" @ Ed>, sum&7 getch<& 7 G .hen this program is e"ecuted, the user has to enter the order m J values of the given matri". A for loop is written to find the sum of the diagonal elements. The inde" variable of loop i is used for row J column subscripts to represent the diagonal elements. Ou# pu#: 8nter order of the s$uare imatri" ) 8nter the matri" ) ( /% * % K

B * ( Trace of the matri" @ B H!TI - DI ENSIONA! ARRAY A list of items can be given one variable name using more that two susbsripts and such a variable is called 0ulti U dimensional array. Three D$mens$onal Array: A list of items can be given one variable name using three subscripts and such a variable is called Three U dimensional array. Declara#$on of Three-D$mens$onal ArraIs : Syn#ax: Ada#a#ypeB Aarray J nameB- s$(eofno3of#woD$mArray+ -s$(eofrow/ -s$(eofcolom/7 The datatype specifies the type of elements that will be contained in the array, such as int, float, or char. In$#$al$($nC Three- D$mens$onal ArraIs: 'i!e the one/dimensional arrays, three/dimensional arrays may be initiali#ed by following their declaration with a list of initial values enclosed in braces. int table5(65(65)6 @ D9,9,9,%,%,%B,B,B,=,=,=G7 This initiali#es the elements of first two dimensional<matri"& first row to #ero s and the second row to one s and second matri" elements are first row to si" s and the second row to seven s. This initiali#ation is done row by row. The above statement can be e$uivalently written as $n# #able-&/ -=/ 0 1 1124242464 1l4l4l664 11424242464 1l4l4l66 6 .e can also initiali#e a two U dimensional array in the form of a matri" as shown.
$n# #able-&/ -=/ 0 1

1 12424264 1*4*4*6 64 1 1F4F4F64 1545456 6 67

STRINK LAND!INK MHN,TIONS I In L language. an anay of characters is !nown as a string. char st5K967 This statement declares a string array with K9 characters . The control character F9 which represents a null character is placed automatically at the end of any string used.

STRINK LAND!INK MHN,TIONS IN ,:

There are four important string Handling functions in L language. <i& s#rlen) + function <ii& s#rcpy) + function <iii& s#rca#) + function <iv& s#rcmp) + function <I& STR'8?< & ZO?LTI,?1 strlen< & function is used to find the length of a character string. 8"1 int n7 char st5(96 @ >Pangalore>7 n @ strlen<st&7 This will return the length of the string ; which is assigned to an integer variable n ?ote that the null charcter F9 available at the end of a string is not counted. )II+ STR,@Y) + MHN,TION: strcpy< & function is used to copy from one string to another string. 8"1 char city5%+67 strcpy<city, >PA?\A',R8>& 7 This will assign the string >PA? \A',R8> to the character variable city. - -?ote that character value li!e city @ >PA? \A',R8>7 cannot be assigned in L language. )III+ STR,AT) + MHN,TION1 strcat< & function is used to Voin character, Strings. .hen two character strings are Voined, it is refened as concatenation of strings. 8"1 This will Voin the two strings and store the result in city as >PA?\A',R8/ ?ote that the resulting string is always stored in the left side string variable. )I%+ STR,l<#@) + MHN,TION: strcm < & function is used to compare two character strings. It returns a 9 when two strings are identical ,therwise it returns a numerical value which is the different in ASLII values of the first mismatching character of the strings being compared. char city5(9& @ >0adras>7 char town5(9& @ >0angalore>7 strcmp<city, town&7 This will return an integer value / %9 which is the difference in the ASLII values of the first mismatching letters : and ? - ?ote that the integer value obtained as the difference may be assigned to an integer variable as follows1 int n7 n @ strcmp<city, town&7 READINK N >RITINK STRINKS: .e use scam < & function to read strings which do not have any white spaces. 8"1 seanf<> Es >, city&7 .hen this statement is e"ecuted, the user has to enter the city name

<eg >?8.:8'HI>& without any white space <i.e not li!e >?8. :8'HI>&, The white space in the string will terminate the reading and only >?8.> is assigned to city. ] To read a string with white spaces gets< & function or a loop can be used as. <i& gets< city&7 <ii& do,., while loop i @9 do D Lh @ getchar< & Lity 5i6 @ ch IAA G .hile <chR @ HnFnQ& i // 7 city5 i 6 @ F9 7 The copy or assign a single character to a string variable, the assignment can be written as given below7 city5 i 6 @ ? 7 .hen a string contains more than one character, the assignment is written as strcpy <city, >?8. :8'HI>&7 AT,I< & ZO?LTI,?1 atoi< & function is a L library function which is used to conveli a string of digits to the integer value. char st5 %96 @ >(*%=+> 7 int n7 n @ atoi<st&1 This will assign the integer F alue (* I 7S to tile integer variable n. <%& .rite a L program to count the occurrence of a particular character in the given string. Solu#$on: LonsIder a stnng >0iSSiSSiSSi. L1ount the appearance ofa charactel, say ) . Srogram1 F- program to count a character in a string - I C include2stdio.h3 C include2conio.h3 C include2string.h3 maine & D char st5(96, ch7 int count, l,i7 clrscr< &7 printf<> Fn 8nter the string1>&7 gets<st&7 printf<> Fn which char to be counted =>&7 scanf<> Ec>, Jch&7 I- loop to chec! the occurrence of the character - I % @ strlen<st&7 count @ 97 fore i@,7 i 2 %7 iAA& if<st5 i 6 @ @ ch& count AA7 printf<> Fn the character Ec occurs Ed times>, ch, count&7 getch< &7

G .hen this program is e"ecuted, the user has to enter the string and the character to be counted in the given string Ou#pu#: 8nter the string1 0ISSISSISSI .hich char to be counted^ S The character S occurs * times. <(& .rite a L program to count the number of vowels present in a sentence. Solu#$on Lonsider a sentence >this is a boo!>. Lount the appearance of vowels A8I,O in capital or small letters. @roCram I- program to count vowels - I C include2stdio.h3 C include2conio.h3 C include2string.h3 0aine< & char +t5K96. ch7 int count @ ,. i1 c lrscr< &1 I- loop to read a string - I printf<> F%% 8nter the sentence1 Fn>&7 getB< st& 1* loop to L,.%t the vowels in the string -I fore i@,7 i2strlen<st&7 i*/A& switch<st 5i 6& D Lase HAQ7 Lase H8Q7 Lase HIQ7 Lase H,Q7 Lase HOQ7 Lase HaQ7 Lase He7 Lase HiQ7 Lase HoQ7 Lase HuQ7 count AA7 brea!7 G printf<>Fn Ed vowels are present in the sentence>, count&7 getch< &7 G .hen thiX program is e"ecuted, the user has to enter the sentence. ?ote that gets< & function is used to read the Xentence because the stimg has white spaces between the words. the vowels are counted using a switch statement in a loop . ?ote that a countAA statement is given only once to e"ecute it for the cases in the the swich statement.
8nter the sentence1 This is a boo!

Ou#pu#:

+ vowels are present in the sentence. <)& .rite a L program to test whether a given string is palindrome string. e"plain the wor!ing of program. Solu#$on: Lonsider the string 'IRI'> when it is reversed, it reads again as >'IRI'>. Any string of this !ind is called a palindrome string. ] Zew other palindrome strings are :A:, 0,0, 0A:A0, 0A'AY A'A0 I- program to chec! for palindrome stimg -I # include2sldio.h3 C include2conio.h3 C include2siring.h3 main< & D char +t<(96. r+t<(9&7

G .hen this program is e"ecuted the user has to read a string T, reverse this string, the original string is copied from the last position J written as The two strings are compared using a strcmp < & function which will return a 9 when the original string and the reversed string are identical. Pased on this value the result is printed.

Llrscr< &7 Srintf <HFn 8nter the stringQ& Scanf <YEsN st&7 1* loop to reverse the string -I i@,7 V@strlen<st&/l7 while< V 3@ 9 & D rst5 i6 @ st5V 67 iAA7 M//7 G rst5 i6 @ F9 7 if< strcmp< st,rst &@9& printf<>Fn Es is a palindrome string>, st&7_ else printf<>Fn Es is not a palindrome string>, st&7 getch< &7

Ou#pu#: 8nter the string1 HY:8RAPA: Hyderabad is not a palindrome string. Al#erna#$Ie e#hod: This program can also be written to compare the first character of the string with the last, second with the second last and so on up to the middle of the string as shown. 9%()*+B=K If all the characters are identical. then the string is a palindrome string. ,therwise the loop will he lenninalec%.
)E+.rite a C program to compare strings which are given as input through !eyboard and print the alphabetically greater string.

Solu#$on
The alphabetically greater string is the one whose ASLII value of the first letter is greater than that

of the second string. Lonsider the two character strings. St% = >A'SHA> St( = >P88T N' St( is greater than stl because the ASLII value of P in >P8TA> is greater than that of A in A'SHA . ?ote that when the first letters of the two strings are identical, the second letters of the strings are compared.

NO @ROKRA TO @RINT A!@LAPETI,A!!Y KREATER STRINK - I C include2stdio.h3 C include2conio.h3 C include2stirng.h3 main<& D char st%5(96, st(5(967 clrscr< &7 printf<> Fn 8nter string % 1>&7 scanf< > Es >,stl&7 printf<> Fn 8nter string (1>&7 scanf< > Es >, st(&7 if<strcmp<stl ,st(` 9& printf<>Fn Es is alphabetically greater string>, stl&7 else printf<> Fn Es is alphabetically greater string>, st(&7 getch< &7 G

.hen this program is e"ecuted, the user has to enter the two strings. ?ote that strcmp< & function returns a positive value when string % is greater & a negative value when string ( is greater. Ou#pu#: 8nter string % 1 ALSHA 8nter string ( 1 P8T A P8T A is alphabetically greater string^

<+& .ri te a L program to read an array of names and to sort them in alphabetical order <dictionary&. Solu#$on: Lonsider a list of names stored in a (/:imensional character array. 8ach row can store a name ofa ma"imum length of %9 letters.

Step involved in ananging names in alphabetical order are given below. Step % 1 Read n Step ( 1 'oop to read n names of the list Step ) 1 'oop to arrange the names by comparisoI% Step *1 'oop to print the alTanged list. Step + 1 Stop

SR,\RA0 T, ARRA?\8 ?AIFI8S I? A'SHAP8TILA' ,R:8R. C include2stdio.h3 C include 2comio.h3 Cinclude2string.h3

0aine< & D char names5+965(96, temp5(967 . int n,i,V7 clrscr< &1 printf<> Fn How many names^>&7 scanf<>Ed>, Jn&7 printf<> Fn 8nter the Ed nXllnes one by one F n>,n&7 fore i@,7 i2n7 iAA& scanf<>Es>, names5 i 6&7 I- loop to arrange names in alphabetical order - I Zor< i@,7 i2n/l7 iAA& for <V @iA %7 V2n7 VAA& if<strcmp<names5 i 6, names5 V 6& 3 9& D strcpy< temp, names5 i 6&7 strcpy<names5 i 6, names 5 V 6&7 strcpy<names5 V 6,temp&7 I- loop to print the alphabetical list of names - I printf L Fn names in alphabetical order>&7 fore i@,7 i2n7 iAA& printf<>Fn Es>, names5 i 6&7 getch< &7 G .hen this program is e"ecuted, the user has to first enter the total no of names <n& and then all the names in the list. The names are compared, rearranged and printed in alphabetical order. Ou#pu#: How many names^ * 8nter the * names one by one :88SAa SH8RI? S,?IaA ARO? ?ames in Alphabetical order ARlM? :88SAa SH8RI? S,?IaA <B& .rite a L program to convert a line in the lower case te"t to upper case, Solu#$on Lonsider ASL'I values of lower and upper case letters A / B+ P / BB > ......................., ....b / ;9 a / ;= b / ;K ............................ .# / %(( - ?ote that the difference between the lower and upper case letters <ie. / )(& is used for conversion. I- @ROKRA TO ,ON%ERT !O>ER LAS8 TEST TO H@@ER LAS8 - I C inc%ude2stdio.h3 C include2conio.h3 C include2string.h3

0ain < & D char st<K967 int i7 c%rscr<&7 printf<> F n 8nter a sentence1 F n>&7 gets<st&7 I- loop to convert lower case alphabet to upper case - I fore i@,7 i2strlen<st&7 iAA& if<st< i 6 3@ a JJ st< i& 2@ # & st< i 6 @ st5 i 6 / )(7 printf<> Fn the converted upper case strings Fn Es>, st&7 getch< &7 G OHT@HT: 8nter a sentence1 'ogical thin!ing is a must to learn pr9gramming The converted upper case string is ',\ILA' THI?aI?\ IS A 0OST T, '8AR? SR,\RA0I?\. <=& .rite a L program to count no of lines, words and characters in a given te"t. Solu#$on: A while loop is used to read the te"t. The character c is used to terminate the reading of te"t. @roCram C include2stdio.h3 Cinclude2string.h3 C include2conio.h3 maine < & D char t"t5(+96, ch, st5)967 int ins, wds, chs, i1 c%rscr < &7 printfr %% 8nter the te"t. type c st end M% F%%>&1 i@,1 wbilc<<t"tri/l// /M@ getchar< &&R // c& 7 i//7 st 5 i 6 @ . ,9 7 ins @ wds @ chs @ 97 I - loop to count lines, words J characters in te"t - I i@,7 while<t"t5 i 6R@ c & D switch<t"t5 i 6& D Lase , 1 case R 1 case F% 1 case 1 wds AA7 chs AA7 brea!7 G case ^, 1 case . 1 D wds AA7 chs AA7 brea!7 G default1 chs AA7 brea!7 G

IAA7 G printf<>FnFn no of char <incl.blan!s& @ Ed>, chs&7 printf<>Fn ?o. of words @ Ed>, wds&7 printf<>Fn ?o of lines @ Ed>, Ins&7 getch< &7 8nter the te"t, type c at end .hat is a string^ How do you initiali#e it^ 8"plain with e"ample. .ith e"ample. c ?o of char <inch. Plan!s& @ B) ?o of words @ %( ?o of lines @ ).

G Ou#pu#:

ADDITONA! STRINK LAND!INK MHN,TIONS1 Some L compilers will accept the following string handling f% llctiam1 which available in header files string.h and ctype.h Zunctions1 <i& strupr< &1 to c.onvert all alphabets in a string to upper case letters. 8"1 strupr<> delhi >& @3 > :8'HIN <ii& strlwr< &1 To convert all alphabets in a string to lower case letters. 8"1 strlwr<> LITY>& @3 >city>. <Hi& strrev< &1 To reverse a string 8"1 strrev<> SALHI?>& @3 >?IHLAS> <iv& strncmp< &1 To compare first n characters of two strings. 8"1 m @ stmcmp <> :8'HI>, >:I:AR>, (&7 @3 m@/* )I+ s#rcmp$) +: To compare two strings with case in sensitive <neglecting upper I lower case& 8"1 m@strcmpi<> :8'HI>, > delhi>&7 X m @ , . I$+ s#rnca#)+: To Voin specific number of letters to another string. 8". char s%5%96 @ >?ew>7 char s(5%96 @ >:elhi /*%>7 are

stmcat<s % ,s(,)&7 @3 sl will be >?ew :el>.

Opera#$ons w$#h ,harac#ers: L language also supports operations with characters and these functions are available in the header file >ctype.h>. - ?ote that the value l or positive integer will be returned when a condition is true, and value 9 will be returned when condition is false. Some ctype.h functions are listed below. Zunction1 <i& isupper<&1 To test if a character is a upper case letters. 8"1 . isupper< A &1111 3 % isupper< a &11113 9 isupper< K &11113 9 <ll& islower<& 1 To test if a charcter is a lower case letter. 8"1 islower< n & 11113 % <Hi& isalpha<& 1 To test if a character is an alphabet. 8"1 isalpha< a & 111 13 % isalpha< A &1111 39 <iv& isalnum<& 1 To test if a character is an alphabet or number. 8"1 isalnum< K & 111 13 % isalnum< y &1111 3 % isalnum< / &11113 9 <v& isdicit< & 1 To test if a character is a number. 8"1 isdigit< B &11113 % isdigit< a &11113 9 <vi& is"dh^1it< &1 To test if a character is a He"a decimal number <9/;, A/Z and a/f are He"a decimal digits&

8"1 is"digit< ; & 1111 3 % is"digi t< A & 11113 % is"digit< 0 & 111 13 9 <vii& tolower<&1 To convert an alphabet to lower case letter 8"1 tolower< A & 11113 a <viii& toupper<& 1 To convert an alphabet to upper case letter 8". @OINTERS Tariables are used to held data values during the e"ecution of the program 8nergy variable when declared occupies certain memory locations. In it is possible to access and display the address of the mine location of variable using & operator with variable name. the pointer variable is used to store the, memory address of any variable the pointer is denoted by A . :ef1 A pointer is a memory variable that stores a memory address. Sointer can have any name that is legal for other variable and it is declared In the same fashion li!e other Tariable but it is always denoted by - operator. Mea#ures: Sointers save the memory space. 8"ecution time with pointer is faster because data is manipulated with the address is direct access to memory location, The pointer assigns memory space and it also releases dynamically memory is allocated. @o$n#er declara#$on: Sointers can be declared as1 Int * " 7 float * f7 char * y7 Exulana#$on: ^ In the first st " is an integer and hills ^ The compiler that it holXs the address of any In#elQer Iar$able: The * <indirection operator& is also called the deference operator, .hen a pointer is deference the value at that address stored by the pointer is retriveed .. .hen a pointer is declared that * indicates that it is a pointer variable but not an co/ordinary

variable the % is used with print fn. Zor printing the address of a variable. The address of any var is a wholf no. @rQQ: .ap to display the value and iXs location / using Sointer. Toid main ,. D Int v@%9, * p7 S @$v7 Srintf <>F%% Address of T @ E n>, p&7 Srintf <>F%% value of T @ % a >, * p&7 Srintf <>F%% Address of S @ % n> ,$p&7 G OHT@HT Address of T @ *9B9 Talue of T @ %9 iFudress u r S @/@ //l9B( In the abme prg. F . IS Xm integer variable and its value is %9. The Jt S @ JT Llssigns address of T to S ie S IS the pointer to the variable T to access the address and value of T pt p can be used,. The value of S is nothing.but address of the variable i F& X7/ To display the valuestorecl at that location -S is used. Srg1 . AS to add ( nos. through variables J then pointers. Toid main 9 D int a,b,c,d, -ap, -bp7 Srintf<> enter two nos >&7 scanf <>E d E d>, J a, Jb&7 Ap@Ja7 Pp@Jb7 L@aAb7 A@ -ap A -bp7 Srintf I sum of A J P using variable1 E d 7 c& 7/4 Srintf <> > using pointers7 E d 7 d&7 G ,Ip enter ( no. 1 K * Sub of A J P Tas1 %( Sub of A J P Sointer1 %( Sointer to Zunction1 In c every variable has an address e"cept register variables using pointer1 L function also have an address. .e invo!e the function using its addrX17Vs. Srf. . AS to display address of uses defined function main c& D int show 97 I-Zunction Srototype -% Show < &7 I Zunction call -% printf< > E u >, show&7 %- fn -& address of function %G Show, D printf <> in address of function show < & is 1 >&7 G ,OTSOT1 A.ddress of the show < & is 1 +)9 Sointer and Arravs1 Array name by itself is an address or pointer. It points to the address of the first oth 8lement of an array the 8lements of the array together with this address can be displayed by using array name itself. Array 8lements of the alTay together with their address on be displayed by sing alTay have itself. Array 8lements are always stored in contiguous memory locations. Srg1 . AS to illustrate the above concept. Toid main<