A
arnuld
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 ?
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 ?