K&R2, exercise 5.5



I have created this program, it works, runs fine, no errors. I just wanted
to have some comments or any advice:


/* K&R2, exercise 5-5, page 107
*
* write versions of the library function strncpy, strncat, and strncmp
* which operate on at most 1st n characters of their argumen strings. for
* example, strncpy(s, t, n) copies at most n characters from t to s. Full
* descriptions are in Appendix B.
*
* This time this is the original problem statament of the authors ;)
*
* presently I have created only the strncat
*
*/


#include <stdio.h>
#include <stdlib.h>


char* my_strcpy( char* , char*, int );
int my_strlen( char* );


int my_strlen( char* s )
{
char* ps;

ps = s;
while( *ps != '\0' )
{
++ps;
}

return ps - s;
}


char* my_strcpy( char* s, char* t, int n)
{
char *ps, *pt;
int len_of_s, len_of_t;


/* these pointers will store the original position of
s and t.
*/
ps = s;
pt = t;

len_of_s = my_strlen( s );
len_of_t = my_strlen( t );

s = ps;
t = pt;

/* what if number of characters to be copied are larger
* than the length of the string
*/
if( n > len_of_s )
{
printf("\n");
printf("ERROR: too many characters\n\n\n");
exit(EXIT_FAILURE);
}

/* what if oone of the strings or both were empty */
if( !len_of_s || !len_of_t)
{
printf("\n");
printf("OOPS: empty strings ? \n\n\n");
exit(EXIT_FAILURE);
}


s = ps;
t = pt;

/* now we are sure that n < length of s, so we do not need
* to check for NULL character at the end of the array
*/
for( ; n != 0; ++s, ++t, --n )
{
*s = *t;
}

return ps;
}



int main(void)
{
int n;
char s[] = "pope";
char t[] = "zzzzzzzz";


n = 3;
printf("copying %d characters from <%s> into <%s> = ", n, t, s);
printf("<%s>\n", my_strcpy( s, t, n));


return 0;
}



my friend said that it is a good C programming practice to check for
NULL pointer at the beginning of both "my_strlen" and "my_strcpy":


if( !s || !t )
printf("ERROR: NULL pointers\n");


any views on this too ?


--
http://lispmachine.wordpress.com/

Please remove capital 'V's when you reply to me via e-mail.

.



Relevant Pages

  • Re: Is this code totaly a shit?
    ... | void UppStrg(char *Low, char *Upp, int cnt); ... whitespace-delimited string. ... You're also assuming that the representations of the characters ...
    (comp.lang.c)
  • Re: K&R exercise 1-18
    ... char lineis in this context equivalent to char *line ... int c, i; ... while (c!= EOF) ... you are reading characters line by line but your ...
    (comp.lang.c)
  • Re: Get ASCII value for character when higher than 127
    ... The reason I want to store the int-values for the characters in stead ... I also imagined, that if I have the int value, I can perform some ... char timeString; ... strcat; ...
    (microsoft.public.vc.language)
  • Re: Get ASCII value for character when higher than 127
    ... The reason I want to store the int-values for the characters in stead ... I also imagined, that if I have the int value, I can perform some ... char timeString; ... strcat; ...
    (microsoft.public.vc.language)
  • Re: Is there any GENRIC MACROS in c for INTEGERS,CHARACTERS ?
    ... >> The descriptions of the ctype functions all take int values. ... >> that char is converted to int in this case and that if char is signed ... What is EOF for in this context? ... the 'space' characters and so 0 must be the result. ...
    (comp.lang.c)