If you have any query feel free to chat us!
Happy Coding! Happy Learning!
strncat()
is a function in C programming language that concatenates (appends) a specified number of characters from one string to the end of another. It is similar to the strcat()
function, but it takes an additional argument, n, which specifies the maximum number of characters to append.
Here is an example of how to use strncat()
:
Copy code
#include <string.h>
int main() {
char str1[20] = "Hello";
char str2[] = ", world!";
strncat(str1, str2, 5); // Append only 5 characters from str2 to str1
printf("str1: %s", str1); // Output: "Hello, wo"
return 0;
}
In this example, the first five characters of the string str2 are appended to the end of str1. The output will be "Hello, wo".
strncmp()
is a function in C that compares a specified number of characters of two strings. It returns an integer value indicating the lexicographic order of the strings. If the first string is lexicographically greater than the second string, it returns a positive value. If the first string is lexicographically less than the second string, it returns a negative value. If the two strings are lexicographically equal, it returns 0.
Here is an example of how to use strncmp()
:
Copy code
#include <string.h>
int main() {
char str1[] = "Hello";
char str2[] = "Hello";
int result = strncmp(str1, str2, 4); // Compare only 4 characters
if (result == 0) {
printf("The strings are equal.");
} else if (result > 0) {
printf("The first string is greater than the second string.");
} else {
printf("The first string is less than the second string.");
}
return 0;
}
In this example, the first 4 characters of str1 and str2 are compared. Since the first 4 characters of both strings are the same, the function returns 0
Comments: 0