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

4 min 40 sec read Basic

1) Use of strlen

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


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.


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


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 :-

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.


How was the tutorial? Nice. Right?

Deep Concept videos to crack the highly-piad interviews.

Chekout out our Youtube Channel to get detailed video content on important topics in interviews.

What is your Interview Score?

Test your skillset with the curated questions created by experts around the globe.

Book a free test slot. Now !.

It usually takes 30 mins for an online test and this test will be MCQ based. Get detailed analytics based on your test and recommedations with personalised roadmaps.

Book a test slot. Now!

Have questions about the trial coding class?

Chat with our experts to discuss

Connect on WhatsApp

Recommended tutorials

#online compiler for c     #python for programming    

#dfs and bfs algorithm     #programming with c language

#storageclass in C    #listcomprehension in python