Reverse a String in C – Dead Fun, Innit

reverse a string in c – dead fun, innit

intro

Oi, you lot! New to C and fancy summat a bit cheeky? We’re gonna sling together a lil program to reverse a string—ya know, turn “hello” into “olleh” like some mad backwards spell. It’s nowt posh but proper ace—like when I was tryna read my mate’s text upside down after a few pints (couldn’t, 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 fancy, 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 gander:

#include
#include

int main() { // nearly did void, ha!
char str[50],temp; // 50’s plenty, innit
int i,j; // for swapping, mate

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

printf("before flip: %s\n",str);

j=strlen(str)-1; // last letter’s spot
for(i=0;i temp=str[i]; // stash one
str[i]=str[j]; // flip em
str[j]=temp; // chuck it back
}

printf("after flip: %s\n",str); // 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
before flip: hello
after flip: olleh

how it works, sorta

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

1. them `#include` bits
This is me nicking my toolkit. `` for `printf` and stuff, `` for `strlen`—fancy pants “string helpers”. Forgot the second one once, and my code just sat there like a lemon. Total plonker 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],temp; int i,j;`
These are my lil buckets. `str[50]` is a big string bucket—50 letters tops, plenty for most words. `temp` is a spare char for swapping, `i` and `j` are for counting from each end. Sorted!

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 type and chucks it in `str`. Bit dodgy—`scanf`’s safer—but quick and dirty, innit! `%s` is for strings, no faffy numbers here.

5. find the end: `j=strlen(str)-1;`
`strlen(str)` counts the letters, minus 1 coz arrays start at 0. So `j` sits at the last letter—like “o” in “hello”. Sets us up to swap from both ends!

6. flip it: `for(i=0;i
Here’s the magic! `for(i=0;i

7. showing off: `printf`
`printf("before flip: %s\n",str)` shows it first, then `printf("after flip: %s\n",str)` flaunts the reverse. `%s` slots the string in, `\n` keeps it tidy. Forgot a `;` once—CODE JUST DIED, and I was proper raging!

8. `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 pig.

And that’s yer lot! A smashing lil program to flip a string backwards. Faff with it—chuck in daft words, tweak it to say “backwards bruv” if ya fancy. First time I ran this, I typed “beer” and got “reeb”—sounded like a right laugh! You’re a star already, mate—keep smashing it! 😊