Count Vowels and Consonants in a String in C – Dead Tasty, Innit

count vowels and consonants in a string in c – dead tasty, innit

intro

Oi, you lot! New to C and fancy summat a bit cheeky? We’re gonna sling together a lil program to tally up vowels and consonants in a string—ya know, them “a e i o u” lads and the rest of the alphabet crew. It’s dead simple but proper handy—like when I was tryna count how many “e”s were in “beer” after a sesh (lost count, too smashed). Great for getting your head round strings in C without a right faff. Let’s have a butcher’s!

code bit

Here’s the code—nowt posh, just a quick job I chucked together. Shoved it in a box so it don’t look like a total dog’s dinner on your blog. take a peek:

#include

int main() { // nearly did void, ha!
char str[50]; // 50’s plenty, innit
int i,vowels=0,consonants=0; // counters, mate

printf("gimme a string: ");
gets(str); // dodgy but quick

for(i=0;str[i]!='\0';i++) { // loop til the end
if(str[i]=='a'||str[i]=='e'||str[i]=='i'||str[i]=='o'||str[i]=='u' ||
str[i]=='A'||str[i]=='E'||str[i]=='I'||str[i]=='O'||str[i]=='U') {
vowels++; // tally the vowel
}
else if((str[i]>='a'&&str[i]<='z')||(str[i]>='A'&&str[i]<='Z')) {
consonants++; // tally the consonant
}
}

printf("vowels in %s: %d\n",str,vowels);
printf("consonants in %s: %d\n",str,consonants); // forgot ; once, ugh!

return 0;
}

what ya get

Here’s what it chucks out when ya run it. I lobbed in a word—worked bang on, mate!

gimme a string: hello
vowels in hello: 2
consonants in hello: 3

how it works, kinda

Right, let’s have a natter bout this like we’re slumped at the boozer with a pint—or a bag of chips, coz I’m Hank Marvin again. I’ll spill it like my tatty old notes from when I was faffing with C. It’s a piece of piss once ya suss it, pinky swear!

1. that `#include ` gubbins
This is me nicking my toolkit before we start. ``—fancy pants “standard input-output”—lets me use `printf` and `gets`. No extra fluff here, just the basics! Forgot it once, and my code just sat there like a plonker. Total mug move!

2. `int main()` funtion
Gotta have a “main” to kick off—it’s like the door to my local. `int` means I’m lobbing a number out at the end (that `0` says “sorted, guv!”), and them curly `{}` lads keep it all snug. Nearly wrote `void main()`—old me was a right numpty!

3. boxes: `char str[50]; int i,vowels=0,consonants=0;`
These are my lil buckets. `str[50]` is a big string pot—50 chars tops, plenty for most words. `i` for looping, `vowels=0` and `consonants=0` start my tallies—both at 0 til we find summat.

4. chatting at ya: `printf` and `gets`
So yeah, `printf("gimme a string: ")` is me barking, “Oi, string, now!” Then `gets(str)` grabs what ya chuck in and shoves it in `str`. Bit ropey—`scanf`’s safer—but quick and dirty, innit! No `%s` faff here, just raw input.

5. counting em: `for(i=0;str[i]!='\0';i++)`
Here’s the guts! `for(i=0;str[i]!='\0';i++)` loops through `str` til it hits `\0`—that null char that says “end here”. `if(str[i]=='a'||...` checks if it’s a vowel—loads of `||`s for all the “a e i o u” gang, big and small. If it matches, `vowels++` ticks up. Else, `if((str[i]>='a'&&str[i]<='z')||(str[i]>='A'&&str[i]<='Z'))` checks if it’s a letter—if so, it’s a consonant, and `consonants++` ticks up. Ignores spaces and junk!

6. showing off: `printf`
Then `printf("vowels in %s: %d\n",str,vowels)` and `printf("consonants in %s: %d\n",str,consonants)` blurt the counts. `%s` slots the string, `%d` the numbers, `\n` keeps it tidy. Forgot a `;` once—CODE JUST DIED, and I was proper fuming!

7. `return 0` to scarper
That `return 0;` is me legging it, like, “Ta-ta, we’re done!” It’s a C thing—means it all went smooth as a greased weasel.

And that’s yer lot! A cracking lil program to count vowels and consonants. Faff with it—chuck in daft words, tweak it to say “vowel tally” if ya fancy. First time I ran this, I tried “oi” and got 1 vowel, 1 consonant—short but sweet, eh! You’re a star already, mate—keep smashing it! 😊