It's My String… June 18, 2009
Okay, I just had way too much fun writing this so I thought I’d post it. It’s exercise 4-1 in K&R2. It’s pretty much “write a function which returns the rightmost index of some character in some string.”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #include <stdio.h> #include <string.h> int strindex(char s[], int t); char mystring[] = "This is my string and I can cry if I want to."; main() { int mychar = 'z'; printf("%c is at %d in %s\n", mychar, strindex(mystring, mychar), mystring); } int strindex(char s[], int t) { int i; for(i = strlen(s)-1; i >= 0; i--) { if(t == s[i]) return i; } return -1; } |
On a somewhat more serious note, I have finally bought some new shoes (yay?) from Zappos.com. They’re orange and like all orange things, they are good. Geoff’s Theorem: If something is orange, then it is good.
Another cool thing that every Mac user should play around with is Delicious Library 2. If you have never heard of it, think of it as a digital book shelf where you can display books, movies, video games, electronics, ect. that you own. The best part is that it lets you use your iSight camera to scan in the bar codes for everything! I got it as part of the Macheist bundle and have never played with it until now and I gotta tell you, scanning everything in is way more fun than it should be.
Your program would suck for the worst case scenario, though.
Prove your theorem.
Yeah, but worse case scenario probably wouldn’t ever happen… If it did though, shit sucks I guess.
Regarding my theorem – it can easily be proven through contradiction so I will leave it as an exercise to the reader. Don’t you hate it when that happens?