Find String Length in C Without Built-ins – Proper Old School, Innit

find string length in c without built-ins – proper old school, innit

intro

Oi, you lot! New to C and fancy summat a bit gritty? We’re gonna sling together a lil program to count how long a string is—without using them posh built-in tricks like `strlen`. It’s dead simple but proper rugged—like when I was tryna count the letters in “pint” by hand after a few (got it wrong, too smashed). Great for getting your head round strings in C without a faff. Let’s have a butcher’s!

code bit

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

#include

int main() { // nearly did void, ha!
char str[50]; // 50’s loads, innit
int len=0; // counter, mate

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

while(str[len]!='\0') { // keep going til the end
len++; // tick it up
}

printf("length of %s is %d\n",str,len); // 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
length of hello is 5

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 pork scratchings, 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 doddle once ya suss it, swear down!

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 `` here, coz we’re going raw! 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 tight. Nearly wrote `void main()`—old me was a right numpty!

3. boxes: `char str[50]; int len=0;`
These are my lil buckets. `str[50]` is a big string pot—50 chars tops, plenty for most stuff. `len=0` is my counter—starts at 0 and ticks up as we go. No faffy built-ins, just grit!

4. chatting at ya: `printf` and `gets`
So yeah, `printf("gimme a string: ")` is me barking, “Oi, string, sharpish!” 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` hassle here, just raw input.

5. counting the bugger: `while(str[len]!='\0')`
Here’s the guts! Strings in C end with a sneaky `\0`—a null char that says “stop here”. `while(str[len]!='\0')` loops through `str`, checking each spot. If it ain’t `\0`, `len++` ticks up. Like counting mates in a queue til ya hit the end!

6. showing off: `printf`
Then `printf("length of %s is %d\n",str,len)` blurts it out. `%s` slots the string, `%d` the count, and `\n` keeps it tidy. Forgot a `;` once—CODE JUST DIED, and I was proper fuming for ages!

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 a string’s length the hard way. Faff with it—chuck in daft words, tweak it to say “how long’s this” if ya fancy. First time I ran this, I typed “oi” and got 2—short but sweet, eh! You’re a champ already, mate—keep smashing it! 😊