Strings in C



In this tutorial, we will learn about strings in C. We will cover the basics of string manipulation, including creating, accessing, modifying, and performing operations on strings.


What is a String

A string in C is a sequence of characters terminated by a null character ('\0'). Strings in C are typically represented as arrays of characters.


Creating Strings

Strings can be created in C using character arrays:

char str[] = "Hello, world!";

Alternatively, you can use a pointer to a character:

char *str = "Hello, world!";


Example 1: Initializing Strings

  1. Include the <stdio.h> header file.
  2. Create a character array and initialize it with a string value.
  3. Print the string using printf.

C Program

#include <stdio.h>
int main() {
    char str[] = "Hello, world!";
    printf("%s", str);
    return 0;
}

Output

Hello, world!


Example 2: Accessing Characters in a String

  1. Include the <stdio.h> header file.
  2. Create a character array and initialize it with a string value.
  3. Access and print individual characters using array indexing.

C Program

#include <stdio.h>
int main() {
    char str[] = "Hello";
    printf("%c\n", str[0]); // Accessing using array indexing
    printf("%c\n", str[1]);
    return 0;
}

Output

H
e


Example 3: Modifying Strings

  1. Include the <stdio.h> header file.
  2. Create a character array and initialize it with a string value.
  3. Modify individual characters or append new characters if the array has enough space.
  4. Print the modified string.

C Program

#include <stdio.h>
int main() {
    char str[50] = "Hello";
    str[0] = 'J'; // Modifying individual character
    strcat(str, " World!"); // Appending new characters using strcat
    printf("%s", str);
    return 0;
}

Output

Jello World!


Example 4: String Concatenation

  1. Include the <stdio.h> and <string.h> header files.
  2. Create two character arrays and initialize them with values.
  3. Concatenate the strings using the strcat function.
  4. Print the concatenated string.

C Program

#include <stdio.h>
#include <string.h>
int main() {
    char str1[50] = "Hello";
    char str2[] = " World!";
    strcat(str1, str2); // Concatenating strings
    printf("%s", str1);
    return 0;
}

Output

Hello World!


Example 5: Finding Substrings

  1. Include the <stdio.h> and <string.h> header files.
  2. Create a character array and initialize it with a string value.
  3. Use the strstr function to find a substring.
  4. Print the position of the found substring if it exists.

C Program

#include <stdio.h>
#include <string.h>
int main() {
    char str[] = "Hello, world!";
    char *pos = strstr(str, "world"); // Finding substring
    if (pos != NULL) {
        printf("Found 'world' at position: %ld", pos - str);
    } else {
        printf("Substring not found");
    }
    return 0;
}

Output

Found 'world' at position: 7


Example 6: String Length

  1. Include the <stdio.h> and <string.h> header files.
  2. Create a character array and initialize it with a string value.
  3. Use the strlen function to get the length of the string.
  4. Print the length of the string.

C Program

#include <stdio.h>
#include <string.h>
int main() {
    char str[] = "Hello, world!";
    printf("Length of the string: %ld", strlen(str)); // Getting string length
    return 0;
}

Output

Length of the string: 13