4. strlen()
The strlen() function is used to calculate the length of
the string.
It counts the total number of characters present in the
string which includes alphabets, numbers, and all
special characters including blankspaces.
6. Program
Example 2
#include <stdio.h>
#include <string.h>
int main()
{
char a[50];
printf("Enter a string: ");
scanf("%s", a);
printf("Length of your string is = %d",strlen(a));
printf("n");
}
7. QUIZ - 1
#include <stdio.h>
#include <string.h>
int main()
{
char a[20]="I love Python Programming";
printf("Length of string a = %d",strlen(a));
printf("n");
}
8. The strcat() function is used for string concatenation
in Cprogramming language. It means it joins the two
strings together
strcat()
12. strcpy()
strcpy() function copies a string from a source location
to a destination location and provides a null character
to terminate thestring.
13. Program
Example 5
#include <stdio.h>
#include <string.h>
int main() {
char str1[20] = "C programming";
char str2[20];
// copying str1 to str2
strcpy(str2, str1);
printf("%s",str2); // C programming
return 0;
}
14. Program
Example 6
#include <stdio.h>
#include <string.h>
int main() {
char str1[20];
char str2[20];
printf("Enter a string: ");
scanf("%s", str1);
// copying str1 to str2
strcpy(str2, str1);
printf("The output of str2 is: %s",str2);
return 0;
}
15. strcmp()
strcmp()in C programminglanguageisused to comparetwo strings.
This function return values that are as follows −
• if Return value < 0 then it indicates str1 is less than str2.
• if Return value > 0 then it indicates str2 is less than str1.
• if Return value = 0 then it indicates str1 is equal to str2.
17. Program
Example 7
#include<stdio.h>
#include<string.h>
int main()
{
int ret;
char str1[10] = "awinash";
char str2[10] = "awi";
ret = strcmp(str1, str2);
//str1 > str2 == +
//str1 < str2 == -
//str1 == str2 equal
if(ret > 0) {
printf("str1 is greater than str2");
} else if(ret < 0) {
printf("str1 is less than str2");
} else {
printf("str1 is equal to str2");
}
}
18. QUIZ - 3
#include<stdio.h>
#include<string.h>
int main()
{
int ret;
char str1[10] = "Awinash Goswami - Computer Science Graduate";
char str2[10] = "awi";
ret = strcmp(str1, str2);
//str1 > str2 == +
//str1 < str2 == -
//str1 == str2 equal
if(ret > 0) {
printf("str1 is greater than str2");
} else if(ret < 0) {
printf("str1 is less than str2");
} else {
printf("str1 is equal to str2");
}
}
19. Assignment
Declare two variables of char data type of size 20, take input from
user for initializing the string and perform following functions.
find the length of first string.
Concatenate the two strings.
For help, view slide number 6 and 9.