Lesson 44 of 60 – String Functions in C
73%

String Functions in C

C provides several standard library functions for working with strings. These functions are mainly declared in the <string.h> header file. They can be used to find string length, copy strings, join strings, compare strings, search strings, and perform other string operations.

Note: Include #include <string.h> when using the standard string functions discussed in this lesson.

1. What are String Functions?

String functions are library functions that make it easier to work with null-terminated C strings.

Examples include:

  • strlen() – finds string length
  • strcpy() – copies a string
  • strcat() – joins strings
  • strcmp() – compares strings

2. The string.h Header File

Many standard string functions are declared in the <string.h> header file.

#include <string.h>

Include this header before using these functions.

3. strlen() Function

The strlen() function returns the number of characters in a null-terminated string, excluding the terminating '\0'.

#include <stdio.h>
#include <string.h>

int main()
{
    char name[] = "Rahul";

    printf("%zu", strlen(name));

    return 0;
}

Output:

5

4. strlen() Does Not Count '\0'

The null character marks the end of a C string, but it is not included in the value returned by strlen().

char word[] = "Hello";

printf("%zu", strlen(word));

The result is 5, not 6.

5. strcpy() Function

The strcpy() function copies the source string, including its terminating null character, into the destination array.

char source[] = "Hello";
char destination[20];

strcpy(destination, source);

printf("%s", destination);

The destination must have enough space for the complete string.

6. strcpy() Syntax

The basic syntax is:

strcpy(destination, source);

The contents of source are copied into destination.

The destination should be a writable character array with sufficient capacity.

7. Copying a Student Name

#include <stdio.h>
#include <string.h>

int main()
{
    char student[] = "Amit";
    char name[30];

    strcpy(name, student);

    printf("Student Name: %s", name);

    return 0;
}

This copies the contents of student into the writable array name.

8. strcat() Function

The strcat() function appends the source string to the end of the destination string.

char first[30] = "Hello ";
char second[] = "World";

strcat(first, second);

printf("%s", first);

Output:

Hello World

9. strcat() Syntax

The basic syntax is:

strcat(destination, source);

The source is added after the existing characters in the destination string. The destination must have enough free space for the resulting string.

10. Joining First and Last Name

#include <stdio.h>
#include <string.h>

int main()
{
    char first[50] = "Rahul ";
    char last[] = "Kumar";

    strcat(first, last);

    printf("Full Name: %s", first);

    return 0;
}

The second string is appended to the first string.

11. strcmp() Function

The strcmp() function compares two strings character by character.

int result = strcmp("Hello", "Hello");

If the two strings contain the same characters, the result is zero.

12. Understanding strcmp() Result

The result of strcmp() is interpreted as:

  • 0 → strings are equal
  • Less than 0 → first string compares before second string
  • Greater than 0 → first string compares after second string

The exact nonzero value is implementation-dependent, so normally test whether the result is less than, equal to, or greater than zero.

13. Comparing Two Strings

#include <stdio.h>
#include <string.h>

int main()
{
    char a[] = "Apple";
    char b[] = "Apple";

    if(strcmp(a, b) == 0)
    {
        printf("Strings are equal");
    }
    else
    {
        printf("Strings are different");
    }

    return 0;
}

14. strcmp() is Case-Sensitive

String comparison is case-sensitive.

strcmp("Hello", "hello")

These strings are different because H and h are different characters.

15. strchr() Function

The strchr() function searches for the first occurrence of a character in a string.

#include <string.h>

char word[] = "Computer";

char *result = strchr(word, 'p');

If the character is found, the function returns a pointer to its first occurrence. Otherwise, it returns NULL.

16. strchr() Example

#include <stdio.h>
#include <string.h>

int main()
{
    char word[] = "Computer";

    if(strchr(word, 'p') != NULL)
    {
        printf("Character found");
    }
    else
    {
        printf("Character not found");
    }

    return 0;
}

17. strstr() Function

The strstr() function searches for the first occurrence of a substring inside another string.

char text[] = "I love programming";

char *result = strstr(text, "programming");

If the substring is found, a pointer to its first character is returned. Otherwise, NULL is returned.

18. strstr() Example

#include <stdio.h>
#include <string.h>

int main()
{
    char text[] = "Welcome to C Programming";

    if(strstr(text, "Programming") != NULL)
    {
        printf("Word found");
    }
    else
    {
        printf("Word not found");
    }

    return 0;
}

19. strncpy() Function

The strncpy() function copies up to a specified number of characters from one string to another.

char source[] = "Computer";
char destination[20];

strncpy(destination, source, 8);
destination[8] = '\0';

printf("%s", destination);

Care is required because strncpy() does not always append a null character when the source length is at least the specified count.

20. strncat() Function

The strncat() function appends up to a specified number of characters from the source string.

char first[30] = "Hello ";
char second[] = "Programming";

strncat(first, second, 7);

printf("%s", first);

The destination must have enough space for the resulting string and its terminating null character.

21. memset() Function

The memset() function fills a block of memory with a specified byte value. It is declared in <string.h>.

char name[10];

memset(name, 0, sizeof(name));

It can be useful for initializing character arrays or other memory regions.

22. memcpy() Function

The memcpy() function copies a specified number of bytes from one memory area to another.

char source[] = "Hello";
char destination[10];

memcpy(destination, source, sizeof(source));

printf("%s", destination);

The destination must have enough space for the copied bytes. For overlapping memory regions, use memmove() instead.

23. memmove() Function

The memmove() function copies bytes between memory regions even when the source and destination overlap.

char text[] = "ABCDE";

memmove(text + 1, text, 4);

It is useful when overlapping memory areas must be handled safely.

24. memcmp() Function

The memcmp() function compares a specified number of bytes in two memory areas.

char a[] = "ABC";
char b[] = "ABC";

if(memcmp(a, b, 3) == 0)
{
    printf("Same data");
}

Unlike strcmp(), memcmp() compares a specified number of bytes and does not require null-terminated strings.

25. Common Mistake: Destination Too Small

The destination array must have enough capacity before using functions such as strcpy() or strcat().

char source[] = "Hello";
char destination[3];

strcpy(destination, source);

This is unsafe because the destination does not have enough space for the complete string and its null character.

26. Common Mistake: Comparing with ==

The == operator should not be used to compare the contents of two C strings.

if(a == b)
{
    printf("Same");
}

For string contents, use:

if(strcmp(a, b) == 0)
{
    printf("Same");
}

27. Common Mistake: Forgetting string.h

Functions such as strlen(), strcpy(), strcat(), and strcmp() are declared in <string.h>.

#include <stdio.h>
#include <string.h>

Including the correct header provides the proper declarations for these standard library functions.

28. Complete String Functions Program

#include <stdio.h>
#include <string.h>

int main()
{
    char first[50] = "Hello";
    char second[] = " World";
    char copy[50];

    printf("Length: %zu\n", strlen(first));

    strcpy(copy, first);
    printf("Copied: %s\n", copy);

    strcat(first, second);
    printf("Joined: %s\n", first);

    if(strcmp(copy, "Hello") == 0)
    {
        printf("Copy matches Hello");
    }

    return 0;
}

29. Important String Functions

Function Purpose
strlen() Finds string length
strcpy() Copies a string
strncpy() Copies up to a specified number of characters
strcat() Appends a string
strncat() Appends up to a specified number of characters
strcmp() Compares strings
strchr() Searches for a character
strstr() Searches for a substring
memcpy() Copies bytes
memmove() Moves bytes safely when regions overlap
memset() Fills memory with a byte value
memcmp() Compares bytes

30. Practice Program

Create a C program that:

  1. Takes two strings from the user.
  2. Displays the length of both strings.
  3. Copies the first string into another array.
  4. Compares the two strings.
  5. Joins the two strings.
  6. Searches for a character using strchr().
  7. Searches for a word using strstr().

Use the appropriate functions from <string.h>. Make sure all destination arrays have enough capacity.

📌 Key Points

  • String functions are mainly declared in <string.h>.
  • strlen() finds the number of characters before '\0'.
  • strcpy() copies a string to a destination array.
  • strcat() appends one string to another.
  • strcmp() compares the contents of two strings.
  • strchr() searches for a character.
  • strstr() searches for a substring.
  • strncpy() copies up to a specified number of characters.
  • strncat() appends up to a specified number of characters.
  • memcpy() copies bytes between memory areas.
  • memmove() handles copying between overlapping memory areas.
  • memset() fills a memory area with a byte value.
  • memcmp() compares a specified number of bytes.
  • Always make sure destination arrays have enough capacity.

🧠 Quick Quiz

Question: Which function is used to find the length of a C string?