Strings in C
String declaration, manipulation, and standard library functions.
String Basics
In C, strings are arrays of characters terminated by a null character '\0'.
strings.c
#include <stdio.h>
int main() {
// Method 1: String literal
char str1[] = "Hello";
// Method 2: Character array
char str2[] = {'H', 'e', 'l', 'l', 'o', '\0'};
// Method 3: Fixed size
char str3[50] = "Hello";
// Print strings
printf("str1: %s\n", str1);
printf("str2: %s\n", str2);
// Access individual characters
printf("First char: %c\n", str1[0]);
printf("Last char: %c\n", str1[4]);
// Modify string
str1[0] = 'h';
printf("Modified: %s\n", str1);
return 0;
}
String Functions (string.h)
| Function | Description |
|---|---|
| strlen(s) | Returns string length |
| strcpy(dest, src) | Copies src to dest |
| strcat(dest, src) | Concatenates src to dest |
| strcmp(s1, s2) | Compares two strings |
| strchr(s, c) | Finds character in string |
| strstr(s1, s2) | Finds substring |
string_functions.c
#include <stdio.h>
#include <string.h>
int main() {
char str1[50] = "Hello";
char str2[50] = "World";
char str3[50];
// strlen - Get length
printf("Length of str1: %zu\n", strlen(str1));
// strcpy - Copy string
strcpy(str3, str1);
printf("After strcpy: %s\n", str3);
// strcat - Concatenate
strcat(str1, " ");
strcat(str1, str2);
printf("After strcat: %s\n", str1);
// strcmp - Compare
if (strcmp(str1, str2) == 0)
printf("Strings are equal\n");
else
printf("Strings are different\n");
// strchr - Find character
char *ptr = strchr(str1, 'W');
if (ptr != NULL)
printf("Found 'W' at position: %ld\n", ptr - str1);
// strstr - Find substring
ptr = strstr(str1, "World");
if (ptr != NULL)
printf("Found 'World' at position: %ld\n", ptr - str1);
return 0;
}
String Operations
string_ops.c
#include <stdio.h>
#include <ctype.h>
// Reverse string
void reverse(char str[]) {
int len = 0;
while (str[len] != '\0') len++;
for (int i = 0; i < len / 2; i++) {
char temp = str[i];
str[i] = str[len - 1 - i];
str[len - 1 - i] = temp;
}
}
// Check palindrome
int isPalindrome(char str[]) {
int len = 0;
while (str[len] != '\0') len++;
for (int i = 0; i < len / 2; i++) {
if (str[i] != str[len - 1 - i])
return 0;
}
return 1;
}
// Convert to uppercase
void toUpper(char str[]) {
for (int i = 0; str[i] != '\0'; i++) {
if (str[i] >= 'a' && str[i] <= 'z')
str[i] = str[i] - 32;
}
}
// Count vowels
int countVowels(char str[]) {
int count = 0;
for (int i = 0; str[i] != '\0'; i++) {
char c = tolower(str[i]);
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
count++;
}
return count;
}
int main() {
char str[100];
printf("Enter a string: ");
scanf(" %[^\n]", str);
printf("Original: %s\n", str);
printf("Vowels: %d\n", countVowels(str));
if (isPalindrome(str))
printf("Palindrome: Yes\n");
else
printf("Palindrome: No\n");
toUpper(str);
printf("Uppercase: %s\n", str);
return 0;
}
Array of Strings
string_array.c
#include <stdio.h>
#include <string.h>
int main() {
// Array of strings
char names[5][20] = {
"Alice",
"Bob",
"Charlie",
"David",
"Eve"
};
// Print all names
printf("Names:\n");
for (int i = 0; i < 5; i++) {
printf("%d. %s\n", i + 1, names[i]);
}
// Sort names
char temp[20];
for (int i = 0; i < 4; i++) {
for (int j = i + 1; j < 5; j++) {
if (strcmp(names[i], names[j]) > 0) {
strcpy(temp, names[i]);
strcpy(names[i], names[j]);
strcpy(names[j], temp);
}
}
}
printf("\nSorted names:\n");
for (int i = 0; i < 5; i++) {
printf("%d. %s\n", i + 1, names[i]);
}
return 0;
}