Find that Super Coder
within you

More than 26.9 million coders exist in this world. In this competitive world, one has to be really unique with the skills they have. We at codzify.com connect the coders with the world's best professional mentors who help the students to crack the journey of being super coders.

Book your Free Class Today

What we offer

Mobile Apps

Web Apps

Python

C,C++

Data Structures & Algorithms

Data Science

Artifical Intelligence

Article

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

4 min 40 sec read

Author: Manish Methani

1) Use of strlen

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

Example

#include 
#include 
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 
#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;
}

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 
#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;
}

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  
#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 ) ; 
}

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.

 

You may also like to
Read about


1) Write C programs to implement i) strncpy(), ii) strstr() iii) strrchr 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.

C Programming Books

I have picked the list of five best books available on the internet for C programming for beginners. The best 5 picks are given below,

Let Us C: Authentic
Guide To C Programming Language

C Programming Book. Best Book for beginners
Let Us C Solutions
- 17Th Edition

Basic C Programming
for beginners.Yashwant Kanetakar.Paperback
Learn The Fundamentals
Of Data Structures
Through C

Data Structures. Yashwant Kanetakar. Paperback
C Programming: For Beginners
C Programming: For Beginners.
Manish Methani.
Kindle Edition

Testimonials


What students Say About Us?

"It is a great experience in Codzify. We learn to program with hands-on experience. We develop website designs such as Facebook, YouTube, Instagram, etc for better understanding. Our doubts are cleared very well with examples from Manish sir. It is an amazing way of learning to program with Codzify"

Tanish Tejaswi MS

(Class 7 Excel Public School, Mysuru)

"I can now create the Web Pages like Google, Facebook. Currently learning the advanced things of the web development course with Manish sir. I can refer the recorded videos at any time along with the live classes. I enjoy connecting with the technology here. "

Dhairya Kukreja

(Class 5 Podar International School, Nandurbar)

"I want to explore more about the Website and especially the database part. I have created my first Webpage design using HTML and CSS. Really enjoyed. Bootstrap is the next thing I am learning with Manish Sir."

Aarush Methani

(Class 5 Vijaya Convent & Vijaya School For Excellence, Amravati)

Book a call with us!
Our customer support number +91 7719087901
or

Book your Free Class Today
 

More than 26.9 million coders exist in this world. In this competitive world, one has to be really unique with the skills they have. We at codzify.com connect the coders with the world's best professional mentors who help the students to crack the journey of being super coders.

Personlised education in an innovative format.