C String Functions

C provides a set of functions for manipulating and handling strings. These functions are part of the standard C library string.h and allow operations such as copying, concatenating, comparing, and searching strings. A string in C is represented as an array of characters terminated by the null character ('0').

Common String Functions in C

The following are some of the most commonly used string functions in C:

1. strcpy() - Copy String

The strcpy() function copies a string from one location to another. The destination string must be large enough to hold the source string, including the null character.

Syntax:

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

Example:

                        
#include <stdio.h>
#include <string.h>
int main() 
{
    char source[] = "Hello, World!";
    char dest[50];
        
    // Copying string from source to dest
    strcpy(dest, source);
        
    // Displaying the copied string
    printf("Destination String: %s\n", dest);
        
    return 0;
}
                        
                    

Output:

Destination String: Hello, World!

2. strcat() - Concatenate Strings

The strcat() function is used to concatenate (append) one string to the end of another string.

Syntax:

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

Example:

                        
#include <stdio.h>
#include <string.h>
int main() 
{
    char str1[50] = "Hello";
    char str2[] = " World!";
                
    // Concatenating str2 to str1
    strcat(str1, str2);
        
    // Displaying the concatenated string
    printf("Concatenated String: %s\n", str1);
        
    return 0;
}
                        
                    

Output

Concatenated String: Hello World!

3. strlen() - String Length

The strlen() function returns the length of a string (excluding the null character).

Syntax:

                        
size_t strlen(const char *str);
                        
                    

Example:

                        
#include <stdio.h>
#include <string.h>
int main() 
{
    char str[] = "Hello, World!";
                
    // Getting the length of the string
    printf("Length of the string: %zu\n", strlen(str));
        
    return 0;
}
                        
                    

Output:

Length of the string: 13

4. strcmp() - Compare Strings

The strcmp() function compares two strings lexicographically. It returns:

Syntax:

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

Example:

                        
#include <stdio.h>
#include <string.h>
int main() 
{
    char str1[] = "Hello";
    char str2[] = "World";
        
    // Comparing two strings
    int result = strcmp(str1, str2);
        
    if(result == 0) {
        printf("Strings are equal.\n");
    } else if(result < 0) {
        printf("First string is less than the second string.\n");
    } else {
        printf("First string is greater than the second string.\n")\;
    }
        
    return 0;
}
                        
                    

Output:

First string is less than the second string.

5. strchr() - Find Character in String

The strchr() function is used to find the first occurrence of a character in a string.

Syntax:

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

Example:

                        
#include <stdio.h>
#include <string.h>
int main() {
    char str[] = "Hello, World!";
    
    // Finding the first occurrence of 'o'
    char *ptr = strchr(str, 'o');

    if(ptr != NULL) {
        printf("Character 'o' found at position: %ld\n", ptr - str);
    } else {
        printf("Character not found.\n");
    }
    return 0;
}
                        
                    

Output:

Character 'o' found at position: 4

Other Useful String Functions

  • strstr(): Finds the first occurrence of a substring in a string.
  • strtok(): Tokenizes a string into smaller strings (tokens) based on a delimiter.
  • strdup(): Duplicates a string.