C String Functions

String manipulation in C.

String Manipulation

String manipulation in C refers to the process of working with strings, which are sequences of characters, in various ways to achieve specific tasks. C does not have built-in string data types like some higher-level programming languages, but it provides a set of functions and libraries for working with character arrays to manipulate strings. Let's describe some commonly used built-in functions in string manipulation in C:

strcpy

The 'strcpy' is a standard library function in C that stands for "string copy." It is used to copy the contents of one string (character array) into another.

Here's the syntax & programming example for 'strcpy':

strcpy(char destination, char source);
c Copy Code
#include <stdio.h>
#include <string.h>

int main() {
    char source[] = "Hello, World!";
    char destination[20];

    // Copy the content of source to destination
    strcpy(destination, source);

    printf("Source: %s\n", source);
    printf("Destination: %s\n", destination);

    return 0;
}
Output:
Source: Copy, strings!
Destination: Copy, strings!

* It's important to ensure that the destination string has enough space to accommodate the copied content to avoid buffer overflows and undefined behavior.

strcat

The 'strcat' is a standard library function in the C programming language that is used to concatenate (join) two strings together. The name 'strcat' stands for "string concatenate." It is part of the C Standard Library and is declared in the <string.h> header file.

Here's the syntax & programming example for 'strcat':

strcat(char destination, char source);
c Copy Code
#include <stdio.h>
#include <string.h>

int main() {
    char destination[]="Join, ";
    char source[] = "Strings!";
    
    strcat(destination, source);

    printf("Concatenated string: %s\n", destination);

    return 0;
}
Output:
Concatenated string: Join, Strings!

* It's important to ensure that the destination string has enough space to accommodate the concatenated result to prevent buffer overflows.

strlen

The name 'strlen' stands for "string length", and it's a standard library function in the C programming language that is used to determine the length (number of characters) of a null-terminated string.

Here's the syntax & programming example for 'strlen':

strlen(char str);
c Copy Code
#include <stdio.h>
#include <string.h>

int main() {
    const char str[] = "What is my length?";
    int length = strlen(str);

    printf("Length of the string: %d\n", length);

    return 0;
}
Output:
Length of the string: 18

const: Used to declare a constant, which is essentially a variable whose value cannot be changed after it is initially assigned.

strcmp

The 'strcmp' stands for "string compare", and it's a standard library function that is used to compare two null-terminated strings.

Here's the syntax & programming example for 'strcmp':

strcmp(char str1, char str2);
c Copy Code
#include <stdio.h>
#include <string.h>

int main()
{

        char str1[] = "Ayan";
        char str2[] = "Ayana";

        if (strcmp(str1, str2)==0)
        {
                printf("Both strings are same.");
        }
        else
        {
                printf("Both strings are not same.");
        }

        return 0;
}
Output:
Both strings are not same.

* The 'strcmp' function returns an integer value that indicates whether the strings are equal, and if not, how they differ.

int val = strcmp(str1, str2);

strstr

The 'strstr' is a standard library function that is used to find the first occurrence of a substring (a sequence of characters) within a larger string.

Here's the syntax & programming example for 'strstr':

strstr(char data, char find);
c Copy Code
#include <stdio.h>
#include <string.h>

 int main()
 {
 
 char data[] = "www.techbaz.org";
 char find[] = "techbaz";
 
 if (strstr(data, find))
 {
 printf("String Found.");
 }
 else
 {
 printf("String Not Found.");
 }
 
 return 0;
}
Output:
String Found.

* Keep in mind that 'strstr' is case-sensitive.

strtok

The 'strtok' function is used to tokenize (split) a string into smaller tokens based on a specified set of delimiter characters.

Here's the syntax & programming example for 'strtok':

strtok(char str, char delimiter);
c Copy Code
#include <stdio.h>
#include <string.h>

int main()
{
    char input[] = "Desktop,Laptop,Mobile";
    const char *delimiter = ",";

    // Tokenize the input string using strtok
    char *token = strtok(input, delimiter);

    while (token != NULL)
    {
        printf("Token: %s\n", token);
        token = strtok(NULL, delimiter);
    }

    return 0;
}
Output:
Token: Desktop
Token: Laptop
Token: Mobile

* Be cautious when using 'strtok' in a multithreaded environment, as it is not thread-safe.

What's Next?

We've now entered the finance section on this platform, where you can enhance your financial literacy.