stackexchange.com/
Due to been 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, the question is:
do you think that my way of handling pointers (code is below) 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 critic my code please do so, I'm always tring to push my current C knowledge to the next level, if you think other (better) way to do it please post it, thank you all.
lksw
/*----------------------------------------------------*/
#include <stdio.h>
#include <string.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 vstrend(char *s, char *t);
int main(int argc, char *argv[])
{
int rt;
if ( rt = vstrend("Unix Powered", "red"))
printf("occurs\n");
return 0;
}
/*
find if 't' occurs at the end of 's'
*/
int
strend(char *s, char *t)
{
while (*s)
s++;
s -= strlen(t);
for (; *t ; s++, t++) {
if ( *s != *t)
return 0;
}
return 1;
}
/*
same as strend but without the call
to strlen
*/
int
vstrend(char *s, char *t)
{
char *pt = t;
for ( ; *s ; s++);
for ( ; *t ; t++, s-- );
t = pt;
for (; *t ; s++, t++) {
if ( *s != *t)
return 0;
}
return 1;
}
/*----------------------------------------------------*/
Aucun commentaire:
Enregistrer un commentaire