0% found this document useful (0 votes)
66 views

Task 8

The given program demonstrates how to reverse a string without using built-in string functions in C. It takes a string as input from the user, calculates its length, and then uses a for loop to swap the first and last characters, second and second last, and so on to reverse the string. The reversed string is then printed as output without needing functions like strrev().
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views

Task 8

The given program demonstrates how to reverse a string without using built-in string functions in C. It takes a string as input from the user, calculates its length, and then uses a for loop to swap the first and last characters, second and second last, and so on to reverse the string. The reversed string is then printed as output without needing functions like strrev().
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

i) Addition of two matrices

Aim: To write a c program for Addition of Two Matrices


Description:
Matrix Addition possible if and only if both matrices have same rows and columns.
1 2 3 4 5 6 5 7 9
4 5 6 + 7 8 9 = 11 13 15
7 8 0 1 2 3 8 10 3
A + B = SUM
The above matrix we declare in program as
A[3][3] + B[3][3] = SUM[3][3]
For rows and columns we take index varaibles i and j.
If we want give size of the matrices in run time then we have to take two variables one
for rows and one for columns like
Int row,col;

Program:
include <stdio.h>
int main( )
{
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
printf("Enter the number of rows (between 1 and 100): ");
scanf("%d", &r);
printf("Enter the number of columns (between 1 and 100): ");
scanf("%d", &c);
printf("\nEnter elements of 1st 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]);
}
}

printf("Enter elements of 2nd matrix:\n");


for (i = 0; i < r; ++i)
{
for (j = 0; j < c; ++j)
{
printf("Enter element b%d%d: ", i + 1, j + 1);
scanf("%d", &b[i][j]);
}
}

// adding two matrices


for (i = 0; i < r; ++i)
{
for (j = 0; j < c; ++j)
{
sum[i][j] = a[i][j] + b[i][j];
}
}

// printing the result


printf("\nSum of two matrices: \n");
for (i = 0; i < r; ++i)
{
for (j = 0; j < c; ++j)
{
printf("%d ", sum[i][j]);

}
printf("\n");
}
return 0;
}

Output:
Enter elements of 1st matrix:
Enter element a11: 1
Enter element a12: 2
Enter element a13: 3
Enter element a21: 4
Enter element a22: 5
Enter element a23: 6
Enter element a31: 7
Enter element a32: 8
Enter element a33: 9
Enter elements of 2nd matrix:
Enter element b11: 9
Enter element b12: 8
Enter element b13: 7
Enter element b21: 6
Enter element b22: 5
Enter element b23: 4
Enter element b31: 3
Enter element b32: 2
Enter element b33: 1

Sum of two matrices:


10 10 10
10 10 10
10 10 10
Result: The given program addition of two matrices has been successfully executed.
ii) Multiplication two matrices
Aim: Write a C program to multiply two matrices
Description:

We can add, subtract, multiply and divide 2 matrices. To do so, we are taking input from
the user for row number, column number, first matrix elements and second matrix
elements. Then we are performing multiplication on the matrices entered by the user.

In matrix multiplication first matrix one row element is multiplied by second matrix all
column elements.

Let's try to understand the matrix multiplication of 2*2 and 2*3 matrices by the figure
given below:

Program:
#include<stdio.h>
int main()
{
int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;
printf("enter the number of row=");
scanf("%d",&r);
printf("enter the number of column=");
scanf("%d",&c);
printf("enter the first matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter the second matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("multiply of the matrix=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
mul[i][j]=0;
for(k=0;k<c;k++)
{
mul[i][j]+=a[i][k]*b[k][j];
}
}
}
//for printing result
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
return 0;
}

Output:
enter the number of row=3
enter the number of column=3
enter the first matrix element=
1
2
3
4
5
6
1
2
3
enter the second matrix element=
1
2
3
4
5
6
1
2
3
multiply of the matrix=
12 18 24
30 45 60
12 18 24

Result: The given program to multiply two matrices has been executed successfully,
iii) Sort array elements using bubble sort
Aim: Write a C program to sort array of elements using Bubble sort
Description:
Bubble sort works on the repeatedly swapping of adjacent elements until they are not in
the intended order. It is called bubble sort because the movement of array elements is
just like the movement of air bubbles in the water. Bubbles in water rise up to the
surface; similarly, the array elements in bubble sort move to the end in each iteration.

Although it is simple to use, it is primarily used as an educational tool because


the performance of bubble sort is poor in the real world. It is not suitable for large data
sets. The average and worst-case complexity of Bubble sort is O(n2), where n is a
number of items.

Bubble short is majorly used where -


complexity does not matter
simple and shortcode is preferred
Program:
#include <stdio.h>
int main()
{
int array[100], n, c, d, swap;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
{
scanf("%d", &array[c]);
}
for (c = 0 ; c < n - 1; c++)
{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1]) /* For decreasing order use '<' instead of '>' */
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
Output:
Enter number of elements
6
Enter 6 integers
17
32
35
14
56
13
Sorted list in ascending order:
13
14
17
32
35
56
Result: The given Program to sort array of elements using bubble sort has been
executed succesfullly.
iv) Concatenate two strings without built-in functions
Aim: Write a C program to concatenate two strings without built-in functions.
Description:
In the main() function, you have to declare two character array name str1 and str2
having size 25 for both. Then you have to declare two integer variables j and j and
initialized them with value 0. Then the printf() is used to display a message "Enter First
String:". Followed by printf() you have to use the gets() to take the 1st string from the
user. Similarly, another printf() is used to display the message "Enter Second String:"
and using gets() takes the value of str2 from the user.
Now a while loop is implemented which checks whether str1[i] does not equal to
'\0' or not, and increments the value of i by 1. Another while is used after this which
checks whether str2[i] not equals to '\0', if the condition becomes true, the loop will get
executed and where it is given the value of str2[j] gets initialized to str1[i]; Then
increments the value of i and j by 1. And finally, initializes the str1[i] as '\0' i.e. null
character terminator. At last printf() is used to display the value of the concatenated
string.

Program:
#include<stdio.h>
int main( )
{
char str1[25],str2[25];
int i=0,j=0;
printf("\nEnter First String:");
gets(str1);
printf("\nEnter Second String:");
gets(str2);
while(str1[i]!='\0')
i++;
while(str2[j]!='\0')
{
str1[i]=str2[j];
j++;
i++;
}
str1[i]='\0';
printf("\nConcatenated String is %s",str1);
return 0;
}
Output:
Enter First String:shali

Enter Second String:vahan

Concatenated String is shalivahan

Result: The given program to Concatenate two strings without built functions has been
executed successfully.
v) Reverse a string using built-in string functions
Aim: Write a C program to reverse a string using built-in string functions.
Description:
Reversing a string is the technique that reverses or changes the order of a given string
so that the last character of the string becomes the first character of the string.
We have built in function strrev( ) to reverse a string.
Syntax: strrev(stringvariable)

Program:

#include <stdio.h>
#include <string.h>
int main()
{
char str[40]; // declare the size of character string
printf (" \n Enter a string to be reversed: ");
scanf ("%s", str);
//use gets(str) if you want to reverse a line
// use strrev() function to reverse a string
printf (" \n After the reverse of a string: %s ", strrev(str));
return 0;
}

Output:
Enter a string to be reversed: ksrmce

After the reverse of a string: ecmrsk


--------------------------------
Result: The given program reverse a string using built in function has been executed
successfully.
v) Reverse a string using without built-in string functions
Aim: Write a C program to reverse a string without built in string functions.
Description:
Reversing a string is the technique that reverses or changes the order of a given string
so that the last character of the string becomes the first character of the string.

Program:
include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
char string[20],temp;
int i,length;
printf("Enter String : ");
scanf("%s",string);
length=strlen(string)-1;
for(i=0;i<strlen(string)/2;i++)
{
temp=string[i];
string[i]=string[length];
string[length--]=temp;
}
printf("Reverse string :%s",string);
getch();
}

Output:
Enter String : firstyearstudents
Reverse string :stnedutsraeytsrif

Result: The given program to reverse a string without built-in string functions ahs
been executed successfully.
vi) String palindrome or not
Aim: Write a C program to find out given string is palindrome or not.
Description:
To find given string is palindrome or not, we have to do reverse a string . if the reversed
string and given string are same then it will called as Palindrome otherwise not.

Program:
#include<stdio.h>
#include<string.h>
int main()
{
char str[20];
int i, len, temp=0;
int flag = 0;
printf("Enter a string:");
scanf("%s", str);
len = strlen(str);
for(i=0; i < len ; i++)
{
if(str[i] != str[len-i-1])
{
temp = 1;
break;
}
}
if (temp==0)
{
printf("String is a palindrome");
}
else
{
printf("String is not a palindrome");
}
return 0;
}
Output:
Enter a string:ksrmmce
String is not a palindrome
Enter a string:madam
String is a palindrome
--------------------------------
Result: The given Program to find given string is palindrome or not has been executed
succesfullly.

You might also like