C Program To Predict The Gender of A Person With Name

Editor's note (2026): This was a 2014 guest post. We've removed the guest author's promotional bio and rewritten the program. The original code had bugs (it could read outside the string, used goto loops, and used curly quotes that won't compile). More importantly, a name cannot reliably tell you anyone's gender. Naming conventions vary across languages and cultures, and many names are used by people of any gender. Treat this purely as a string-handling exercise, and in real applications, ask people directly (with an optional, inclusive field) instead of guessing.

The idea

The original program guessed gender by checking a name's last letter (for example, names ending in "a" or "i"). This rule fails constantly: Joshua, Luca, Nikita, Andrea, and Kai are just a few names that break it. But the code is still a useful exercise in reading and examining strings in C.

A corrected program

This version reads a name, safely checks its last character, and reports what it found, without claiming to know the person's gender.

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

int main(void) {
    char name[100];
    char again = 'y';

    while (again == 'y' || again == 'Y') {
        printf("Enter a name: ");
        if (scanf("%99s", name) != 1) {
            return 1;
        }

        size_t len = strlen(name);
        if (len < 2) {
            printf("Please enter a longer name.\n");
            continue;
        }

        char last = (char)tolower((unsigned char)name[len - 1]);
        int ends_in_vowel = strchr("aeiou", last) != NULL;

        printf("\"%s\" has %zu letters and ends in '%c' (%s).\n",
               name, len, last, ends_in_vowel ? "a vowel" : "a consonant");

        printf("Check another name? (y/n): ");
        scanf(" %c", &again);
    }

    return 0;
}

What this exercise teaches

  • Reading input safely with a width limit (%99s) to avoid buffer overflows
  • Using strlen() and checking length before indexing into a string
  • Normalizing case with tolower()
  • Searching a set of characters with strchr()
  • Using a while loop instead of goto

Need help with software development or data handling done right? I.T. For Less can help — contact us.

Development