C String Functions

C String Functions

The C string functions are built-in functions in the C programming language that are used for various operations and manipulations on strings. These functions enable you to work with strings in C and perform tasks such as copying, concatenating, comparing, and finding the length of strings. The <string.h> header file in C contains the declarations and implementations of these string functions, making them readily available for use in your C programs.

Commonly Used String Functions In C

1. strcat() Function

The strcat() function in C is used for string concatenation. It takes two arguments: a destination string and a source string. It appends (concatenates) the characters from the source string to the end of the destination string. The source string is modified, and the destination string is the resulting concatenated string.

Syntax

char* strcat(char* dest, const char* src);

The terminating character at the end of dest  will be replaced by the first character of src.

Parameters

  • dest: Destination string
  • src: Source string

Return value

  • The strcat() function returns a pointer to the dest string.

Example


// C Program to illustrate the strcat function 
#include <stdio.h> 
  
int main() 
{ 
    char dest[50] = "This is an"; 
    char src[50] = " example"; 
  
    printf("dest Before: %s\n", dest); 
  
    // concatenating src at the end of dest 
    strcat(dest, src); 
  
    printf("dest After: %s", dest); 
  
    return 0; 
}

Output

dest Before: This is an
dest After: This is an example

Whereas, In C programming, strncat() will be similar to strcat().

strncat()

The strncat() function is used for string handling and concatenation. It appends, at most, n characters from the source string (src) to the end of the destination string (dest) along with a terminating null-character. This function is a variant of strcat() but allows you to specify the maximum number of characters to append from the source string.

Syntax of strncat()

char* strncat(char* dest, const char* src, size_t n);

N indicates the number of characters to be appended. size_t is an unsigned integral type.

2. strlen() Function

The strlen() function is used to calculate the length of a given string in C. It counts all the characters in the string, including the null character '\0' at the end of the string. The function returns the number of characters in the string up to, but not including, the null character.

Syntax

int strlen(const char *str);

Parameters

  • str– it will indicate the string variable whose length to be estimated.

Return Value

  • strlen() function in C returns the length of the string.

Example


// C program to demonstrate the strlen() function 
#include <stdio.h> 
#include <string.h> 
  
int main() 
{ 
    // Declare and initialize a character array 'str' with 
    // the string " A Skills  Vertex" 
    char str[] = "A Skills Vertex"; 
  
    // Calculate the length of the string using the strlen() 
    // function and store it in the variable 'length' 
    size_t length = strlen(str); 
  
    // Print the length of the string 
    printf("String: %s\n", str); 
  
    printf("Length: %zu\n", length); 
  
    return 0; 
}

Output

String: A Skills Vertex
Length: 13

3. strcmp() Function

The strcmp() function is a built-in library function in C that is used for comparing two strings lexicographically (based on their characters’ ASCII values). It takes two strings as arguments and returns an integer value, which indicates the result of the comparison.

Syntax

int strcmp(const char *str1, const char *str2);

Parameters

  • str1: first string to be compared.
  • str2: second string to be compared.

Return Value

  • If str1 is less than str2, the return value is less than 0.
  • If str1 is greater than str2, the return value is greater than 0.
  • If str1 will be equal to str2, the return value is 0.

Example

// C program to demonstrate the strcmp() function 
#include <stdio.h> 
#include <string.h> 
  
int main() 
{ 
    // Define a string 'str1' and initialize it with "  Skills" 
    char str1[] = '' Skills "; 
    // Define a string 'str2' and initialize it with "Ver" 
    char str2[] = ""; 
    // Define a string 'str3' and initialize it with "Skills" 
    char str3[] = "Skills"; 
  
    // Compare 'str1' and 'str2' using strcmp() function and 
    // store the result in 'result1' 
    int result1 = strcmp(str1, str2); 
    // Compare 'str2' and 'str3' using strcmp() function and 
    // store the result in 'result2' 
    int result2 = strcmp(str2, str3); 
    // Compare 'str1' and 'str1' using strcmp() function and 
    // store the result in 'result3' 
    int result3 = strcmp(str1, str1); 
  
    // Print the result of the comparison between 'str1' and 
    // 'str2' 
printf("Comparison of str1 and str2: %d\n", result1); 
    // Print the result of the comparison between 'str2' and 
    // 'str3' 
    printf("Comparison of str2 and str3: %d\n", result2); 
    // Print the result of the comparison between 'str1' and 
    // 'str1' 
    printf("Comparison of str1 and str1: %d\n", result3); 
  
    return 0; 
}

Output

Comparison of str1 and str2: 1
Comparison of str2 and str3: -1
Comparison of str1 and str1: 0

 strncmp() function will be  similar to strcmp().

strncmp()

The strncmp() function in C performs a lexicographic comparison between the first n characters of two null-terminated strings. It returns an integer value based on the outcome of the comparison:

Syntax

int strncmp(const char* str1, const char* str2, size_t num);

Whereas, num is the number of characters to be compared.

4. strcpy

The strcpy() function in C is used to copy the characters from one string to another. It is a standard library function and is included in the <string.h> header file. This function takes two arguments: the destination string (where the characters will be copied) and the source string (from which the characters will be copied). It copies the characters from the source string to the destination string, replacing the contents of the destination string.

Syntax

char* strcpy(char* dest, const char* src);

Parameters

  • dest: Pointer to the destination array ie,content is to be copied. 
  • src: string has to be copied.

Return Value

  • strcpy() function will return a pointer pointing to the output string.

Example



// C program to illustrate the use of strcpy() 
#include <stdio.h> 
#include <string.h> 
  
int main() 
{ 
    // defining strings 
  
    char source[] = "A Skills vertex"; 
    char dest[20]; 
  
    // Copying the source string to dest 
    strcpy(dest, source); 
  
    // printing result 
    printf("Source: %s\n", source); 
    printf("Destination: %s\n", dest); 
  
    return 0; 
}

Output

Source:  A Skills vertex
Destination: A Skills vertex

strncpy()

The strncpy() function is similar to the strcpy() function, but with a specified limit (n) on the number of characters to copy from the source string. The function ensures that at most n bytes of the source are copied to the destination. The important points to note about strncpy() are as follows:

  • strncpy() copies at most n characters from the source string to the destination string.
  • If there is no null character ('\0') among the first n characters of the source string, the resulting destination string will not be null-terminated.
  • If the length of the source string is less than n, strncpy() writes additional null characters to the destination to ensure that a total of n characters are written.

Syntax

char* strncpy( char* dest, const char* src, size_t n );

 n is the first n characters to be copied from src to dest.

5. strchr()

The strchr() function is a predefined function used for string handling in C. It is used to find the first occurrence of a specified character in a given string. Here’s a correct description:

The strchr() function in C is used to locate the first occurrence of a specified character within a given string. It returns a pointer to the first occurrence of the character or a null pointer if the character is not found in the string.

Syntax

char *strchr(const char *str, int c);
  • str: This parameter specifies the pointer to the null-terminated string in which the search for the character will be performed. It is the string that is examined.
  • ch: This parameter specifies the character to be searched for within the string specified by str. It is passed as its int promotion, which means it is treated as an int, but the function internally converts it back to char when performing the search.

Return Value

  • It returns a pointer to the first occurrence of the character in the string.

Example



#include <stdio.h> 
#include <string.h> 
  
int main() 
{ 
    char str[] = "GeeksforGeeks"; 
    char ch = 'e'; 
  
    // Search for the character 'e' in the string 
    // Use the strchr function to find the first occurrence 
    // of 'e' in the string 
    char* result = strchr(str, ch); 
  
    // Character 'e' is found, calculate the index by 
    // subtracting the result pointer from the str pointer 
    if (result != NULL) { 
        printf("The character '%c' is found at index %ld\n", 
               ch, result - str); 
    } 
    else { 
        printf("The character '%c' is not found in the "
               "string\n", 
               ch); 
    } 
   return 0; 
}

Output

The character 'e' is found at index 1

strrchr() Function

The strrchr() function in C is used to locate the last occurrence of a specified character within a given string. It returns a pointer to the last occurrence of the character or a null pointer if the character is not found in the string.

Syntax

char* strchr(const char *str, int ch);

6. strstr()

The strstr() function in C is used to search for the first occurrence of a specified substring within a given string. It returns a pointer to the first character of the first occurrence of the substring in the string, or a null pointer if the substring is not found.

Syntax

char *strstr (const char *s1, const char *s2);

Parameters

  • s1-main string to be examined.
  • s2– sub-string to be searched in the s1 string.

Return Value

  • If the s2 will be found in s1, this function will be returned to a pointer, and then to the first character of the s2 in s1, else, it will be returned as a null pointer.
  • It will return s1 when the if s2 points to an empty string.

Example

// C program to demonstrate the strstr() function 
#include <stdio.h> 
#include <string.h> 
  
int main() 
{ 
    // Define a string 's1' and initialize it with 
    // "Skill Vertex" 
    char s1[] = "Skill vertex"; 
    // Define a string 's2' and initialize it with "for" 
    char s2[] = "for"; 
    // Declare a pointer 'result' to store the result of 
    // strstr() 
    char* result; 
  
    // Find the first occurrence of 's2' within 's1' using 
    // strstr() function and assign the result to 'result' 
    result = strstr(s1, s2); 
  
    if (result != NULL) { 
        // If 'result' is not NULL, it means the substring 
        // was found, so print it 
        printf("Substring found: %s\n", result); 
    } 
else { 
        // If 'result' is NULL, it means the substring was 
        // not found, so print appropriate message 
        printf("Substring not found.\n"); 
    } 
  
    return 0; 
}

Output

Substring found: Vertex

7. strtok()

The strtok() function will be used to split the string into small tokens based on a set of delimiter characters.

Syntax

char * strtok(char* str, const char *delims);

Parameters

  • str: The string will be tokenized.
  • delims: A set of delimiters

Example

// C program to demonstrate the strtok() function 
#include <stdio.h> 
#include <string.h> 
  
int main() 
{ 
    char str[] = "A,Skill.Vertex"; 
    // Delimiters: space, comma, dot, 
    // exclamation mark 
    const char delimiters[] = ",."; 
  
    // Tokenize the string 
    char* token = strtok(str, delimiters); 
    while (token != NULL) { 
        printf("Token: %s\n", token); 
        token = strtok(NULL, delimiters); 
    } 
  
    return 0; 
}

Output

Token: A 
Token: Skill
Token: Vertex

FAQ- C String Functions

Q1. How many strings function in C?

Ans. There are 9 strings in c
strcat(): Concatenate two strings.
strchr(): Find a character in a string.
strcmp(): Compare two strings.
strlen(): Get the length of a string.
strcpy(): Copy one string to another.
strncpy(): Copy a specified number of characters.
strncat(): Concatenate a specified number of characters.
strncmp(): Compare the first N characters.
strstr(): Find a substring in a string.

Q2. Can we use string function in C?

Ans. C provides several useful string functions for performing operations on strings.

Q3.What is the syntax of string in C?

Ans.The basic syntax for declaring a string in C is: char str_name[size];

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

Leave a Comment