C Program to Delete Vowels from a String (In-Place, O(n))

Deleting the vowels from a string is a classic C exercise because the obvious approach — remove a character, then shift everything after it left by one — is O(n²) and fiddly. The clean solution is the two-index in-place filter: one index reads every character, a second index writes only the characters you’re keeping, and the string compacts itself in a single pass. That read/write-index pattern shows up everywhere (it’s how std::remove_if works in C++, and how you strip whitespace, digits, or duplicates too). Tested, warning-free C89 below.

How It Works — Step by Step

  1. Read the line with fgets() and strip its trailing newline.
  2. Walk the string with read_pos. For each character, ask is_vowel().
  3. Not a vowel: copy it to text[write_pos] and advance write_pos. Vowel: skip it (just count it).
  4. After the loop, text[write_pos] = '\0' — re-terminating at the new, shorter length is what actually “deletes” the vowels.

Trace for "fun""fn":

read_pos char vowel? action string so far
0 f no write at 0 f
1 u yes skip f
2 n no write at 1 fn
terminate at index 2 fn

C Program to Delete Vowels from a String

#include <stdio.h>
#include <string.h>

#define MAX_LEN 100

static int is_vowel(char c)
{
    switch (c) {
    case 'a': case 'e': case 'i': case 'o': case 'u':
    case 'A': case 'E': case 'I': case 'O': case 'U':
        return 1;
    default:
        return 0;
    }
}

int main(void)
{
    char text[MAX_LEN];
    int read_pos, write_pos = 0, removed = 0;

    printf("Enter a string: ");
    if (fgets(text, sizeof text, stdin) == NULL) {
        printf("No input.\n");
        return 1;
    }
    text[strcspn(text, "\n")] = '\0';     /* strip the trailing newline */

    /* in-place filter: copy only non-vowels, using a second index */
    for (read_pos = 0; text[read_pos] != '\0'; read_pos++) {
        if (!is_vowel(text[read_pos])) {
            text[write_pos++] = text[read_pos];
        } else {
            removed++;
        }
    }
    text[write_pos] = '\0';               /* re-terminate the shorter string */

    printf("Without vowels: %s\n", text);
    printf("Vowels removed: %d\n", removed);
    return 0;
}

How to Compile and Run

gcc -ansi -Wall -Wextra -o vowels vowels.c
./vowels

Sample Input and Output

Test 1:

Enter a string: Programming in C is fun
Without vowels: Prgrmmng n C s fn
Vowels removed: 6

Test 2 — all vowels (the spaces survive):

Enter a string: AEIOU aeiou
Without vowels:
Vowels removed: 10

The second output line is a single space — every letter was removed, but the space between the words is not a vowel, and the program handles the nearly-empty result without any special-casing.

Code Explanation

  • is_vowel() — a switch with case fall-through lists all ten vowels in one readable place. (An alternative one-liner: strchr("aeiouAEIOU", c) != NULL — but mind that it matches '\0' too, so guard it.)
  • write_pos only advances on kept characters — since write_pos ≤ read_pos always, the copy never overwrites a character that hasn’t been read yet. That invariant is what makes the in-place filter safe.
  • fgets() + strcspn() — bounded input (the 2012 version of this exercise used gets(), which is a buffer overflow and was removed from the C standard) and the idiomatic newline strip.
  • text[write_pos] = '\0' — forgetting the re-termination leaves the tail of the original string visible; it’s the most common bug in student versions.

Time and Space Complexity

Aspect Complexity Why
Time O(n) one pass, one comparison per character
Space O(1) in-place — no second buffer
Naive delete-and-shift O(n²) each removal shifts the whole tail — the approach this pattern replaces

What This Program Teaches

  • The read-index / write-index in-place filter — a pattern you’ll reuse constantly
  • Why “deleting” from a C string is really writing a new terminator
  • switch fall-through used deliberately and legibly
  • Safe line input with fgets + strcspn instead of gets

Related C Programs

Test yourself: our free C Programming Quiz app for Android has 150+ questions with explanations for every answer.

Recommended Book

The C Programming Language by Kernighan & Ritchie builds string-processing exactly this way in Chapter 2 — we’ve solved all of its exercises. Also on Amazon.com.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>