Nearby lessons

45 of 72

C - String Functions

C provides useful string functions through the string.h header file. Learn how to find string length, copy strings, join strings, and compare strings using simple examples.

Introduction to String Functions

C provides ready-made functions to perform common string operations.

To use these functions, include the string.h header file.

  • strlen() - Finds the length of a string.
  • strcpy() - Copies one string into another.
  • strcat() - Joins two strings.
  • strcmp() - Compares two strings.
Example01
CCode Cell
1#include <stdio.h>
2#include <string.h>
3 
4int main()
5{
6 char name[] = "Rahul";
7 
8 printf("Name: %s\n", name);
9 printf("Length: %zu", strlen(name));
10 
11 return 0;
12}
Output
Name: Rahul
Length: 5

strlen() - Find String Length

The strlen() function counts the characters in a string. It does not count the ending null character.

Example02
CCode Cell
1#include <stdio.h>
2#include <string.h>
3 
4int main()
5{
6 char name[] = "Hello";
7 
8 printf("Length = %zu", strlen(name));
9 
10 return 0;
11}
Output
Length = 5

strcpy() - Copy a String

The strcpy() function copies the contents of one string into another character array.

The destination array must have enough space to store the copied string, including the null character.

Example03
CCode Cell
1#include <stdio.h>
2#include <string.h>
3 
4int main()
5{
6 char first[] = "Hello";
7 char second[20];
8 
9 strcpy(second, first);
10 
11 printf("Copied string: %s", second);
12 
13 return 0;
14}
Output
Copied string: Hello

strcat() - Join Two Strings

The strcat() function adds the second string to the end of the first string.

The first array must have enough space for both strings and the ending null character.

Example04
CCode Cell
1#include <stdio.h>
2#include <string.h>
3 
4int main()
5{
6 char first[30] = "Hello ";
7 char second[] = "Rahul";
8 
9 strcat(first, second);
10 
11 printf("%s", first);
12 
13 return 0;
14}
Output
Hello Rahul

strcmp() - Compare Two Strings

The strcmp() function compares two strings.

  • It returns 0 when both strings are equal.
  • It returns a negative value or a positive value when the strings differ.

Use strcmp() instead of == when comparing string contents.

Example05
CCode Cell
1#include <stdio.h>
2#include <string.h>
3 
4int main()
5{
6 char first[] = "Hello";
7 char second[] = "Hello";
8 
9 if (strcmp(first, second) == 0)
10 {
11 printf("Strings are equal");
12 }
13 else
14 {
15 printf("Strings are different");
16 }
17 
18 return 0;
19}
Output
Strings are equal

Using Multiple String Functions

We can use different string functions in the same program.

Example06
CCode Cell
1#include <stdio.h>
2#include <string.h>
3 
4int main()
5{
6 char first[30] = "Hello";
7 char copy[30];
8 
9 printf("Length: %zu\n", strlen(first));
10 
11 strcpy(copy, first);
12 printf("Copied: %s\n", copy);
13 
14 strcat(first, " World");
15 printf("Joined: %s", first);
16 
17 return 0;
18}
Output
Length: 5
Copied: Hello
Joined: Hello World

Common Mistakes

  • Forgetting to include #include <string.h>.
  • Using a destination array that is too small for strcpy().
  • Using a destination array that is too small for strcat().
  • Using == to compare string contents instead of strcmp().
  • Thinking strlen() counts the ending null character. It does not.
📝 Key Takeaways
  • Include <string.h> to use these string functions.
  • strlen() finds the number of characters in a string.
  • strcpy() copies one string into another array.
  • strcat() joins two strings.
  • strcmp() compares two strings.

🧠 Test Your Knowledge

5 Questions
Progress: 0 / 5