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
- Include the
<stdio.h>
header file. - Create a character array and initialize it with a string value.
- 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
- Include the
<stdio.h>
header file. - Create a character array and initialize it with a string value.
- 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
- Include the
<stdio.h>
header file. - Create a character array and initialize it with a string value.
- Modify individual characters or append new characters if the array has enough space.
- 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
- Include the
<stdio.h>
and<string.h>
header files. - Create two character arrays and initialize them with values.
- Concatenate the strings using the
strcat
function. - 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
- Include the
<stdio.h>
and<string.h>
header files. - Create a character array and initialize it with a string value.
- Use the
strstr
function to find a substring. - 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
- Include the
<stdio.h>
and<string.h>
header files. - Create a character array and initialize it with a string value.
- Use the
strlen
function to get the length of the string. - 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