Download as PPTX, PDF, TXT or read online from Scribd
Download as pptx, pdf, or txt
You are on page 1of 20
UNIT 4 :
ARRAYS AND STRINGS
WRITE A C PROGRAM TO READ , DISPLAY AND ADD THE ELEMENTS OF AN ARRAY . #include<stdio.h> #include<conio.h> void main () { int a[5],I; clrscr(); printf(“enter array elements \n”); for (i=0; i<5;i++) { scanf(“%d”,&a a[i]); } printf(“display the array elements are \n”); for(i=0;i<5;i++) { printf(“%d \n”, a[i]); printf(“\n”); } getch(); } WRITE A C POGRAM TO SORT THE GIVEN NUMBERS OR ARRAY ELEMENTS IN ASCENDING ORDER . //a[5] ={ 5 , 3 , 1 , 2 , 4 } #include<stdio.h> #include<conio.h> void main () { int a [5] I, j ,temp; clrscr(); printf(“enter the array elements \n”); for (i=0;i<5;i++) { scanf(“%d”, &a[i]); } for(i=o; i<5; i++) { for(j=i+1;j<5;j++) { if(a[i]>a[j]); a[i]=a[j]; a[i]=temp; } } } printf(“array elements in ascending order are”); for(i=0;i<5;i++) { printf(“%d”, a[i]); } getch(); } INPUT : ENTER ARRAY ELEMENTS 5 3 1 2 4 OUTPUT : 1 2 3 4 5 WRITE A C PROGRAM TO FIND / DISPLAY THE LARGEST ELEMENT / VALUE STORED IN AN ARRAY . #include<stdio.h> #include<conio.h> void main () { int a[6]={25 , 67 , 88 , 97 , 126 , 55}; int large ,I ; clrscr(); large=a[0]; for(i=0; i<6;i++) { if(a[i]>large) { large =a[i]; } } Printf(“largest elements of an array is %d \n”, large ); getch(); } OUTPUT : Largest element of an array is 126. WRITE A C PROGRAM TO FIND THE SUM OF ALL THE ELEMENTS OF THE GIVEN ARRAY . #include<stdio.h> #include<conio.h> void main () { int a[10], i, sum =0; a[10]={1 , 3 , 5 , 7 , 9 ,11 , 15 , 17 19 , 21}; clrscr(); printf(“sum of al elements of an array “); for(i=o;i<10;i++) { sum =a[i]; } printf(“sum =%d \n “, sum ); printf(“\n”); getch(); } OUTPUT : sum of all elements of an array sum =108 WRITE A C PROGRAM TO FIND THE LENGTH OF THE STRING USING STRLEN() FUNCTION #imclude<stdio.h> #include<string.h> void main() { char str [20]; int len; printf(“enter the string :\n”); gets(str); len= strlen(str); printf(“the length of the string is : %d”, len ); } OUTPUT : Enter the string : C PROGRAMMING The length of the string is : 13 (because it considers space also as one character ) WRITE A C PROGRAM TO CONCATENATION OF TWO STRINGS USING STRCAT () FUNCTION #include<stdio.h> #include<conio.h> void main () { char source [20],target[20]; printf(“enter the source string :\n”); gets(source); printf(“enter the target string :\n”); gets(target); strcat(target , source ); printf(“the source string is : %s \n”, source); printf(“the target string is : %s” , target); strcat (target ,”_one”); printf(“ \n now target string is : %s”, target ); } OUTPUT : Enter the source string : base Enter the target string : data The source string is : base The target string is : database Now target string is :database_one The function strcat() returns a pointer to the first string , hence it can be nested c allows the nesting of strcat function . WRITE A C PROGRAM TO REVERSE THE STRING USING STRREV () FUNCCTION #include<stdio.h> #include<string.h> void main () { char str [20] printf(“ enter string :”); gets(str); strrev(str); printf(“resversed string is : %s”, str); } OUTPUT : Enter string : welcome to hyderabad Reversed string is : dabaredyh ot emoclew WRITE A C PROGRAM TO COPY THE CONTENTS OF ONE STRING INTO ANOTHER STRING USING STRCPY ( ) FUNCTION #include<stdio.h> #include<conio.h> void main () { char source [20],target[20]; printf(“enter the string :”); gets(source); strcpy(target , source ); puts(“\n the target string is :”); puts(target ); } OUTPUT : Enter the string : WELCOME The target string is : WELCOME ASSIGNMENT WRITE A C PROGRAM TO SORT THE GIVEN NUMBERS OR ARRAY ELEMENTS IN DESCENDING ORDER. WRITE A C PROGRAM TO ADD THE GIVEN TWO MATRICES