0% found this document useful (0 votes)
39 views27 pages

Am - En.u4cse20370 Lab 6

The document contains 12 programming problems involving arrays in C language. The problems cover 1D arrays and 2D arrays. Some of the problems involve reading/displaying arrays, finding largest/smallest elements, sorting arrays, searching arrays, performing arithmetic operations on 2D arrays like addition. The document provides sample code solutions for each problem to perform the required task using basic array operations like loops, conditional statements etc.

Uploaded by

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

Am - En.u4cse20370 Lab 6

The document contains 12 programming problems involving arrays in C language. The problems cover 1D arrays and 2D arrays. Some of the problems involve reading/displaying arrays, finding largest/smallest elements, sorting arrays, searching arrays, performing arithmetic operations on 2D arrays like addition. The document provides sample code solutions for each problem to perform the required task using basic array operations like loops, conditional statements etc.

Uploaded by

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

THOTA SAI NAGA MANIKANTA

AM EN U4CSE20370
Lab Assignment 6

1. Write a program which reads in 10 integers from the user and stores them
in an array. Find the largest value in the array and print it.
#include<stdio.h>
int main(){
int int forarr[11]; t=0;
{
(int i=0;i<10;i++)

scanf("%d",&arr[i]);
}
for(inti=0;i<10;i++)
{
if (arr[i]>t){ t = arr[i];
}
}
printf("Largestnumberis%d",t);
}

2. Modify the last program to use a preprocessor constant for the size of the
array and in the test condition of the loop which processes the array.
#include<stdio.h>
#definet10
int main(){
int int for
arr[t]; max=0;
{
(int i=0;i<t;i++)

scanf("%d",&arr[i]);
}
for(inti=0;i<t;i++)
{
if(arr[i]>max){ max=arr[i];
}
}
printf("Largestnumberis%d",max);
}
3. Modify the last program to find mean of n numbers using arrays.

int main(){
int int int for
t=10;
{ arr[t]; max=0;

(int i=0;i<t;i++)

scanf("%d",&arr[i]);
}
for(inti=0;i<t;i++)
{
max += arr[i];
}
max=max/10;
printf("Averagenumberis%d",max);
}

4. Write a program to interchange the largest and the smallest number in the array.
#include<stdio.h>
int main(){
int int intt=10;
int for
{ arr[t]; max=0; min=0;

(int i=0;i<t;i++)

scanf("%d",&arr[i]);
}
for (inti=0;i<t;i++)
{
if (arr[i]>arr[max]){ max = i;
}
if (arr[i]<arr[min]){ min = i;
}
}
int
temp=arr[max];
arr[max]=arr[min];
arr[min]=temp;
printf("largest afterinterchanging%d\n",arr[mi n]); printf("smallestnumbersafterinterchanging%d ",arr[max]);
}
5. Write a program to find the second biggest number using an array of n
numbers.
#include<stdio.h>
int main(){
int int int
arr[11];
for t=0; x=0;
{

(int i=0;i<10;i++)

scanf("%d",&arr[i]);
}
for(inti=0;i<10;i++)
{
if (arr[i]>t){ x=t;
t = arr[i];
}
}
printf("secondnumberis%d",x);
}

6. Write a program to find whether the array of integers contain a duplicate


number. If it’s there print the position of duplicate numbers.
#include<stdio.h>
int main(){
int int forarr[11]; t=0;
{
(int i=0;i< 10;i++)

scanf("%d",&arr[i]);
}
for(inti=0;i< 10;i++)
{
for(intk=i+1; k<10;k++)
{
if(arr[k]==arr[i]){ printf("duplicate
}}}
valueof%disonlocati ons%d\n",arr[i],k);}

7. Write a program which can store 10 integers in an array. Fill the array with
“random” numbers using the library functions rand() instead of reading them
from the user. Find the largest element in the array and print it out.
Each time rand() is called it returns a “random” integer. Use the mod operator
( % ) to get a value in the desired range. For example:
int result; result = rand()
% 1000;

will assign a random value in the range 0 – 999 to the variable


result. Make sure your program contains the line:

#include <stdlib.h>

to include information about the rand() function.

#include<stdio.h>
#include<stdlib.h>
int main(){
int for arr[10];
{ (inti=0;i <10;i++)

arr[i]=rand()%1000;
}
int max=0;
for (inti=0;i <10;i++)
{
if (max<arr[i])
{
max = arr[i];
}
}
printf("largestnumberinrandaomarrayis %d",max);
}

8. Modify the last program so that instead of finding the largest element in the
array, the program sorts the elements of the array into ascending order.

#include<stdio.h>
#include<stdlib.h>
int main(){
int for arr[10];
{ (inti=0; i<10;i++)

arr[i]=rand()%999;
}
intmax=0,tem=0; for(inti=0;
{ i <10;i++)
for(intj=
1; j < 10;j++){
if(arr[j]<arr[j-1]){
tem=arr[j]; arr[j]=arr[j-1]; arr[j-1]=tem;
}}
}
for (int i = 0; i < 10; i++)
{
printf("array %d ",arr[i]);
}}

9. Write a program to find out whether a particular element is in the integer


array using Linear search.
#include<stdio.h>
intmain(){
intarr[]={1,2,3,4,5,6,7,8,9,10};
printf("Enternumbertosearch1-10 "); intt=0; scanf("%d",&t);

for(inti=0;i < 10;i++)


{
if(t==arr[i])
{
printf("\nlocationof %d inarrayis%d",t,i);
}
else{
printf("\noutof
}} range");
}

10. Write a program to find out whether a particular element is in the


integer array using Binary search.

#include<stdio.h>
int bs(intstart,intend,int x,intarr[])
{
if (start<=end)
{
intmid=start+(end-start)/2; if(x==arr[mid])
{
return mid;
}
if(arr[mid]>x)
{
returnbs(start,mid-1,x,arr);
}
if (arr[mid]<x)
{
returnbs(mid+1,end,x,arr);
}
}
else{
return-1;
}
}
intmain(){
int arr[]={1,2,3,4,5,6,7,8,9,10};
int x=0,e=0; e= 9;
printf("Enter number bw 1-10 ");
scanf("%d",&x);
int res = bs(0,e,x,arr);
if (res!=-1)
{
printf("\nFound at %d",res);
}
else{
printf("\nFound at %d",res);
printf("\n not found");
}}

11. Write a program to sort an array of elements using Bubble sort.

#include<stdio.h>
int main(){
int 8}; arr[]={15,12,48,56,12,7,8,78,59,13,4,6,18,49,3
int for
{ tem=0;
(inti=0;i<15; i++)

for(intj=0;j<15;j++){
if(arr[j]<arr[j-1]){ tem=arr[j]; arr[j]=arr[j-1];
arr[j-1]=tem;

}
}}
for (int i=0;i<15;i++)
{
printf("%d",arr[i]);
}
}

12. Write a program to sort an array of elements using Selection sort.

#include<stdio.h>
int main(){
int int for arr[]={15,12,48,56,12,7,8,78,59,13,4,6,18,49,38};
{ m=100; int x=0;
(inti= 0;i<15;i++)

for(intj=i;j<15;j++){
if(m>arr[j]){
m=arr[j]; x=j;
}
}
arr[x]=arr[i]; arr[i]=m; m=100;
}
for (int i = 0; i < 15; i++)
{
printf("%d ",arr[i]);
}
}

13. Read a value k from the user and using k left rotation and right
rotation depending on the user choice Left or Right, print the rotated
value. [Use function for the rotations]

#include <conio.h>

int leftrotate(int *a,int n,int k)


{
int i,j,temp;
for(i=0; i<k; i++)
{
temp=a[0];
for(j=0; j<n-1; j++)
{
a[j]=a[j+1];
}
a[n-1]=temp;
}
}
print(int *a,int n)
{
int i;
for(i=0; i<n; i++)
{
printf("%d ",a[i]);
}
}
int main()
{
int a[100],i,n,j,k,temp;
printf("Enter size of the
array : "); scanf("%d",
&n);
printf("Enter elements in array : ");
for(i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
printf("how many times left rotate : ");
scanf("%d", &k);
leftrotate(a,n,k);
printf("\narray elements after left rotate : ");
print(a,n);
}
14. Read an array of size n and a variable k from user. find all the
pairs of elements in the array which yields a sum as k.

#include <stdio.h>
int main(void){
int nums[10]={1,10,6,12,2,4,5,8,0,-1};
sum;
printf("Enter a number: ");
scanf("%d",&sum);
for(int i=0;i<10;i++){
for(int j=i+1;j<10;j++)
{
if(nums[i]+nums[j]==sum){ printf("(%d,
%d)\n",nums[i],n ums[j]); break;
}}
}
return 0;
}

2-D Array
15. Write a program to read and display a matrix.

#include <stdio.h>
int main()
{
int i=0,j=0,x[100][100],m,n;

printf("Enter the order of the matrix:");


scanf("%d %d",&m,&n); for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("Enter Row %d Column %d:",i,j);
scanf("%d",&x[i][j]);
}
}
printf("The matrix is:\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",x[i][j]);
}
printf("\n");}
return 0;
}
16. Write a program to add two matrices.
#include <stdio.h>
int main()
{
int i,j=0,n;
printf("Enter the order of
matrices:"); scanf("%d",&n);
printf("Enter 1st
matrix:\n"); int x[n][n];
for(i=0;i<n;i++){
for(j=0;j<n;j++){
printf("Enter Row %d Column
%d:",i,j); scanf("%d",&x[i][j]);
}}
printf("The 1st matrix is:\n");
for(i=0;i<n;i++){ for(j=0;j<n;j+
+)
{
printf("%d\t",x[i][j]);
} printf("\n");
}
printf("Enter 2nd
matrix:\n"); int y[n][n];
for(i=0;i<n;i++){
for(j=0;j<n;j++)
{
printf("Enter Row %d Column
%d:",i,j); scanf("%d",&y[i][j]);
}}
printf("The 2nd matrix is:\n");
for(i=0;i<n;i++){ for(j=0;j<n;j+
+){ printf("%d\t",y[i][j]);
}
printf("\n");
}
int sum[n][n];
for(i=0;i<n;i++){
for(j=0;j<n;j++)
{
sum[i][j]=x[i][j]+y[i][j];}
}
printf("The sum of the 2 matrices is:
\n"); for(i=0;i<n;i++)
{
for(j=0;j<n;j++){
printf("%d\t",sum[i][j]);
}
printf("\n");
}
return 0;
}
17. Write a program to find the transpose of a matrix.

#include <stdio.h>
int main()
{
int i=0,j=0,m,n,x[100][100];
printf("Enter the order of the matrix:");
scanf("%d %d",&m,&n);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("Enter Row %d Column %d:",i,j);
scanf("%d",&x[i][j]);}
}
printf("The matrix is:\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",x[i][j]);
}
printf("\n");
}
printf("Matrix after transposing:\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",x[j][i]);
}
printf("\n");
}
return 0;
}

18. Write a program to find the sum of all the elements in a 2D array.

#include <stdio.h>
int main()
{
int i=0,j=0,sum,m,n;
printf("Enter the order of the matrix:");
scanf("%d %d",&m,&n);
int x[100][100];
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("x[%d][%d]:",i,j);
scanf("%d",&x[i][j]);}
}
for(i=0;i<m;i++){ for(j=0;j<n;j++)
{
sum=sum+x[i][j];}
}
printf("Sum of all rows in the array:
%d",sum); return 0;
}

19. Write a program to find the sum of the elements in each row of a 2D array and
print it.

#include <stdio.h>
int main()
{
int i,j,sum=0,p,q; int x[100][100];
printf("Enter the dimensions of the array:");
scanf("%d %d",&p,&q);
printf("Enter %d elements into the
array:\n",p*q); for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
printf("x[%d][%d]:",i,j);
scanf("%d",&x[i][j]);
}
}
printf("The array is:\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
printf("%d\t",x[i][j]);
}
printf("\n");
}
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{sum=sum+x[i][j];
}
printf("Sum of elements of row %d is:
%d\n",i,sum); sum=0;
}
return 0;
}
20. Write a program to fill a square matrix with value 0 on the diagonal, 1 on
the upper right triagle and -1 on the lower left triangle.
#include <stdio.h>
int main()
{
int i=0,j=0,x[100][100],n;
printf("Enter the order of the square
matrix:"); scanf("%d",&n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("Enter Row %d Column
%d:",i,j); scanf("%d",&x[i][j]);
}
}
printf("The matrix
is:\n"); for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",x[i][j]);
}
printf("\n");
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i==j)
x[i][j]=0;
else if(i!=j && i>j) x[i]
[j]=1; else x[i][j]=-1;
}
}
printf("Matrix after
Interchanging:\n"); for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",x[i][j]);
}
printf("\n");
}
return 0;
}
21. Write a program to multiply two matrices.
#include <stdio.h>
int main()
{
int p,q;
printf("Enter order of 1st matrix:");
scanf("%d %d",&p,&q);
printf("Enter elements into 1st
matrix:\n"); int i=0,j=0,x[100][100];
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
printf("Enter Row %d Column
%d:",i,j); scanf("%d",&x[i][j]);
}
}
printf("The 1st matrix is:\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
printf("%d\t",x[i][j]);} printf("\n");
}
int y[100][100];
int a,b;
printf("Enter order of 2nd matrix:");
scanf("%d %d",&a,&b);

printf("Enter elements into 2nd


matrix:\n"); for(i=0;i<a;i++)
{
for(j=0;j<b;j++)
{
printf("Enter Row %d Column
%d:",i,j); scanf("%d",&y[i][j]);
}
}
printf("The 2nd matrix is:\n");
for(i=0;i<a;i++)
{
for(j=0;j<b;j++)
{
printf("%d\t",y[i][j]);
}
printf("\n");
}
if(q!=a)
printf("The entered matrices cannot be
multiplied:\n"); int prod[15][15],k;
int res=0; for(i=0;i<p;i++)
{
for(j=0;j<b;j++)
{
for(k=0;k<a;k++)
{
res=res+x[i][k]*y[k][j];
}
prod[i][j]=res; res=0;
}
}
printf("The product of two matrices is:\n");

for(i=0;i<p;i++){ for(j=0;j<b;j++)
{
printf("%d\t",prod[i][j]);
}
printf("\n");
}
return 0;
}

22. Write a program to find out whether a particular element is in the 2D


integer array and print its row and column value using call by reference.
#include <stdio.h>
int main()
{
int i,j,p,q,n;
int x[10][10];
printf("Enter row-size of the array:");
scanf("%d",&p);
printf("Enter column-size of the array:");
scanf("%d",&q);
printf("Enter %d elements into the
array:\n",p*q); for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
printf("x[%d][%d]:",i,j);
scanf("%d",&x[i][j]);
}
}
printf("Enter the element to be searched in the
array:"); scanf("%d",&n);
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
if (x[i][j]==n)
printf("%d is present at x[%d][%d]\n",n,i,j);}
} return 0;
}
23. Write a program to interchange any two Rows & Columns in the given Matrix.
Sample Output
Enter the order of the matrix
33
Enter co-efficients of the
matrix 3 7 9
248
526
The given matrix
is 3 7 9
248
526
After arranging rows in ascending
order 3 7 9
248
256
After arranging the columns in descending
order 5 7 9
348
226

#include <stdio.h>

void main()
{
static int array1[10][10], array2[10][10]; int i, j, m, n, a, b, c, p, q, r;

printf("Enter the order of the matrix \n");


scanf("%d %d", &m, &n);
printf("Enter the co-efficents of the matrix
\n"); for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
scanf("%d,", &array1[i][j]);
array2[i][j] = array1[i][j];
}
}
printf("Enter the numbers of two rows to be exchanged
\n"); scanf("%d %d", &a, &b);
for (i = 0; i < m; ++i)
{
/* first row has index is 0 */ c = array1[a - 1][i];
array1[a - 1][i] = array1[b - 1][i]; array1[b - 1][i] =
c;
}
printf("Enter the numbers of two columns to be exchanged
\n"); scanf("%d %d", &p, &q);
printf("The given matrix is \n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
printf(" %d", array2[i][j]);
printf("\n");
}
for (i = 0; i < n; ++i)
{
/* first column index is 0 */ r = array2[i][p - 1];
array2[i][p - 1] = array2[i][q - 1]; array2[i][q - 1] =
r;
}
printf("The matix after interchanging the two rows(in the original matrix) \n");

for (i = 0; i < m; ++i)


{
for (j = 0; j < n; ++j)
{
printf(" %d", array1[i][j]);
}
printf("\n");
}
printf("The matix after interchanging the two columns(in the original matrix)
\n"); for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
printf(" %d", array2[i][j]);
printf("\n");
}
}

24. Write a program to Sort Rows of the Matrix in Ascending & Columns in
Descending Order.
Sample Output
Enter the order of the matix
22
Enter the co-efficients of the
matrix 40 30
38 90
The given matrix
is 40 30
38 90

The sum of the main diagonal elements is = 130


#include <stdio.h>
void main()
{
static int array1[10][10], array2[10][10]; int i, j, k, a, m, n;

printf("Enter the order of the matrix \n");


scanf("%d %d", &m, &n);
printf("Enter co-efficients of the matrix
\n"); for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
scanf("%d", &array1[i][j]);
array2[i][j] = array1[i][j];
}
}
printf("The given matrix is
\n"); for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
printf(" %d", array1[i][j]);
}
printf("\n");
}
printf("After arranging rows in ascending
order\n"); for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
for (k =(j + 1); k < n; ++k)
{
if (array1[i][j] > array1[i][k])
{
a = array1[i][j]; array1[i][j] = array1[i][k]; array1[i][k] = a;
}
}
}
}
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
printf(" %d", array1[i][j]);
}
printf("\n");
}
printf("After arranging the columns in descending order
\n"); for (j = 0; j < n; ++j)

{
for (i = 0; i < m; ++i)
{
for (k = i + 1; k < m; ++k)
{
if (array2[i][j] < array2[k][j])
{
a = array2[i][j]; array2[i][j] = array2[k][j]; array2[k][j] = a;
}
}
}
}
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
printf(" %d", array2[i][j]);
}
printf("\n");
}
}

25. Write a program to do the Sum of the Main & Opposite Diagonal Elements
of a MxN Matrix.

The sum of the off diagonal elements is = 68

#include <stdio.h>
void main ()
{

static int array[10][10];

int i, j, m, n, a = 0, sum = 0;

printf("Enetr the order of the matix \n");


scanf("%d %d", &m, &n);

if (m == n )
{

printf("Enter the co-efficients of the


matrix\n"); for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
scanf("%d", &array[i][j]);
}
}
printf("The given matrix is \n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
printf(" %d", array[i][j]);
}
printf("\n");
}

for (i = 0; i < m; ++i)


{
sum = sum + array[i][i]; a = a + array[i][m - i - 1];
}

printf("\nThe sum of the main diagonal elements is = %d\n", sum); printf("The sum of the off diagonal elements is = %d\

else
printf("The given order is not square matrix\n");

}
Strings

Reading Strings
26. If we declare a string by writing char str[50]; Then str can be read
from the user by using three ways:
1.Using scanf() function
2.Using gets() function
3.Using getchar() function repeatedly
Write program to read a string in the above three ways.

CODE 1
#include <stdio.h>
int main()
{
char str[50];printf("Enter your name:"); scanf("%
[^\n]",str);
printf("%s using 'scanf':\n",str);
}
CODE 2
#include <stdio.h>
int main()
{
char str[50];
printf("Enter your full name:");
gets(str);
printf("%s",str);
return 0;
}
CODE 3

#include <stdio.h>
int main()
{
char str[50], ch; int i=0;
printf("Enter a string:
"); ch = getchar ();
while(ch!='\n')
{
str[i] = ch; i++;
ch = getchar();
}
str[i] ='\0';
printf("Using 'getchar': %s",
str); return 0;
}
Writing strings
27. The string can be displayed on screen using three ways:
1.Using printf() function
2.Using puts() function
3.Using putchar()function repeatedly.
Modify the above program to display the string that you read.
CODE 1
#include <stdio.h>
int main()
{
char str[50];
printf(“Enter your name:”);
scanf(“%[^\n]”,str);
printf(“%s”,str);
}
CODE 2
#include<stdio.h>
int main()
{
char str[50];
printf("Enter your name: ");
scanf("%[^\n]",str);
puts(str);
return 0;
}
CODE 3
#include <stdio.h>
int main()
{
char str[50]; int i= 0;
char ch = getchar();
while(ch!='\n')
{
str[i] = ch;
ch = getchar(); i++;
}
str[i] ='\0'; int j = 0;
while(str[j]!='\0')
{
putchar( str[j] );
j++;}
return 0;
}
28. Run the following program and analyze the result. #include<stdio.h> main()
{
char str = “Hello”; printf(“\n
%s”,str); printf(“\n
%s”,&str); printf(“\n
%s”,&str[2]);
}
OUTPUT:
Hello
Hello
Llo
REASON:
* Here, %s in the printf function prints the string until termination
character is reached, which is Hello.
* %s , & str also prints the entire string until the termination character, that is
Hello
* %s,&str[2] prints the string from the index location:2 which is, llo

29. Run the following program and analyze the result. It’s
about the use of width and precision specifications along with
%s .
#include<stdio.h> main()
{
char str[] = “Introduction
to C”; printf(“\n
|%s|”,str); printf(“\n
|%20s|”,str); printf(“\n
|%- 20s|”,str); printf(“\n
|%.4s|”,str); printf(“\n
|%20.4s|”,str); printf(“\n
|%-20.4s|”,str);
}

REASON:

 In 1st printf function |%s| is used so it prints the string between | and |, ie
| Introduction to c|

 In the 2nd prints function |%20s| is used So it leaves 20


spaces including the string. Since the string consists of 17
characters, 3 blank spaces will be leaved before the string.
 In the 3rd print function |-%20s| 20 spaces including the string
will be leaved after the string. Since the string consists of 17
characters, 3 blank spaces will be left after the string.
 In the 4th printf function |%.4s| is used along with the printf
function, hence it prints the first four lines of the string.

 In the 5th printf function |%20.4s| is used, hence it leaves the 20


spaces before the string, including the string. Since the string
consists of only 4 characters, 16 spaces will be left before the
string.

 In the 6th printf function, |%-20.4s| is used, hence it leaves 20


spaces after the string, including the string. Since the string
consists of only 4 characters, 16 blank spaces will be left after
the string.

30. Write a program to find the length of a string. Also use strlen( ) to do the same.

WITHOUT LIBRARY FUNCTION

#include <stdio.h>
int main()
{
char str[100],i; printf("Enter a string:
\n"); scanf("%s",str);
for(i=0; str[i]!='\0'; ++i);
printf("\nLength of input string: %d",i);
return 0;
}

WITH STRLEN()
#include <stdio.h>
#include <string.h>
int main()
{
char a[100]; int length;
printf("Enter a string to calculate its
length\n"); gets(a);
length = strlen(a);
printf("Length of the string = %d\n",
length); return 0;
}

31. Write a program to copy on string to another without using any string
library functions. Do the same operation using strcpy() function in string.h)

WITHOUT LIBRARY FUNCTION


#include<stdio.h>

int main() {
char s1[100], s2[100]; int i;
printf("\nEnter the string :");
gets(s1);
i = 0;
while (s1[i] != '\0') { s2[i] =
s1[i]; i++;
}
s2[i] = '\0';
printf("\nCopied String is %s ",
s2); return (0);
}

WITH STRCPY()
#include<stdio.h>
#include<string.h>

int main() {
char str1[100];
char str2[100];
printf("\nEnter the String 1 : ");
gets(str1);
strcpy(str2, str1);
printf("\nCopied String : %s", str2);
return (0);
}

32. Write a program to convert characters of a string to upper case.


Note: Recall that ASCII code for A-Z varies from 65 to 91 and the
ASCII code for a-z ranges from 97 to 123
#include <stdio.h>
#define MAX_SIZE 100
int main()
{
char str[MAX_SIZE]; int i;
printf("Enter your text : ");
gets(str);
for(i=0; str[i]!='\0'; i++)
{
if(str[i]>='a' && str[i]<='z')
{
str[i] = str[i] - 32;}
}
printf("Uppercase string : %s",str);
return 0;
}
33. Write a program to concatenate two strings. (Do the same operation
using the string library function strcat() and analyze the behavior; you
should include string .h)

Without library function

#include <stdio.h>
int main()
{
char str1[50], str2[50], i, j;
printf("\nEnter first string: ");
scanf("%s",str1);
printf("\nEnter second string: ");
scanf("%s",str2);
for(i=0; str1[i]!='\0'; ++i);
for(j=0; str2[j]!='\0'; ++j, ++i)
{
str1[i]=str2[j];
}
printf("\nOutput: %s",str1);
return 0;
}

With strcat()
#include <stdio.h>
#include <string.h>

int main()
{
char a[100], b[100];
printf("Enter the first
string\n"); gets(a);
printf("Enter the second string\n");
gets(b);
strcat(a,b);
printf("String obtained on concatenation is %s\n",a); return 0;
}
34. Write a program to compare two strings. (Do the same operation using the
string library function strcmp() and analyze the behavior; you should include
string .h)
WITHOUT LIBRARY FUNCTION

#include<stdio.h>

int main() {
char str1[30], str2[30]; int i;
printf("\nEnter two strings :");
gets(str1);
gets(str2);
i = 0;
while (str1[i] == str2[i] && str1[i] !=
'\0') i++;
if (str1[i] > str2[i])
printf("str1 > str2");
else if (str1[i] < str2[i])
printf("str1 < str2");
else
printf("str1 = str2");
return (0);
}

WITH STRCMP()
#include
<stdio.h>
#include<string.h
> int main()
{
char str1[20];
printf("Enter the second string : ");
scanf("%s",str2);
if(value==0)
printf("strings are
same"); else
printf("strings are not same");
return 0;
}

35. Write a program to check whether the entered string is a palindrome.


#include <string.h>

int checkpalindrome(char *s)


{
int i,c=0,n; n=strlen(s); for(i=0;i<n/2;i++)
{
if(s[i]==s[n-i-1]) c++;
}
if(c==i) return 1; else
return 0;
}
int main()
{
char s[1000];
printf("Enter the string: "); gets(s); if(checkpalindrome(s)) printf("string is palindrome"); else
printf("string is not palindrome");
}

You might also like