Arrays & Strings

49. WRITE A C PROGRAM TO INITIALIZE A SINGLE DIMENSIONAL ARRAY

/*program to initialize a single dimensional array*/
#include<stdio.h>
#include<conio.h>
int main()
{
int a[4]={1,2,3,4};
clrscr();
printf(“zeroth location elemrnt=%d”,a[0]);
printf(“first location elemrnt=%d”,a[1]);
printf(“second location elemrnt=%d”,a[2]);
printf(“third location elemrnt=%d”,a[3]);
getch();
return 0;
}
output
display array elements
zeroth location element=1
first location element=2
second location element=3
third location element=4


50. WRITE A C PROGRAM TO DECLARE SINGLE-DIMENSIONAL ARRAY READ VALUES INTO ARRAY AND PRINT IT.

/* program to declare single dimensional array*/
#include<stdio.h>
#include<conio.h>
int main()
{
int a[5];
clrscr();
printf(“enter the values into array”);
for(i=0;i<5;i++)
{
scanf(“%d”,&a[i]);
}
printf(“the elements of the array elements are: \n”);
for(i=0;i<5;i++)
printf(“a[%d]=%d\n”,a[i]);
getch();
return 0;
}
output
enter the values into array
8
26
14
37
11
the elements in the array are
a[0]=8
a[1]=26
a[2]=14
a[3]=37
a[4]=11


51. WRITE A C PROGRAM TO CALCULATE ADDITION OF ALL ELEMENTS IN ARRAY

/*C program to Calculate Addition of All Elements in Array*/
#include<stdio.h>
int main() {
int i, arr[50], sum, num;

printf("\nEnter no of elements :");
scanf("%d", &num);

//Reading values into Array
printf("\nEnter the values :");
for (i = 0; i < num; i++)
scanf("%d", &arr[i]);

//Computation of total
sum = 0;
for (i = 0; i < num; i++)
sum = sum + arr[i];

//Printing of all elements of array
for (i = 0; i < num; i++)
printf("\na[%d]=%d", i, arr[i]);

//Printing of total
printf("\nSum=%d", sum);

return (0);
}
output

Enter no of elements : 3
Enter the values : 11 22 33
a[0]=11
a[1]=22
a[2]=33
Sum=66
/*C program to Search an elemen2t in Array*/
#include<stdio.h>
int main() {
int a[30], ele, num, i;
printf("\nEnter no of elements :");
scanf("%d", &num);
printf("\nEnter the values :");
for (i = 0; i < num; i++) {
scanf("%d", &a[i]);
}
//Read the element to be searched
printf("\nEnter the elements to be searched :");
scanf("%d", &ele);
//Search starts from the zeroth location
i = 0;
while (i < num && ele != a[i]) {
i++;
}
//If i < num then Match found
if (i < num) {
printf("Number found at the location = %d", i + 1);
} else {
printf("Number not found");
}
return (0);
}
output
Enter no of elements : 5
11 22 33 44 55
Enter the elements to be searched : 44
Number found at the location = 4


52. WRITE A C PROGRAM TO SEARCH AN ELEMENT IN ARRAY

/*C program to Search an element in Array*/
#include<stdio.h>
int main() {
int a[30], ele, num, i;
printf("\nEnter no of elements :");
scanf("%d", &num);
printf("\nEnter the values :");
for (i = 0; i < num; i++) {
scanf("%d", &a[i]);
}
//Read the element to be searched
printf("\nEnter the elements to be searched :");
scanf("%d", &ele);
//Search starts from the zeroth location
i = 0;
while (i < num && ele != a[i]) {
i++;
}
//If i < num then Match found
if (i < num) {
printf("Number found at the location = %d", i + 1);
} else {
printf("Number not found");
}
return (0);
}
output
Enter no of elements : 5
11 22 33 44 55
Enter the elements to be searched : 44
Number found at the location = 4


53. WRITE A C PROGRAM TO MERGE TWO ARRAYS IN C PROGRAMMING

/*C program to Merge Two arrays in C Programming*/
#include<stdio.h>
int main() {
int arr1[30], arr2[30], res[60];
int i, j, k, n1, n2;

printf("\nEnter no of elements in 1st array :");
scanf("%d", &n1);
for (i = 0; i < n1; i++) {
scanf("%d", &arr1[i]);
}

printf("\nEnter no of elements in 2nd array :");
scanf("%d", &n2);
for (i = 0; i < n2; i++) {
scanf("%d", &arr2[i]);
}

i = 0;
j = 0;
k = 0;

// Merging starts
while (i < n1 && j < n2) {
if (arr1[i] <= arr2[j]) {
res[k] = arr1[i];
i++;
k++;
} else {
res[k] = arr2[j];
k++;
j++;
}
}

/* Some elements in array 'arr1' are still remaining
where as the array 'arr2' is exhausted */

while (i < n1) {
res[k] = arr1[i];
i++;
k++;
}

/* Some elements in array 'arr2' are still remaining
where as the array 'arr1' is exhausted */

while (j < n2) {
res[k] = arr2[j];
k++;
j++;
}

//Displaying elements of array 'res'
printf("\nMerged array is :");
for (i = 0; i < n1 + n2; i++)
printf("%d ", res[i]);

return (0);

}
output
Enter no of elements in 1st array : 4
11 22 33 44

Enter no of elements in 2nd array : 3
10 40 80

Merged array is : 10 11 22 33 40 44 80


54. WRITE A C PROGRAM TO COPY ALL ELEMENTS OF AN ARRAY INTO ANOTHER ARRAY

/*C program to Copy all elements of an array into Another array*/
#include<stdio.h>
int main() {
int arr1[30], arr2[30], i, num;
printf("\nEnter no of elements :");
scanf("%d", &num);
//Accepting values into Array
printf("\nEnter the values :");
for (i = 0; i < num; i++) {
scanf("%d", &arr1[i]);
}
/* Copying data from array 'a' to array 'b */
for (i = 0; i < num; i++) {
arr2[i] = arr1[i];
}
//Printing of all elements of array
printf("The copied array is :");
for (i = 0; i < num; i++)
printf("\narr2[%d] = %d", i, arr2[i]);
return (0);
}
output
Enter no of elements : 5
Enter the values : 11 22 33 44 55
The copied array is : 11 22 33 44 55


55. WRITE A C PROGRAM TO APPEND TWO SINGLE DIMENSIONAL ARRAYS

/*C program to append two single dimensional arrays*/
#include<stdio.h>
#include<conio.h>
int main()
{
int a[50],b[50],c[100],n1,n2,i,j;
clrscr();
printf(“how many elements for a array\n”);
scanf(“%d”,&n1);
printf(“enter the values of a”);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
printf(“how many elements for b array\n”);
scanf(“%d”,&n2);
printf(“enter the values of b”);
for(j=0;j<n2;j++)
scanf(“%d”,n2);
printf(“result = ”);
for(i=0;i<n1;i++)
c[i]=a[i];
for(j=0;j<n2;j++)
{
c[i]=b[i];
i++;
}
for(i=0;i<n1+n2;i++)
printf(“%d”,c[i]);
getch();
return 0;
}
output
how many elements for a array2
enter the values of a1
2
how many elements for b array3
enter the values of b3
4
5
result = 1
2
3
4
5


56. WRITE A C PROGRAM FOR SWAPPING OF TWO ARRAYS

/*C program for swapping of two arrays*/
#include<stdio.h>
int main(){
int a[5],b[5],c[5],i;
printf("Enter First array->");
for(i=0;i<5;i++)
scanf("%d",&a[i]);
printf("\nEnter Second array->");
for(i=0;i<5;i++)
scanf("%d",&b[i]);
printf("Arrays before swapping");
printf("\nFirst array->");
for(i=0;i<5;i++){
printf("%d",a[i]);
}
printf("\nSecond array->");
for(i=0;i<5;i++){
printf("%d",b[i]);
}
for(i=0;i<5;i++){
//write any swapping technique
c[i]=a[i];
a[i]=b[i];
b[i]=c[i];
}
printf("\nArrays after swapping");
printf("\nFirst array->");
for(i=0;i<5;i++){
printf("%d",a[i]);
}
printf("\nSecond array->");
for(i=0;i<5;i++){
printf("%d",b[i]);
}
return 0;
}
output
enter first array1
2
3
4
5
enter second array6
7
8
9
10
arrays before swapping:
first array1
2
3
4
5
second array6
7
8
9
10
arrays after swapping
first array
6
7
8
9
10
second array1
2
3
4
5


57. WRITE A C PROGRAM TO DECLARE TWO DIMENSIONAL ARRAY AND READ THE ELEMENTS INTO THE ARRAY. PRINT THE ELEMENT MATRIX

/*program to print the element matrix*/
#include<stdio.h>
#include<conio.h>
int main()
{
int I,j,m,n,a[10][10];
clrscr();
printf(“enter the size of two dimensional array”);
scanf(“%d%d”,&m&n);
printf(“enter the elements\n”);
for(“i=0;i<m;i++”)
{
for(j=0;j<n;j++)
{
scanf(“%d”,a[i][j]);
}
}
printf(“matrix is\n”)
for(“i=0;i<m;i++”)
{
for(j=0;j<n;j++)
{
printf(“%d\t”,a[i][j]);
}
printf(“\n”);
}
getch();
return 0;
}
output
enter the size of two dimensional array
3
3
enter the elements
1
2
3
4
5
6
7
8
9
matrix is 
          1 2 3
          4 5 6 
          7 8 9


58. WRITE A C PROGRAM TO FIND TRACE OF A MATRIX

/* write a program to find trace of a matrix*/
#include<stdio.h>
#include<conio.h>
int main()
{
int a[3][3],I,jsum=0;
clrscr();
printf(“enter a matrix elements\n”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf(“%d”,&a[i][j]);
}
}
printf(“printing a matrix elements”)
printf(“\n”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf(“%d\t”,&a[i][j]);
}
printf(“\n”);
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i==j)
{
sum=sum+a[i][j];
}
}
}
printf(“trace=%d\n”sum);
getch();
return 0;
}
output
enter a matrix elements
1
2
3
4
5
6
7
8
9
printing a matrix elements
 1 2 3
 4 5 6 
 7 8 9 
trace=15


59. WRITE A C PROGRAM TO PRINT UPPER AND LOWER TRIANGLE OF A MATRIX

/*C program to print upper and lower triangle of a matrix*/
#include <stdio.h>
#include <stdlib.h>
int main() {
int **data, i, j, n;
/* Enter the order of the input matrix */
printf("Enter the order of matrix:\n");
scanf("%d", &n);
/* dynamically allocate memory to store matrix elements */
data = (int **)malloc(sizeof (int) * n);
for (i = 0; i < n; i++)
data[i] = (int *)malloc(sizeof (int) * n);
/* get the inputs from the user */
printf("Enter your inputs:\n");
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
scanf("%d", &data[i][j]);
/* print the upper triangular matrix */
printf("Upper triangular matrix:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++)
if (j >= i)
printf("%3d", data[i][j]);
else
printf("%3d", 0);
printf("\n");
}
/* print the lower triangular matrix */
printf("Lower triangular matrix:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++)
if (j <= i)
printf("%3d", data[i][j]);
else
printf("%3d", 0);
printf("\n");
}
return 0;
}
output
Enter the order of matrix:
 3

Enter your inputs:
 10 20 30
 40 50 60
 70 80 90

Upper triangular matrix:
 10 20  30
 0  50  60
 0  0   90

Lower triangular matrix:
 10  0  0
 40 50  0
 70 80 90


60. WRITE A C PROGRAM TO FIND THE MAXIMUM ELEMENT OF A MATRIX

/*C program to find the maximum element of a matrix*/
#include<stdio.h>
#include<conio.h>
int main()
{
int a[3][3],i,j;
clrscr();
printf(“enter a matrix elements\n”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf(“%d”,&a[i][j]);
}
}
printf(“printing a matrix elements”)
printf(“\n”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf(“%d\t”,&a[i][j]);
}
printf(“\n”);
}
max=a[0][0];
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(a[i][j]>max)
}
}
}
printf(“maximum element = %d\n”,max);
getch();
return 0;
}
output
enter a matrix elements 
1
2
3
4
5
6
7
8
9
printing a matrix elements
 1 2 3
 4 5 6 
 7 8 9
maximum element = 9


61. WRITE A C PROGRAM FOR LINEAR SEARCH IN AN ARRAY

/*C program for linear search in an array*/
#include<stdio.h>
#include<conio.h>
int main()
{
int num[10],key,found=0,=0;
clrscr();
printf(“enter the key value\n”);
for(i=0;i<10;i++)
{
scanf(“%d”,num[i]);
}
printf(“enter the key value\n”);
scanf(“%d”,key);
for(i=0;i<10;i++)
if(num[i]==key)
{
found==1;
printf(“% delement found at %d\n”,key);
}
if(found==0);
printf(“the element is not found ”);
getch();
return 0;
}
output
enter the values
1
2
3
4
5
6
7
8
9
enter the key value 5
5 element found at 5 position


62. WRITE A C PROGRAM TO FIND TRANSPOSE OF A MATRIX

/*C program to find transpose of a matrix*/
#include<stdio.h>
#include<conio.h>
int main()
{
int a[10][10], transpose[10][10], r, c, i, j;
printf("Enter rows and columns of matrix: ");
scanf("%d %d", &r, &c);
// Storing elements of the matrix
printf("\nEnter elements of matrix:\n");
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
printf("Enter element a%d%d: ",i+1, j+1);
scanf("%d", &a[i][j]);
}
// Displaying the matrix a[][] */
printf("\nEntered Matrix: \n");
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
printf("%d ", a[i][j]);
if (j == c-1)
printf("\n\n");
}
// Finding the transpose of matrix a
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
transpose[j][i] = a[i][j];
}
// Displaying the transpose of matrix a
printf("\nTranspose of Matrix:\n");
for(i=0; i<c; ++i)
for(j=0; j<r; ++j)
{
printf("%d ",transpose[i][j]);
if(j==r-1)
printf("\n\n");
}
return 0;
}
output
Enter rows and columns of matrix: 2
3

Enter element of matrix:
Enter element a11: 2
Enter element a12: 3
Enter element a13: 4
Enter element a21: 5
Enter element a22: 6
Enter element a23: 4

Entered Matrix: 
2 3 4

5 6 4

Transpose of Matrix:
2 5

3 6

4 4 


63. WRITE A C PROGRAM FOR ADDITION OF TWO MATRICES IN C

/*C program for addition of two matrices in C*/
#include<stdio.h>
int main() {
int i, j, mat1[10][10], mat2[10][10], mat3[10][10];
int row1, col1, row2, col2;
printf("\nEnter the number of Rows of Mat1 : ");
scanf("%d", &row1);
printf("\nEnter the number of Cols of Mat1 : ");
scanf("%d", &col1);

printf("\nEnter the number of Rows of Mat2 : ");
scanf("%d", &row2);
printf("\nEnter the number of Columns of Mat2 : ");
scanf("%d", &col2);

/* Before accepting the Elements Check if no of
rows and columns of both matrices is equal */
if (row1 != row2 || col1 != col2) {
printf("\nOrder of two matrices is not same ");
exit(0);
}

//Accept the Elements in Matrix 1
for (i = 0; i < row1; i++) {
for (j = 0; j < col1; j++) {
printf("Enter the Element a[%d][%d] : ", i, j);
scanf("%d", &mat1[i][j]);
}
}

//Accept the Elements in Matrix 2
for (i = 0; i < row2; i++)
for (j = 0; j < col2; j++) {
printf("Enter the Element b[%d][%d] : ", i, j);
scanf("%d", &mat2[i][j]);
}

//Addition of two matrices
for (i = 0; i < row1; i++)
for (j = 0; j < col1; j++) {
mat3[i][j] = mat1[i][j] + mat2[i][j]; //for substraction use - symbol
}

//Print out the Resultant Matrix
printf("\nThe Addition of two Matrices is : \n");
for (i = 0; i < row1; i++) {
for (j = 0; j < col1; j++) {
printf("%d\t", mat3[i][j]);
}
printf("\n");
}

return (0);
}
output
Enter the number of Rows of Mat1 : 3
Enter the number of Columns of Mat1 : 3

Enter the number of Rows of Mat2 : 3
Enter the number of Columns of Mat2 : 3

Enter the Element a[0][0] : 1
Enter the Element a[0][1] : 2
Enter the Element a[0][2] : 3
Enter the Element a[1][0] : 2
Enter the Element a[1][1] : 1
Enter the Element a[1][2] : 1
Enter the Element a[2][0] : 1
Enter the Element a[2][1] : 2
Enter the Element a[2][2] : 1

Enter the Element b[0][0] : 1
Enter the Element b[0][1] : 2
Enter the Element b[0][2] : 3
Enter the Element b[1][0] : 2
Enter the Element b[1][1] : 1
Enter the Element b[1][2] : 1
Enter the Element b[2][0] : 1
Enter the Element b[2][1] : 2
Enter the Element b[2][2] : 1

The Addition of two Matrices is :
2 4 6
4 2 2
2 4 2


64. WRITE A C PROGRAM TO MULTIPLY TWO MATRICES

/*C program to Multiply Two Matrices*/
#include<stdio.h>
#include<conio.h>
int main()
{
int a[10][10], b[10][10], result[10][10], r1, c1, r2, c2, i, j, k;
printf("Enter rows and column for first matrix: ");
scanf("%d %d", &r1, &c1);
printf("Enter rows and column for second matrix: ");
scanf("%d %d",&r2, &c2);
// Column of first matrix should be equal to column of second matrix and
while (c1 != r2)
{
printf("Error! column of first matrix not equal to row of second.\n\n");
printf("Enter rows and column for first matrix: ");
scanf("%d %d", &r1, &c1);
printf("Enter rows and column for second matrix: ");
scanf("%d %d",&r2, &c2);
}
// Storing elements of first matrix.
printf("\nEnter elements of matrix 1:\n");
for(i=0; i<r1; ++i)
for(j=0; j<c1; ++j)
{
printf("Enter elements a%d%d: ",i+1, j+1);
scanf("%d", &a[i][j]);
}
// Storing elements of second matrix.
printf("\nEnter elements of matrix 2:\n");
for(i=0; i<r2; ++i)
for(j=0; j<c2; ++j)
{
printf("Enter elements b%d%d: ",i+1, j+1);
scanf("%d",&b[i][j]);
}
// Initializing all elements of result matrix to 0
for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j)
{
result[i][j] = 0;
}
// Multiplying matrices a and b and
// storing result in result matrix
for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j)
for(k=0; k<c1; ++k)
{
result[i][j]+=a[i][k]*b[k][j];
}
// Displaying the result
printf("\nOutput Matrix:\n");
for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j)
{
printf("%d ", result[i][j]);
if(j == c2-1)
printf("\n\n");
}
return 0;
}
Output
Enter rows and column for first matrix: 3
2
Enter rows and column for second matrix: 3
2
Error! column of first matrix not equal to row of second.

Enter rows and column for first matrix: 2
3
Enter rows and column for second matrix: 3
2

Enter elements of matrix 1:
Enter elements a11: 3
Enter elements a12: -2
Enter elements a13: 5
Enter elements a21: 3
Enter elements a22: 0
Enter elements a23: 4

Enter elements of matrix 2:
Enter elements b11: 2
Enter elements b12: 3
Enter elements b21: -9
Enter elements b22: 0
Enter elements b31: 0
Enter elements b32: 4

Output Matrix:
24  29
6    25


65. WRITE A C PROGRAM TO READ AND PRINT STRING USING SCANF AND PRINTF

/*C program to read and print string using scanf and printf*/
#include <stdio.h>
#include <conio.h>
int main(){
char inputString[100];
printf("Enter a string\n");
/* Read string from user using scanf and 
store it in inputString char array */
scanf("%s", inputString);
/* Print string stored in inputString using printf */
printf("%s\n", inputString);
getch();
return 0;
}
output
Enter a string
kucet
kucet


66. WRITE A C PROGRAM TO FIND LENGTH OF STRING WITHOUT USING LIBRARY FUNCTION

/*C program to Find Length of String Without using Library Function*/
#include<stdio.h>
int main() {
char str[100];
int length;
printf("\nEnter the String : ");
gets(str);
length = 0; // Initial Length
while (str[length] != '\0')
length++;
printf("\nLength of the String is : %d", length);
return(0);
}
output
enter the string
kucet
the length of the string is : 5


67. WRITE A C PROGRAM TO COPY ONE STRING INTO OTHER WITHOUT USING LIBRARY FUNCTION.

/*C program to Copy One String into Other Without Using 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);
}
output
Enter the string : kucet
Copied String is kucet


68. WRITE A C PROGRAM TO CONCATENATE TWO STRINGS WITHOUT USING LIBRARY FUNCTION

/*C program to concatenate two strings without using library function*/
#include<stdio.h>
#include<conio.h>
void main()
{
char a[10],b[10],c[40];
int i,j;
clrscr();
printf("\n\nENTER FIRST STRING:");
gets(a);
printf("\n\nENTER SECOND STRING:");
gets(b);
for(i=0;a[i]!='\0';i++)
c[i]=a[i];
for(j=0;a[j]!='\0';j++)
{
c[i]=b[j];
i++;
}
c[i]='\0';
printf("\n\nTHE COMBINED STRING IS:");
puts(c);
getch();
}
output
ENTER FIRST STRING:KUCET
ENTER SECOND STRING:_IT
THE COMBINED STRING IS:KUCET_IT
/*C program to compare two strings without using library functions*/
#include<stdio.h>
int stringCompare(char[],char[]);
int main(){
char str1[100],str2[100];
int compare;
printf("Enter first string: ");
scanf(str1);
printf("Enter second string: ");
scanf("%s",str2);
compare = stringCompare(str1,str2);
if(compare == 1)
printf("Both strings are equal.");
else
printf("Both strings are not equal");
return 0;
}
output
Enter first string: c_programming
Enter second string: c_programming
Both strings are equal.


69. WRITE A C PROGRAM TO COMPARE TWO STRINGS WITHOUT USING LIBRARY FUNCTIONS

/*C program to compare two strings without using library functions*/
#include<stdio.h>
int stringCompare(char[],char[]);
int main(){
char str1[100],str2[100];
int compare;
printf("Enter first string: ");
scanf(str1);
printf("Enter second string: ");
scanf("%s",str2);
compare = stringCompare(str1,str2);
if(compare == 1)
printf("Both strings are equal.");
else
printf("Both strings are not equal");
return 0;
}
output
Enter first string: c_programming
Enter second string: c_programming
Both strings are equal.


70. WRITE A C PROGRAM FOR GETS() AND PUTS()

/*C program For gets() and puts()*/
#include<stdio.h>
#include<conio.h>
int main()
{
char str[81];
puts("Enter a line of text:\n");
gets (str);
puts("You entered:\n")
puts(str);
grtch();
return 0;
}
output
enter a line of text:
kucet
you entered
kucet


71. WRITE A C PROGRAM USING STRING HANDLING FUNCTIONSa)strlenb)strcpyc)strcatd)strcmpa) /*C program using string handling functions*/a)strlenb)strcpyc)strcatd)strcmp

a)strlen(to find length of a string)
#include<stdio.h>
#include<conio.h>
int main()
{
char str[20];
int length ;
printf("\nEnter the String : ");
gets(str);
length = strlen(str);
printf("\nLength of String : %d ", length);
getch();
return 0;
}
output
enter the string
kucet
length of string : 5
b) /*C Program to Copy one string into other with using library function*/
#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);
}
output
Enter the String 1 : kucet
Copied String : kucet


c) /*C Program to Concat Two Strings with using Library Function*/
#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; 
}
output
enter the first string
KUCET_
enter the second string
IT
string obtained on catenation is KUCET_IT
d)/*c program to compare two strings*/ 
#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); 
if( strcmp(a,b) == 0 ) 
printf("Entered strings are equal.\n"); 
else 
printf("Entered strings are not equal.\n"); 
return 0; 
}
output
enter the first string
KUCET
enter the second string
KUCET
entered strings are equal


72. WRITE A C PROGRAM USING STRING HANDLING FUNCTIONSa)strlwr b)strupr c)strrev

a) C – strlwr() function
#include<stdio.h>
#include<string.h>
int main()
{
char str[ ] = "MODIFY THIS STRING TO LOWER";
printf("%s\n",strlwr (str));
return 0;
}
output
modify this string to lower
b) C – strupr() function
#include<stdio.h>
#include<string.h>

int main()
{
char str[ ] = "Modify This String To Upper";
printf("%s\n",strupr(str));
return 0;
}
output
MODIFY THIS STRING TO UPPER
c) C – strrev() function
#include<stdio.h>
#include<string.h>

int main()
{
char name[30] = "Hello";

printf("String before strrev( ) : %s\n",name);

printf("String after strrev( ) : %s",strrev(name));

return 0;
}
output
String before strrev( ) : Hello
String after strrev( ) : olleH

Comments