• Fri, Mar 2026

String Manipulation in C

String Manipulation in C

Master string manipulation in C—learn to create, modify, compare, concatenate, convert, and handle null-terminated char arrays with clear examples.

Strings are a fundamental data type in programming, used to represent sequences of characters. In the C programming language, strings are represented as arrays of characters terminated by a null character ('\0'). This tutorial will cover various string operations, including string input/output, concatenation, comparison, and manipulation.

1. Introduction to Strings

In C, strings are sequences of characters stored in an array. The last character of a string is always the null character ('\0'), which marks the end of the string.

2. String Declaration and Initialization

Strings can be declared and initialized using the following syntax:

char myString[] = "Hello, World!";

3. String Input and Output

C provides the printf() function for output and scanf() function for input. To print a string, you can use the %s format specifier:

#include 

int main() {
    char myString[] = "Hello, World!";
    printf("String: %s\n", myString);

    return 0;
}

To read a string, use the %s format specifier within scanf():

#include 

int main() {
    char inputString[100];
    printf("Enter a string: ");
    scanf("%s", inputString);
    printf("You entered: %s\n", inputString);

    return 0;
}

4. String Length

To find the length of a string, you can use the strlen() function from the string.h library:

#include 
#include 

int main() {
    char myString[] = "Hello, World!";
    int length = strlen(myString);
    printf("Length of string: %d\n", length);

    return 0;
}

5. String Concatenation

String concatenation involves combining two strings into a single string. This can be done using functions like strcat() from the string.h library or by manually iterating through the strings and copying characters.

#include 
#include 

int main() {
    char str1[50] = "Hello, ";
    char str2[] = "World!";
    
    strcat(str1, str2);
    
    printf("Concatenated string: %s\n", str1);

    return 0;
}

6. String Comparison

String comparison is performed using functions like strcmp() from the string.h library. It returns 0 if both strings are equal.

#include 
#include 

int main() {
    char str1[] = "apple";
    char str2[] = "orange";
    
    int result = strcmp(str1, str2);
    if (result == 0) {
        printf("Strings are equal.\n");
    } else {
        printf("Strings are not equal.\n");
    }

    return 0;
}

7. String Copying

To copy one string into another, you can use functions like strcpy() from the string.h library:

#include 
#include 

int main() {
    char source[] = "Copy this.";
    char destination[20];
    
    strcpy(destination, source);
    
    printf("Copied string: %s\n", destination);

    return 0;
}

8. String Manipulation

C provides various functions for manipulating strings, such as strchr(), strstr(), strtok(), etc. These functions help in searching for characters or substrings within a string, and tokenizing strings into smaller parts.

This website uses cookies to enhance your browsing experience. By continuing to use this site, you consent to the use of cookies. Please review our Privacy Policy for more information on how we handle your data. Cookie Policy