⚡0 XP🔥0

Strings

Lesson 9 of 13 ⏱ 5 min read ✎ 2 exercises ❓ 4-question quiz ▶ Runs C

Strings as character arrays ending in '\0' - reading, printing, string.h functions (strlen, strcpy, strcat, strcmp), and writing your own string logic.

C has no separate "string" type. A string is an array of char that ends with the special null character '\0' (ASCII 0). That terminator is how functions like printf know where the text stops.

char city[] = "Indore";
Index 0 1 2 3 4 5 6
Value I n d o r e \0

"Indore" has 6 letters but needs 7 bytes.

▶ Try it YourselfC
Ctrl + Enter to run
Output
Indore has 7 bytes
First letter: I
Changed: indore
V-i-n-d-h-y-a-e-d-u-
हिंदी में समझें

C में string असल में char का array है जिसके आखिर में '\0' (null character) लगा होता है। इसी '\0' से पता चलता है कि string कहाँ खत्म हुई। इसलिए array का size हमेशा अक्षरों से एक ज़्यादा रखें।

Reading strings#

  • scanf("%s", s) - reads one word (stops at a space). No & - s is already an address.
  • fgets(s, size, stdin) - reads a full line, including spaces and the newline.
▶ Try it YourselfC
Ctrl + Enter to run
Output
Word: [Asha]
Line: [Asha Rani Verma]
Common Mistake

Never use gets() - it was removed from the C standard because it cannot limit the input size and lets a long input overwrite other memory (a buffer overflow, a famous security hole). Use fgets.

The string.h library#

Function What it does
strlen(s) length, not counting \0
strcpy(dest, src) copy src into dest
strncpy(dest, src, n) copy at most n characters
strcat(dest, src) append src to the end of dest
strcmp(a, b) compare: 0 if equal, <0 if a comes first, >0 otherwise
strchr(s, c) pointer to the first c in s (or NULL)
strstr(s, sub) pointer to the first occurrence of sub (or NULL)
▶ Try it YourselfC
Ctrl + Enter to run
Output
strlen(a) = 4
a = Data Science
c = Data
strcmp("abc", "abd") = -1
strcmp(c, "Data") = 0
'Sci' found inside a
Common Mistake

strcpy and strcat do not check the destination size. Make sure dest is large enough for the result plus \0.

Writing string functions yourself#

Interviewers love asking you to do these without string.h. All of them loop until '\0':

▶ Try it YourselfC
Ctrl + Enter to run
Output
Length: 10
Upper: VINDHYAEDU
Reversed: UDEAYHDNIV

Character functions: ctype.h#

isalpha, isdigit, isspace, isupper, islower, toupper, tolower:

▶ Try it YourselfC
Ctrl + Enter to run
Output
Upper 1, lower 3, digits 4, special 1
Strong password: yes

Array of strings#

A 2D char array holds several strings - one per row:

▶ Try it YourselfC
Ctrl + Enter to run
Output
1. Aditi
2. Kabir
3. Meera
4. Rohan

Summary#

  • A string is a char array ending in '\0'; size = letters + 1.
  • Read a word with scanf("%s"), a full line with fgets (never gets).
  • Use strlen, strcpy, strcat, strcmp from <string.h> - and strcmp, not ==, to compare.
  • <ctype.h> classifies and converts characters.

Practice Exercises

Write your code, then press Check. Your program is tested automatically.

1

Palindrome check

Read a word (no spaces) and print madam is a palindrome or hello is not a palindrome. Do it with two indexes moving towards the middle. Test input racecar.

Test input:
racecar
Expected output:
racecar is a palindrome
2

Count vowels, consonants, digits, spaces

Read a full line with fgets and print the counts in this format:

Vowels: 6
Consonants: 11
Digits: 4
Spaces: 4
Test input:
Batch 2026 starts on 15 July
Expected output:
Vowels: 4
Consonants: 13
Digits: 6
Spaces: 5

Quick Quiz

Q1.

How many bytes does char s[] = "INDIA"; take?

Q2.

What does strcmp("apple", "banana") return?

Q3.

Why can't you write s1 = s2; to copy one char array into another?

Q4.

Which function safely reads a whole line with spaces?