String Functions and Operations in C

C Programming @ Freshers.in

String manipulation is a fundamental aspect of C programming, and mastering string functions and operations is essential. In this article, we will explore various string functions and operations in C, covering their uses, syntax, and providing practical examples with code and output. Understanding and utilizing string functions and operations in C is essential for effective text manipulation in your programs. By mastering these functions, you’ll be well-equipped to work with strings efficiently, split and manipulate text, and perform various other string-related tasks in your C programs.

Common String Functions

1. strlen – String Length

The strlen function calculates the length of a string (the number of characters) excluding the null character ('\0').

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

2. strcpy – String Copy

The strcpy function copies the contents of one string to another.

#include <stdio.h>
#include <string.h>
int main() {
    char source[] = "Copy me!";
    char destination[20];
    strcpy(destination, source);
    printf("Destination: %s\n", destination);
    return 0;
}

3. strcat – String Concatenation

The strcat function appends one string to the end of another.

#include <stdio.h>
#include <string.h>
int main() {
    char greeting[] = "Hello, ";
    char name[] = "Alice";
    strcat(greeting, name);
    printf("Greeting: %s\n", greeting);
    return 0;
}

4. strcmp – String Comparison

The strcmp function compares two strings and returns an integer value indicating their relationship.

#include <stdio.h>
#include <string.h>
int main() {
    char str1[] = "apple";
    char str2[] = "banana";
    int result = strcmp(str1, str2);
    
    if (result < 0) {
        printf("%s comes before %s\n", str1, str2);
    } else if (result > 0) {
        printf("%s comes after %s\n", str1, str2);
    } else {
        printf("%s and %s are the same\n", str1, str2);
    }
    return 0;
}

5. strtok – Tokenizing a String

The strtok function is used to split a string into tokens based on a specified delimiter.

#include <stdio.h>
#include <string.h>
int main() {
    char text[] = "apple,banana,grape";
    char delimiter[] = ",";
    char *token = strtok(text, delimiter);
    
    while (token != NULL) {
        printf("Token: %s\n", token);
        token = strtok(NULL, delimiter);
    }
    
    return 0;
}

Example: Counting Words in a String

Let’s see a practical example where we count the number of words in a string.

#include <stdio.h>
#include <string.h>
int countWords(char text[]) {
    int count = 0;
    char *token = strtok(text, " ");
    
    while (token != NULL) {
        count++;
        token = strtok(NULL, " ");
    }
    return count;
}

int main() {
    char text[] = "This is a sample sentence.";
    int wordCount = countWords(text);
    printf("Number of words: %d\n", wordCount);
    return 0;
}

In this example, we declare a string text and use the countWords function to count the number of words. The output of this program will be:

Number of words: 5
Author: user