check if a string’s a palindrome in c – dead cool, innit
intro
Oi, you lot! New to C and fancy summat a bit nifty? We’re gonna sling together a lil program to see if a string’s a palindrome—ya know, one of them words that reads the same backwards, like “racecar” or “madam”. It’s dead simple but proper ace—like when I was tryna read “beer” backwards in the pub mirror (spoiler: “reeb” ain’t a thing). 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 shambles on your blog. take a gander:
#include
int main() { // nearly did void, ha!
char str[50]; // 50’s loads, innit
int i,j,flag=1; // flag’s our mate
printf("gimme a string: ");
gets(str); // dodgy but quick
j=strlen(str)-1; // last letter’s spot
for(i=0;i
flag=0; // wave it goodbye
break; // leg it outta here
}
}
if(flag==1) {
printf("%s’s a palindrome, nice one!\n",str);
}
else {
printf("%s ain’t a palindrome, mate!\n",str); // forgot ; once, ugh!
}
return 0;
}
what ya get
Here’s what it chucks out when ya run it. I lobbed in a couple words—worked bang on, mate!
racecar’s a palindrome, nice one!
gimme a string: hello
hello ain’t a palindrome, mate!
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 piece of piss once ya clock it, swear down!
1. them `#include` bits
This is me nicking my toolkit. `
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,j,flag=1;`
These are my lil buckets. `str[50]` is a big string pot—50 chars tops, plenty for most words. `i` and `j` for checking ends, `flag=1` starts as “yep, it’s a palindrome” til we prove it ain’t.
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` faff here.
5. find the end: `j=strlen(str)-1;`
`strlen(str)` counts the letters, minus 1 coz arrays start at 0. So `j` lands on the last letter—like “r” in “racecar”. Sets us up to check from both ends!
6. check it: `for(i=0;i 7. palindrome or not: `if(flag==1)` 8. `return 0` to scarper And that’s yer lot! A cracking lil program to check if a string’s a palindrome. Faff with it—chuck in daft words, tweak it to say “samey same” if ya fancy. First time I ran this, I tried “lager” and got “nope”—gutted, eh! You’re a star already, mate—keep smashing it! 😊
Here’s the guts! `for(i=0;i
If `flag`’s still 1 after the loop, `printf("%s’s a palindrome, nice one!\n",str)` cheers it. Else, `printf("%s ain’t a palindrome, mate!\n",str)` shrugs it off. `%s` slots the string, `\n` keeps it tidy. Forgot a `;` once—CODE JUST DIED, and I was proper fuming!
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.