Due to being unable to ask a C programming language expert or advanced user in person, I'm posting this simple question about my C code. Today I was refreshing my C with exercise 5.4 of the C programming Language 2nd Edition by K&R, which is a very simple exercise:
Do you think that my way of handling pointers is 'very good' or 'world class'?
I know the example is short/simple but that question comes to my mind because I want to know the opinion from other knowledgeable people about the subject, if you want to critique my code please do so. I'm always trying to push my current C knowledge to the next level.
/*----------------------------------------------------*/ #include <stdio.h> #include <string.h> #include <stdlib.h> /* Exercise 5-4. Write the function strend(s,t) which returns 1 if the string t occurs at the end of the string s, and zero otherwise. */ int strend(char *s, char *t); int main(int argc, char *argv[]) { int rt; if ( rt = strend("Unix Powered", "red")) printf("occurs\n"); return EXIT_SUCCESS; } /* find if 't' occurs at the end of 's' */ int strend(char *s, char *t) { while (*s) s++; if ( strlen(s) < strlen(t)) return 0; s -= strlen(t); for (; *t ; s++, t++) { if ( *s != *t) return 0; } return 1; } /*----------------------------------------------------*/
strend("s1", "longer string than s");\$\endgroup\$