Connect with the Expert mentors

In-depth knowldege, Right Mentors, Persoanlised experience, Full attention, access to the like minded community of people and more.

Learn More

Article

Strings Functions in C Language - strlen, strcpy,strcmp,strcat functions

4 min 40 sec read

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 ''. 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 ( "
Source String = %s", source ) ;
    printf ( "
Target 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 <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 ( "
Source string = %s", source ) ;
    printf ( "
Target 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 :-

#include <stdio.h> 
#include <string.h> 
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 ) ; 
}

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

 

Related Articles

1) Write C programs to implement i) strcpy(), ii) strlen() library function by yourself

2) Write C program to insert a node after a given node in a linked list

3) How to use %5.2f format specifier in C Programming?

4) Question on C Program referencing the Increment and Decrement Operators concept asked in GATE CSE

5) Article on different storage classes in C.

6) C Tutorial on Calloc() vs malloc() functions is explained in depth in this article.

Try to execute what you have learnt

Easy to use online data structure compiler where you can execute the programs in your favourite programming language.
(C, C++, Python)

Open Compiler

HTML, CSS and Javascript Real time Web Editor

Execute your HTML, CSS and javascript code in real time with the web editor
(HTML, CSS, Bootstrap, Javascript)

Open Web Editor