String Handling Functions implementation in C Language

Introduction

In C programming, strings are fundamental for working with textual data. Unlike some other languages, C handles strings using character arrays with a null terminator. String handling in the C programming language typically involves the use of character arrays (strings) and a set of functions provided by the standard C library (<string.h>). A C string is essentially a one-dimensional character array. Each character in the string occupies an element in the array. The key difference is a special character '\0' (null character) at the end of the string. This null character signifies the string's termination.

Declaring C Strings

char name[50]; Here, the name can hold up to 49 characters (excluding the null terminator).

Input and Output in String Handling

Strings can be read from the standard input (keyboard) or written to the standard output (console) using scanf() and printf() functions respectively, or their variants like gets() and puts() as well.

For example

char str[100];
printf("Enter a string: ");
scanf("%s", str); // Input a string
printf("You entered: %s\n", str); // Output a string

Consider a Program in C Language to implement all String Functions.

Source Code

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

// Function prototypes
int shikhaT_strlen(const char *str);
void shikhaT_strcpy(char *dest, const char *src);
void shikhaT_strcat(char *dest, const char *src);
int shikhaT_strcmp(const char *str1, const char *str2);
void shikhaT_strrev(char *str);

void main()
 {
    char str1[100], str2[100], temp[100];
    clrscr();
    printf("Enter a string: ");
    scanf("%s", str1);

    // Using standard library functions
    printf("Length of string: %d\n", (int)strlen(str1));
    printf("Copy of string: %s\n", strcpy(temp, str1));

    printf("Enter another string: ");
    scanf("%s", str2);

    // Using standard library functions
    printf("Concatenation of strings: %s\n", strcat(str1, str2));
    printf("Comparison of strings: %d\n", strcmp(str1, str2));

    // Using custom implementations
    printf("Length of string (custom): %d\n", shikhaT_strlen(str1));
    shikhaT_strcpy(temp, str1);
    printf("Copy of string (custom): %s\n", temp);

    shikhaT_strcat(str1, str2);
    printf("Concatenation of strings (custom): %s\n", str1);

    printf("Comparison of strings (custom): %d\n", shikhaT_strcmp(str1, str2));

    // Reverse the string using custom function
    shikhaT_strrev(str1);
    printf("Reversed string: %s\n", str1);
    getch();


}

// Custom implementation of strlen function
int shikhaT_strlen(const char *str) {
    int length = 0;
    while (*str != '\0') {
        length++;
        str++;
    }
    return length;
}

// Custom implementation of strcpy function
void shikhaT_strcpy(char *dest, const char *src) {
    while (*src != '\0') {
        *dest = *src;
        dest++;
        src++;
    }
    *dest = '\0';
}

// Custom implementation of strcat function
void shikhaT_strcat(char *dest, const char *src) {
    while (*dest != '\0') {
        dest++;
    }
    while (*src != '\0') {
        *dest = *src;
        dest++;
        src++;
    }
    *dest = '\0';
}

// Custom implementation of strcmp function
int shikhaT_strcmp(const char *str1, const char *str2) {
    while (*str1 && *str2 && *str1 == *str2) {
        str1++;
        str2++;
    }
    return *(unsigned char *)str1 - *(unsigned char *)str2;
}

// Custom implementation of strrev function
void shikhaT_strrev(char *str) {
    char *start = str;
    char *end = start + strlen(str) - 1;
    
    while (start < end) {
        char temp = *start;
        *start = *end;
        *end = temp;
        start++;
        end--;
    }
}

Output

String function in C Output

Code Description

This program implements custom versions of some common string functions (strlen, strcpy, strcat, strcmp, and strrev) along with the usage of standard library functions for comparison. It takes two strings as input from the user and demonstrates the working of these functions.

The length of the first string "Shikha" is 6 characters. The copy of the first string is "Shikha". The concatenation of the strings "Shikha" and "Tiwari" is "ShikhaTiwari". The comparison of the strings "ShikhaTiwari" and "Tiwari" results in a negative value (-1). The length of the concatenated string "ShikhaTiwari" is 12 characters (including the null terminator). The custom copy of the first string "Shikha" is "ShikhaTiwari". The custom concatenation of the strings "Shikha" and "Tiwari" is "ShikhaTiwariTiwari". The custom comparison of the strings "ShikhaTiwariTiwari" and "Tiwari" results in a negative value (-1). The reversed string of "ShikhaTiwariTiwari" is "irawiTahkihS".

Summary

C strings are powerful for text processing, but they require manual memory management. To find the length of a string, you can use the strlen() function. To copy one string to another, you can use the strcpy() function. To concatenate (append) one string to another, you can use the strcat() function. To compare two strings, you can use the strcmp() function.


Similar Articles