position: fixed; top: auto !important; margin-left: 112px;

strlen function

1) Use of strlen

strlen C function counts the number of characters present in a string and gives us string length.

Example

#include <stdio.h>
#include <string.h>
int main( )
{
    char arr[] = "Manish" ;
    int len1;
    len1 = strlen ( arr ) ;
    printf ("Length of string = %d", len1) ;
    return 0;
}

Output :-

Length of string = 6

Remember while counting the length of a string it does not count '\0'. Program itself is self explanatory. Count should starts with 1. That's why the length of a string is 6.

2) Use of strcpy

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.

Example

#include <stdio.h>
#include <string.h>
int main( )
{
    char source[ ] = "Hello World" ;
    char target[20] ;
    
    /* This function copies the base address of source to target */
    strcpy ( target, source ) ;
    
    printf ( "\nSource String = %s", source ) ;
    printf ( "\nTarget String = %s", target ) ;

    return 0;
}

Output :-

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.

3) Use of strcat

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".

Example

#include lt;stdio.h>
#include <string.h>
int main( )
{
    char source[ ] = "Bombay!" ;
    char target[30] = "Nagpur" ;
    /* This function concatenates the source at the end of target */
    strcat ( target, source ) ;
    printf ( "\nSource string = %s", source ) ;
    printf ( "\nTarget string = %s", target ) ;
    return 0;
}

Output :-

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.

4) Use of strcmp

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.

Example :-

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 ( "\n%d %d %d", i, j, k ) ; 
}

Output :-

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., ‘\0’ minus ‘ ’, which is equal to -32.



Previous Next Article







codzify.com


Largest collection of up-to-date tutorials to learn programming languages. We are focused on easy learning. Massive collection of interview questions one may need for preparation.

Social Profile


Linkedin
Twitter
Facebook

Copyright 2018. All rights reserved.