String In C Language

Understanding Strings and String Manipulation in C


Introduction

In C programming, a string is represented as an array of characters. Any group of characters within double quotes marks a constant string, making it a one-dimensional array of characters. String variables are declared as arrays since a string is essentially an array of characters.

String Declaration Syntax

char string_name[size];
The size determines the number of characters in the string_name. For example:
char std_name[25];
A string constant is an array of characters terminated by a null character '\0'.
char std_name[] = {'s', 'u', 'r', 'e', 's', 'h', '\0'};
Each character in the array occupies one byte of memory, and the elements are stored in contiguous memory locations.

Input and Displaying Strings

To input and display strings in C, you can use the following program:
#include <stdio.h>

    int main() {
        char std_name[10];

        printf("Enter your name: ");
        scanf("%s", std_name);

        printf("Hi, Hello %s sir", std_name);

        return 0;
    }

gets() and puts() Functions

The gets() and puts() functions handle one string at a time. Use gets() to input data from the user and puts() to display the output.

    #include <stdio.h>

    int main() {
        char std_name[10];

        puts("Enter your name");
        fgets(std_name, sizeof(std_name), stdin);

        puts("Hi");
        puts(std_name);

        return 0;
    }

String Manipulation

Here's a program to copy one string into another:
#include <stdio.h>

    int main() {
        char str[10], str1[10];
        int i;

        printf("Enter the string: ");
        scanf("%s", str);

        for (i = 0; str[i] != '\0'; i++) {
            str1[i] = str[i];
        }

        str1[i] = '\0';

        printf("\nThe Copied string is %s", str1);
        printf("\nThe Length of the string is %d", i);

        return 0;
    }

String Handling Functions

strcat(): Appends one string at the end of another.
strcat(string1, string2);
strncat(): Appends the first n characters of one string at the end of another.
strncat(string1, string2, n);
strcpy(): Copies one string into another.
strcpy(target, source);
strncpy(): Copies the first n characters of one string into another.
strncpy(string1, string2, n);

Two-Dimensional Array of Characters

Extend the concept of a character array to a two-dimensional array for storing lists, such as names.
#include <stdio.h>

    int main() {
        char std_name[4][10] = {"mahesh", "sanjay", "shrikant", "sandeep"};
        int i, j;

        printf("The student names:\n");

        for (i = 0; i < 4; i++) {
            printf("The %d student name is: ", i + 1);

            for (j = 0; j < 10; j++) {
                printf("%c", std_name[i][j]);
            }

            printf("\n");
        }

        return 0;
    }

Conclusion:

Strings in C provide a powerful tool for handling text, and by understanding the fundamentals and mastering manipulation functions, you can unlock a world of possibilities in programming and data processing.