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

String Notes

The document discusses matrix multiplication in C. It provides code to multiply two matrices by taking input from the user and looping through the rows and columns. It also discusses other string handling functions in C like strcat(), strlen(), and strcmp().

Uploaded by

mirajb004
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

String Notes

The document discusses matrix multiplication in C. It provides code to multiply two matrices by taking input from the user and looping through the rows and columns. It also discusses other string handling functions in C like strcat(), strlen(), and strcmp().

Uploaded by

mirajb004
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Matrix Multiplication

Matrix multiplication in C: 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 3*3 matrices by
the figure given below:

Let's see the program of matrix multiplication in C.


#include<stdio.h>
#include<stdlib.h>
int main(){
int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;
system("cls");
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=
111
222
333
enter the second matrix element=
111
222
333
multiply of the matrix=
666
12 12 12
18 18 18

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

The transpose of a matrix is a new matrix that is obtained by


exchanging the rows and columns.

In this program, the user is asked to enter the number of rows r and
columns c. Their values should be less than 10 in this program.

Then, the user is asked to enter the elements of the matrix (of
order r*c).

The program below then computes the transpose of the matrix and
prints it on the screen.

Program to Find the Transpose of a Matrix


#include <stdio.h>
int main() {
int a[10][10], transpose[10][10], r, c;
printf("Enter rows and columns: ");
scanf("%d %d", &r, &c);

// asssigning elements to the matrix


printf("\nEnter matrix elements:\n");
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}

// printing the matrix a[][]


printf("\nEntered matrix: \n");
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j) {
printf("%d ", a[i][j]);
if (j == c - 1)
printf("\n");
}

// computing the transpose


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

// printing the transpose


printf("\nTranspose of the matrix:\n");
for (int i = 0; i < c; ++i)
for (int j = 0; j < r; ++j) {
printf("%d ", transpose[i][j]);
if (j == r - 1)
printf("\n");
}
return 0;
}
Output :

Enter rows and columns: 2

Enter matrix elements:

Enter element a11: 1

Enter element a12: 4

Enter element a13: 0

Enter element a21: -5

Enter element a22: 2

Enter element a23: 7


Entered matrix:

1 4 0

-5 2 7

Transpose of the matrix:

1 -5

4 2

0 7

String in c:
A String in C programming is a sequence of characters terminated with a
null character ‘\0’. The C String is stored as an array of characters. The
difference between a character array and a C string is the string is
terminated with a unique character ‘\0’.

C String Declaration Syntax


Declaring a string in C is as simple as declaring a one-dimensional array.
Below is the basic syntax for declaring a string.
char string_name [size ];
In the above syntax str_name is any name given to the string variable and
size is used to define the length of the string, i.e the number of characters
strings will store.
There is an extra terminating character which is the Null character (‘\0’)
used to indicate the termination of a string that differs strings from
normal character arrays .
C String Initialization
A string in C can be initialized in different ways. We will explain this with
the help of an example. Below are the examples to declare a string with
the name str and initialize it with “GeeksforGeeks”.

Ways to Initialize a String in C

We can initialize a C string in 4 different ways which are as follows:


1. Assigning a string literal without size
String literals can be assigned without size. Here, the name of the string
str acts as a pointer because it is an array.
char str[] = "GeeksforGeeks";
2. Assigning a string literal with a predefined size
String literals can be assigned with a predefined size. But we should
always account for one extra space which will be assigned to the null
character. If we want to store a string of size n then we should always
declare a string with a size equal to or greater than n+1.
char str[50] = "GeeksforGeeks";
3. Assigning character by character with size
We can also assign a string character by character. But we should
remember to set the end character as ‘\0’ which is a null character.
char str[14] = { 'G','e','e','k','s','f','o','r','G','e','e','k','s','\0'};
4. Assigning character by character without size
We can assign character by character without size with the NULL
character at the end. The size of the string is determined by the compiler
automatically.
char str[] = { 'G','e','e','k','s','f','o','r','G','e','e','k','s','\0'};

C String handeling Functions


The C string functions are built-in functions that can be used for various
operations and manipulations on strings. These string functions can be
used to perform tasks such as string copy, concatenation, comparison,
length, etc. The <string.h> header file contains these string functions.
In this article, we will discuss some of the most commonly used String
functions in the C programming language.

String Functions in C
Some of the commonly used string functions in C are as follows:

1. strcat() Function
The strcat() function in C is used for string concatenation. It will append a
copy of the source string to the end of the destination string.

Syntax
char* strcat(char* dest , const char* src );
The terminating character at the end of dest is replaced by the first
character of src.
Parameters
 dest: Destination string
 src: Source string
Return value
 The strcat() function returns a pointer to the dest string.
Example
// C Program to illustrate the strcat function
#include <stdio.h>

int main()
{
char dest[50] = "This is an";
char src[50] = " example";

printf("dest Before: %s\n", dest);

// concatenating src at the end of dest


strcat(dest, src);

printf("dest After: %s", dest);

return 0;
}
Output
dest Before: This is an
dest After: This is an example
In C, there is a function strncat() similar to strcat().
1) strncat()
This function is used for string handling. This function appends not more
than n characters from the string pointed to by src to the end of the string
pointed to by dest plus a terminating Null-character.
Syntax of strncat()
char* strncat(char* dest , const char* src , size_t n );
where n represents the maximum number of characters to be appended.
size_t is an unsigned integral type.
2. strlen() Function

The strlen() function calculates the length of a given string. It doesn’t


count the null character ‘\0’.
Syntax
int strlen(const char *str );
Parameters
 str: It represents the string variable whose length we have to find.
Return Value
 strlen() function in C returns the length of the string.
Example
// C program to demonstrate the strlen() function
#include <stdio.h>
#include <string.h>

int main()
{

char str[] = "GeeksforGeeks";

// Calculate the length of the string using the strlen()


// function and store it in the variable 'length'
size_t length = strlen(str);

// Print the length of the string


printf("String: %s\n", str);

printf("Length: %zu\n", length);

return 0;
}
Output
String: GeeksforGeeks
Length: 13

3. strcmp() Function

The strcmp() is a built-in library function in C. This function takes two


strings as arguments and compares these two strings lexicographically.
Syntax
int strcmp(const char *str1 , const char *str2 );
Parameters
 str1: This is the first string to be compared.
 str2: This is the second string to be compared.
Return Value
 If str1 is less than str2, the return value is less than 0.
 If str1 is greater than str2, the return value is greater than 0.
 If str1 is equal to str2, the return value is 0.
Example
// C program to demonstrate the strcmp() function
#include <stdio.h>
#include <string.h>

int main()
{
// Define a string 'str1' and initialize it with "Geeks"
char str1[] = "Geeks";
// Define a string 'str2' and initialize it with "For"
char str2[] = "For";
// Define a string 'str3' and initialize it with "Geeks"
char str3[] = "Geeks";

// Compare 'str1' and 'str2' using strcmp() function and


// store the result in 'result1'
int result1 = strcmp(str1, str2);
// Compare 'str2' and 'str3' using strcmp() function and
// store the result in 'result2'
int result2 = strcmp(str2, str3);
// Compare 'str1' and 'str1' using strcmp() function and
// store the result in 'result3'
int result3 = strcmp(str1, str1);

// Print the result of the comparison between 'str1' and


// 'str2'
printf("Comparison of str1 and str2: %d\n", result1);
// Print the result of the comparison between 'str2' and
// 'str3'
printf("Comparison of str2 and str3: %d\n", result2);
// Print the result of the comparison between 'str1' and
// 'str1'
printf("Comparison of str1 and str1: %d\n", result3);

return 0;
}

Output
Comparison of str1 and str2: 1
Comparison of str2 and str3: -1
Comparison of str1 and str1: 0
There is a function strncmp() similar to strcmp().
strncmp()
This function lexicographically compares the first n characters from the
two null-terminated strings and returns an integer based on the outcome.
Syntax
int strncmp(const char* str1 , const char* str2 , size_t num );
Where num is the number of characters to compare.

4. strcpy

The strcpy() is a standard library function in C and is used to copy one


string to another. In C, it is present in <string.h> header file.
Syntax
char* strcpy(char* dest , const char* src );
Parameters
 dest: Pointer to the destination array where the content is to be
copied.
 src: string which will be copied.
Return Value
 strcpy() function returns a pointer pointing to the output string.
Example
// C program to illustrate the use of strcpy()
#include <stdio.h>
#include <string.h>

int main()
{
// defining strings

char source[] = "GeeksforGeeks";


char dest[20];

// Copying the source string to dest


strcpy(dest, source);

// printing result
printf("Source: %s\n", source);
printf("Destination: %s\n", dest);

return 0;
}
Output
Source: GeeksforGeeks
Destination: GeeksforGeeks

You might also like