String Notes
String Notes
Let's try to understand the matrix multiplication of 2*2 and 3*3 matrices by
the figure given below:
return 0;
}
Output:
Let's try to understand the matrix multiplication of 3*3 and 3*3 matrices by
the figure given below:
Transpose of a matrix.
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.
1 4 0
-5 2 7
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’.
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";
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
int main()
{
return 0;
}
Output
String: GeeksforGeeks
Length: 13
3. strcmp() Function
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";
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
int main()
{
// defining strings
// printing result
printf("Source: %s\n", source);
printf("Destination: %s\n", dest);
return 0;
}
Output
Source: GeeksforGeeks
Destination: GeeksforGeeks