Arrays & Strings
Strings
Character arrays ending in '\0': reading strings, the string.h functions, and writing length, reverse, palindrome and vowel-count programs yourself.
C has no special string type. A string is a char array that ends with the null character '\0'. Every string function relies on that terminator to know where the text stops.
C
#include <stdio.h>
int main(void) {
char name[] = "ASHA"; // the compiler adds '\0' for you
printf("%s has %zu bytes of storage\n", name, sizeof(name));
for (int i = 0; name[i] != '\0'; i++)
printf("name[%d] = '%c' (%d)\n", i, name[i], name[i]);
return 0;
}Output
ASHA has 5 bytes of storage name[0] = 'A' (65) name[1] = 'S' (83) name[2] = 'H' (72) name[3] = 'A' (65)
char name[] = "ASHA" takes 5 bytes: four letters plus the terminating '\0'.Declaring strings
C
char a[20] = "Hello"; // 20 bytes, "Hello" + '\0' + unused space
char b[] = "Hello"; // exactly 6 bytes
char c[] = {'H', 'i', '\0'}; // character by character — don't forget '\0'string.h functions
| Function | What it does |
|---|---|
strlen(s) |
length, not counting '\0' |
strcpy(dst, src) |
copy src into dst |
strncpy(dst, src, n) |
copy at most n characters |
strcat(dst, src) |
append src to the end of dst |
strcmp(a, b) |
0 if equal, negative if a < b, positive if a > b (dictionary order) |
strchr(s, c) |
pointer to first c in s, or NULL |
strstr(s, t) |
pointer to first occurrence of t in s, or NULL |
C
#include <stdio.h>
#include <string.h>
int main(void) {
char first[30] = "Data";
char second[] = "Structures";
printf("Length of \"%s\": %zu\n", first, strlen(first));
strcat(first, " ");
strcat(first, second);
printf("Joined: %s\n", first);
char copy[30];
strcpy(copy, first);
printf("Copy: %s\n", copy);
printf("strcmp(\"apple\", \"banana\") = %d\n", strcmp("apple", "banana") < 0 ? -1 : 1);
printf("strcmp(\"cat\", \"cat\") = %d\n", strcmp("cat", "cat"));
printf("Found \"Struct\" at position %td\n", strstr(first, "Struct") - first);
return 0;
}Output
Length of "Data": 4
Joined: Data Structures
Copy: Data Structures
strcmp("apple", "banana") = -1
strcmp("cat", "cat") = 0
Found "Struct" at position 5Write them yourself
Exams frequently say "without using library functions". These four are the most common:
C
#include <stdio.h>
int myStrlen(const char s[]) {
int n = 0;
while (s[n] != '\0') n++;
return n;
}
void myReverse(char s[]) {
for (int i = 0, j = myStrlen(s) - 1; i < j; i++, j--) {
char t = s[i]; s[i] = s[j]; s[j] = t;
}
}
int isPalindrome(const char s[]) {
for (int i = 0, j = myStrlen(s) - 1; i < j; i++, j--)
if (s[i] != s[j]) return 0;
return 1;
}
int countVowels(const char s[]) {
int v = 0;
for (int i = 0; s[i]; i++) {
char c = s[i] | 32; // quick lowercase for letters
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') v++;
}
return v;
}
int main(void) {
char word[] = "Programming";
printf("Length: %d\n", myStrlen(word));
printf("Vowels: %d\n", countVowels(word));
myReverse(word);
printf("Reversed: %s\n", word);
printf("\"madam\" palindrome? %s\n", isPalindrome("madam") ? "yes" : "no");
printf("\"kalvium\" palindrome? %s\n", isPalindrome("kalvium") ? "yes" : "no");
return 0;
}Output
Length: 11 Vowels: 3 Reversed: gnimmargorP "madam" palindrome? yes "kalvium" palindrome? no
Changing case with ctype.h
C
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main(void) {
char line[100];
int letters = 0, digits = 0, spaces = 0;
printf("Type a line: ");
fgets(line, sizeof line, stdin);
line[strcspn(line, "\n")] = '\0';
for (int i = 0; line[i]; i++) {
if (isalpha((unsigned char)line[i])) letters++;
else if (isdigit((unsigned char)line[i])) digits++;
else if (line[i] == ' ') spaces++;
line[i] = toupper((unsigned char)line[i]);
}
printf("Upper case: %s\n", line);
printf("Letters %d, digits %d, spaces %d\n", letters, digits, spaces);
return 0;
}Output
Type a line: Hello, CSE 101!
Upper case: HELLO, CSE 101!
Letters 8, digits 3, spaces 2Sample input is includedTry it Yourself
Arrays of strings
A list of words is a 2D char array — one row per word:
C
#include <stdio.h>
int main(void) {
char days[7][10] = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
for (int i = 0; i < 7; i++)
printf("%d: %s\n", i + 1, days[i]);
return 0;
}Output
1: Mon 2: Tue 3: Wed 4: Thu 5: Fri 6: Sat 7: Sun
Test yourself
4 questions · pick an answer to check it instantly
- 1
How many bytes does
char s[] = "C101";occupy?Four characters plus the terminating'\0'. - 2
What does
strlen("Kalvium")return?strlencounts characters before'\0'and does not include it. - 3
How should you check if two strings
aandbhave the same text?==compares addresses.strcmpreturns 0 when the contents match. - 4
What does
'\0'mark?Every C string ends with the null character so functions know where to stop.