strlen C function counts the number of characters present in a string and gives us string length.
#include #include int main( ) { char arr[] = "Manish" ; int len1; len1 = strlen ( arr ) ; printf ("Length of string = %d", len1) ; return 0; }
Length of string = 6
Remember while counting the length of a string it does not count ''. Program itself is self explanatory. Count should starts with 1. That's why the length of a string is 6.
strcpy C function copies the contents of one string into another. The base addresses of the source and target strings should be supplied to this function.
#include #include int main() { char source[] = "Hello World" ; char target[20] ; /* This function copies the base address of source to target */ strcpy (target, source) ; printf ("Source String = %s", source) ; printf ("Target String = %s", target) ; return 0; }
Source String = Hello World Target String = Hello World
strcpy(target,source) takes two arguments . The string to be copied and where it should be copied. Here source[] character array is being copied into target[] array.
strcat C function concatenates the source string at the end of the target string. For example, "Manish" and "Methani" on concatenation would result into a string "ManishMethani".
#include #include int main() { char source[] = "Bombay!" ; char target[30] = "Nagpur" ; /* This function concatenates the source at the end of target */ strcat (target, source); printf ("Source string = %s", source) ; printf ("Target string = %s", target ) ; return 0; }
Source String = Bombay Target String = NagpurBombay
This function concatenates the source at the end of target . Target string is Nagpur in this case. Source String is Bombay. So strcat function will concatenate Bombay to Nagpur.
strcmp C function is used to compares two strings to find out whether they are same or different. The two strings are compared character by character until there is a mismatch or end of one of the strings is reached, whichever occurs first.
1) If the two strings are identical, strcmp( ) returns a value zero.
2) If they’re not, it returns the numeric difference between the ASCII values of the first non-matching pairs of characters.
#include #include void main() char string1[] = "Jerry" ; char string2[] = "Ferry" ; int i,j,k; i = strcmp (string1, "Jerry") ; j = strcmp (string1, string2) ; k = strcmp (string1, "Jerry boy") ; printf ("%d %d %d", i, j, k ) ; }
0 4 -32
In the first call to strcmp( ), the two strings are identical—“Jerry” and “Jerry”—and the value returned by strcmp( ) is zero. In the second call, the first character of “Jerry” doesn't match with the first character of “Ferry” and the result is 4, which is the numeric difference between ASCII value of ‘J’ and ASCII value of ‘F’. In the third call to strcmp( ) “Jerry” doesn’t match with “Jerry boy”, because the null character at the end of “Jerry” doesn’t match the blank in “Jerry boy”. The value returned is -32, which is the value of null character minus the ASCII value of space, i.e., ‘’ minus ‘ ’, which is equal to -32.
Unlock learning opportunities with our expert-led coding courses.
Real-time doubt solving in a live online coding class is also available in the course
that will help you to stay connected with an experts while learning.