Character Arrays and Strings
Definition and Usage
A character array is a sequence of characters stored in contiguous memory locations. Character arrays can be used to store text or strings in C, where each character occupies a single byte. A string in C is typically represented as a character array terminated by a null character ('0'
), which indicates the end of the string.
Declaring and Initializing Strings
Strings can be declared using character arrays with an explicit size or initialized with a string literal. Initializing with a string literal automatically includes the null terminator.
Declaration and Initialization Syntax:
char array_name[size];
or
char array_name[] = "string";
- char: Data type for storing a single character.
- array_name: Name of the character array.
- size: Number of characters the array can hold.
- Null Character: Every string in C is terminated with the null character ('0'). It marks the end of the string.
- Size of the Array: The size of the array must be large enough to hold the characters plus the null terminator.
- String Functions: C provides several functions for manipulating character arrays (strings), such as
strlen()
,strcpy()
,strcat()
, etc. - Example:
strlen(str)
returns the length of the string without counting the null character.
Important Points to Remember:
String operations:
- String Length (strlen()): This program calculates the length of a string using
strlen()
. - String Copy (strcpy()): This program copies one string into another using
strcpy()
. - String Concatenation (strcat()): This program concatenates two strings using
strcat()
. - String Comparison (strcmp()): This program compares two strings using
strcmp()
and prints whether they are equal or not. - String Search (strstr()): This program searches for a substring in a string using
strstr()
and prints the position where it's found.
1 : Declaring and Initializing a Character Array
#include <stdio.h>
#include <string.h>
int main() {
char greeting[6] = {'H', 'e', 'l', 'l', 'o', '0'};
char word[] = "Hello";
return 0;
}
Output
Reading Strings from Terminal
The scanf
function can be used to read strings from the terminal, but it stops reading at whitespace. To include spaces, use gets
or fgets
(preferred to avoid buffer overflow).
Example : 2
#include <stdio.h>
#include <string.h>
int main() {
char name[20];
printf("Enter your name: ");
scanf("%s", name); // Stops at whitespace
return 0;
}
Output
Writing Strings to Screen
Strings can be printed to the screen using the printf
function, which interprets the null character to determine the end of the string.
Example : 3
#include <stdio.h>
#include <string.h>
int main() {
char greeting[] = "Hello, World!";
printf("%s", greeting);
return 0;
}
Output
4 : String Concatenation(strcat)
#include <stdio.h>
#include <string.h>
int main() {
char str1[50] = "Hello, ";
char str2[] = "C World!";
// Concatenate str2 to str1
strcat(str1, str2);
printf("Concatenated String: %s\n", str1);
return 0;
}
Output
5 : String Comparison(strcmp)
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello";
char str2[] = "Hello";
char str3[] = "World";
// Compare two strings
if (strcmp(str1, str2) == 0) {
printf("str1 and str2 are equal.\n");
} else {
printf("str1 and str2 are not equal.\n");
}
// Compare different strings
if (strcmp(str1, str3) == 0) {
printf("str1 and str3 are equal.\n");
} else {
printf("str1 and str3 are not equal.\n");
}
return 0;
}
Output
6 : String Search(strstr)
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, C World!";
char* result;
// Search for a substring in the string
result = strstr(str, "C");
if (result != NULL) {
printf("Substring found at: %s\n", result);
} else {
printf("Substring not found.\n");
}
return 0;
}
Output
7 : String Copy(strcpy)
#include <stdio.h>
#include <string.h>
int main() {
char source[] = "Hello, C World!";
char destination[50];
// Copy the source string into the destination string
strcpy(destination, source);
printf("Source: %s\n", source);
printf("Destination: %s\n", destination);
return 0;
}
Output
8 : String Length(strlen)
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, C World!";
// Calculate the length of the string
int length = strlen(str);
printf("The length of the string is: %d\n", length);
return 0;
}