Open In App

C String Functions

Last Updated : 16 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

C language provides various built-in functions that can be used for various operations and manipulations on strings. These string functions make it easier to perform tasks such as string copy, concatenation, comparison, length, etc. The <string.h> header file contains these string functions.

The below table lists some of the most commonly used string functions in the C language:

Function

Description

Syntax

strlen()

Find the length of a string excluding ‘\0’ NULL character.

strlen(str);

strcpy()

Copies a string from the source to the destination.

strcpy(dest, src);

strncpy()

Copies n characters from source to the destination.

strncpy( dest, src, n );

strcat()

Concatenate one string to the end of another.

strcat(dest, src);

strncat()

Concatenate n characters from the string pointed to by src to the end of the string pointed to by dest.

strncat(dest, src, n);

strcmp()

Compares these two strings lexicographically.

strcmp(s1, s2);

strncmp()

Compares first n characters from the two strings lexicographically.

strncmp(s1, s2, n);

strchr()

Find the first occurrence of a character in a string.

strchr(s, c);

strrchr()

Find the last occurrence of a character in a string.

strchr(s, ch);

 strstr()

First occurrence of a substring in another string.

strstr(s, subS);

sprintf()

Format a string and store it in a string buffer.

sprintf(s, format, …);

strtok()

Split a string into tokens based on specified delimiters.

strtok(s, delim);

Let’s understand each of them with an example.

strlen()

The strlen() function is used to find the length of a string. It returns the number of characters in a string, excluding the null terminator (‘\0’).

Example

C
//Driver Code Starts{
#include <stdio.h>
#include <string.h>

int main() {
//Driver Code Ends }

    char s[] = "Gfg";
  
  	// Finding and printing length of string s
    printf("%lu", strlen(s));

//Driver Code Starts{
    return 0;
}

//Driver Code Ends }

Output
3

strcpy()

The strcpy() function copies a string from the source to the destination. It copies the entire string, including the null terminator.

Example:

C
//Driver Code Starts{
#include <stdio.h>
#include <string.h>

int main() {
//Driver Code Ends }

    char src[] = "Hello";
    char dest[20];
    
    // Copies "Hello" to dest
    strcpy(dest, src);  
    printf("%s", dest);

//Driver Code Starts{
    return 0;
}
//Driver Code Ends }

Output
Hello

strncpy()

The strncpy() function is similar to strcpy(), but it copies at most n bytes from source to destination string. If source is shorter than n, strncpy() adds a null character to destination to ensure n characters are written.

Example:

C
//Driver Code Starts{
#include <stdio.h>
#include <string.h>

int main() {
//Driver Code Ends }

    char src[] = "Hello";
    char dest[20];
    
    // Copies "Hello" to dest
    strncpy(dest, src, 4);
    printf("%s", dest);

//Driver Code Starts{
    return 0;
}
//Driver Code Ends }

Output
Hell

strcat()

The strcat() function is used to concatenate (append) one string to the end of another. It appends the source string to the destination string, replacing the null terminator of the destination with the source string’s content.

Example

C
//Driver Code Starts{
#include <stdio.h>
#include <string.h>

int main() {
//Driver Code Ends }

    char s1[30] = "Hello, ";
    char s2[] = "Geeks!";
    
    // Appends "Geeks!" to "Hello, "
    strcat(s1, s2);  
    printf("%s", s1);

//Driver Code Starts{
    return 0;
}

//Driver Code Ends }

Output
Hello, Geeks!

strncat()

In C, there is a function strncat() similar to strcat(). This function appends not more than n characters from the string pointed to by source to the end of the string pointed to by destination plus a terminating NULL character.

Example:

C
//Driver Code Starts{
#include <stdio.h>
#include <string.h>

int main() {
//Driver Code Ends }

    char s1[30] = "Hello, ";
    char s2[] = "Geeks!";
    
    // Appends "Geeks!" to "Hello, "
    strncat(s1, s2, 4);  
    printf("%s", s1);

//Driver Code Starts{
    return 0;
}
//Driver Code Ends }

Output
Hello, Geek

strcmp()

The strcmp() is a built-in library function in C. This function takes two strings as arguments, compares these two strings lexicographically and returns an integer value as a result of comparison.

Example

C
//Driver Code Starts{
#include <stdio.h>
#include <string.h>

int main() {
//Driver Code Ends }

    char s1[] = "Apple";
    char s2[] = "Applet";
    
  	// Compare two strings 
  	// and print result
    int res = strcmp(s1, s2);
    if (res == 0) 
        printf("s1 and s2 are same");
  	else if (res < 0)
      	printf("s1 is lexicographically " 
      	        "smaller than s2");
  	else
      	printf("s1 is lexicographically " 
      	       "greater than s2");

//Driver Code Starts{
    return 0;
}
//Driver Code Ends }

Output
s1 is lexicographically smaller than s2

strncmp()

This function lexicographically compares the first n characters from the two null-terminated strings and returns an integer based on the outcome.

Example:

C
//Driver Code Starts{
#include <stdio.h>
#include <string.h>

int main() {
//Driver Code Ends }

    char s1[] = "Apple";
    char s2[] = "Applet";
    
  	// Compare two strings upto 
  	// 4 characters and print result
    int res = strncmp(s1, s2, 4);
    if (res == 0) 
        printf("s1 and s2 are same");
  	else if (res < 0)
      	printf("s1 is lexicographically "
      	        "smaller than s2");
  	else
      	printf("s1 is lexicographically "
      	"greater than s2");

//Driver Code Starts{
    return 0;
}

//Driver Code Ends }

Output
s1 and s2 are same

strchr()

The strchr() function is used to find the first occurrence of a given character in a string. If the character is found, it returns a pointer to the first occurrence of the character; otherwise, it returns NULL.

Example:

C
//Driver Code Starts{
#include <stdio.h>
#include <string.h>

//Driver Code Ends }

int main() {
    char s[] = "Hello, World!";
  
  	// Finding the first occurence of 'o' in string s
    char *res = strchr(s, 'o');
    if (res != NULL)
        printf("Character found at: %ld index", res - s);
    else
        printf("Character not found");

//Driver Code Starts{
    return 0;
}
//Driver Code Ends }

Output
Character found at: 4 index

strrchr()

In C, strrchr() function is similar to strchr() function used to find the last occurrence of a given character in a string.

Example:

C
//Driver Code Starts{
#include <stdio.h>
#include <string.h>

int main() {
//Driver Code Ends }

    char s[] = "Hello, World!";
  
  	// Finding the last occurence of 'o' is string s
    char *res = strrchr(s, 'o');
    
    if (res != NULL)
        printf("Character found at: %ld index", res - s);
    else
        printf("Character not found
");

//Driver Code Starts{
    return 0;
}
//Driver Code Ends }

Output
Character found at: 8 index

strstr()

The strstr() function in C is used to search the first occurrence of a substring in another string. If it is not found, it returns a NULL.

Example:

C
//Driver Code Starts{
#include <stdio.h>
#include <string.h>

int main() {
//Driver Code Ends }

    char s[] = "Hello, Geeks!";
  
  	// Find the occurence of "Geeks" in string s
    char *pos = strstr(s, "Geeks");
    
    if (pos != NULL)
        printf("Found"); 
    else
        printf("Not Found");

//Driver Code Starts{
    return 0;
}
//Driver Code Ends }

Output
Found

sprintf()

The sprintf() function is used to format a string and store it in a buffer. It is similar to printf(), but instead of printing the result, it stores it in a string.

Example:

C
//Driver Code Starts{
#include <stdio.h>

int main() {
//Driver Code Ends }

    char s[50];
    int n = 10;
    
    // Output formatted string into string bugger s
    sprintf(s, "The value is %d", n);
    printf("%s", s);

//Driver Code Starts{
    return 0;
}
//Driver Code Ends }

Output
The value is 10

strtok()

The strtok() function is used to split a string into tokens based on specified delimiters. It modifies the original string by replacing delimiters with null characters (‘\0’).

Example:

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

int main() {
    char s[] = "Hello, Geeks, C!";
  
  	// Initializing tokens
    char *t = strtok(s, ", ");

  	// Printing rest of the tokens
    while (t != NULL) {
        printf("%s\n", t);
        t = strtok(NULL, ", ");
    }
    return 0;
}

Output
Hello
Geeks
C!


Next Article
Practice Tags :

Similar Reads