Exercise 5-5 (String functions - strncpy, strncat, strncmp)

Chapter_5     Exercise_5-4 Exercise_5-6







Exercise 5-5     K&R, p. 107


Exercise 5-5. Write versions of the library functions strncpy(), strncat(), and strncmp(), which operate on at most the first n characters of their argument strings. For example, strncpy(s,t,n) copies at most n characters of t to s. Full descriptions are in Appendix B.




CONTENTS:     strncpy.c     strncat.c     strncmp.c




strncpy.c         download


#include <stdio.h> // for printf(), scanf()
#include <string.h> // for strlen()

#define SIZE 100 // array size

void strncpy1(char*, char*, int n); // copy at most n characters from
void strncpy2(char*, char*, int n); // the second string to the first

int main()
{
char s[SIZE], t[SIZE];
int len;

printf("Type a word: ");
scanf("%s", t);

printf("(1) Copy 0 chars: ");
strncpy1(s, t, 0);
printf("%s\n", s);

len = strlen(t);

printf("(1) Copy %d chars: ", len-1);
strncpy1(s, t, len-1);
printf("%s\n", s);

printf("(1) Copy %d chars: ", len+1);
strncpy1(s, t, len+1);
printf("%s\n", s);

printf("(2) Copy 0 chars: ");
strncpy2(s, t, 0);
printf("%s\n", s);

printf("(2) Copy %d chars: ", len-1);
strncpy2(s, t, len-1);
printf("%s\n", s);

printf("(2) Copy %d chars: ", len+1);
strncpy2(s, t, len+1);
printf("%s\n", s);

return 0;
}

void strncpy1(char *s, char *t, int n) // copy at most n chars of t to s
{
if (n <= 0)
{
s[0] = '\0';
return;
}

int i = 0;

while ((s[i] = t[i]) && (i < n))
{i++;}

if (s[i]) {s[i] = '\0';}

while (i < n)
{
s[i] = '\0'; // pad with zeros
i++;
}
}
void strncpy2(char *s, char *t, int n) // copy at most n chars of t to s
{
if (n <= 0)
{
*s = '\0';
return;
}

while ((*s = *t) && n)
{s++, t++, n--;}

if (*s) {*s = '\0';}

while (n) // while (n > 0)
{
*s = '\0'; // pad with zeros
s++, n--;
}
}

/*
gcc strncpy.c -o strncpy
./strncpy
Type a word: abracadabra
(1) Copy 0 chars:
(1) Copy 10 chars: abracadabr
(1) Copy 12 chars: abracadabra
(2) Copy 0 chars:
(2) Copy 10 chars: abracadabr
(2) Copy 12 chars: abracadabra
*/











strncat.c         download


#include <stdio.h> // for printf(), scanf()
#include <string.h> // for strcpy(), strlen()

#define SIZE 100 // array size

void strncat1(char*, char*, int n); // concatenate at most n chars
void strncat2(char*, char*, int n); // from second to first string

int main()
{
char s1[SIZE*2], s2[SIZE*2], t[SIZE];
int len;

printf("Type a word: ");
scanf("%s", s1);
printf("Type another word: ");
scanf("%s", t);

printf("strncat1(%s, %s, 0): ", s1, t);
strcpy(s2, s1); // save old s1
strncat1(s1, t, 0); // nothing changes
printf("%s\n", s1);
len = strlen(t);
printf("strncat1(%s, %s, %d): ", s1, t, len-1);
strncat1(s1, t, len-1); // only s1 changes
printf("%s\n", s1);

strcpy(s1, s2); // revert to old s1

printf("strncat1(%s, %s, %d): ", s1, t, len+1);
strncat1(s1, t, len+1); // only s1 changes
printf("%s\n", s1);

strcpy(s1, s2); // revert to old s1

printf("strncat2(%s, %s, 0): ", s1, t);
strncat2(s1, t, 0); // nothing changes
printf("%s\n", s1);
printf("strncat2(%s, %s, %d): ", s1, t, len-1);
strncat2(s1, t, len-1); // only s1 changes
printf("%s\n", s1);

strcpy(s1, s2); // revert to old s1

printf("strncat2(%s, %s, %d): ", s1, t, len+1);
strncat2(s1, t, len+1); // only s1 changes
printf("%s\n", s1);

return 0;
}
// concatenate at most n chars of t to the end of s; s must be big enough
void strncat1(char *s, char *t, int n)
{
if (n <= 0) {return;} // nothing to do

int i = 0, j = 0;
// find the end of s:
while (s[i]) // while (s[i] != '\0')
{i++;}

while ((s[i] = t[j]) && (j < n))
{i++, j++;} // copy ending '\0', then exit

if (s[i]) {s[i] = '\0';}
}
// concatenate at most n chars of t to the end of s; s must be big enough
void strncat2(char *s, char *t, int n)
{
if (n <= 0) {return;} // nothing to do

// find the end of s:
while (*s) // while (*s != '\0')
{s++;}

while ((*s = *t) && n)
{s++, t++, n--;} // copy ending '\0', then exit

if (*s) {*s = '\0';}
}
/*
gcc strncat.c -o strncat
./strncat
Type a word: hocus-
Type another word: pocus
strncat1(hocus-, pocus, 0): hocus-
strncat1(hocus-, pocus, 4): hocus-pocu
strncat1(hocus-, pocus, 6): hocus-pocus
strncat2(hocus-, pocus, 0): hocus-
strncat2(hocus-, pocus, 4): hocus-pocu
strncat2(hocus-, pocus, 6): hocus-pocus
*/











strncmp.c         download


#include <stdio.h> // for printf(), scanf()
#include <string.h> // for strlen()

#define SIZE 100 // array size

int strncmp1(char*, char*, int n); // compare at most n chars
int strncmp2(char*, char*, int n); // of the two strings

int main()
{
char s[SIZE], t[SIZE];
int compare;
int ls, lt; // length of s, t
int min, max;

printf("Type a word: ");
scanf("%s", s);
printf("Type another word: ");
scanf("%s", t);

printf("(1) Compare first 0 chars: ");
printf("%s ", s);
compare = strncmp1(s, t, 0);
if (compare > 0) {putchar('>');}
else if (compare < 0) {putchar('<');}
else {putchar('=');}
printf(" %s\n", t);

printf("(2) Compare first 0 chars: ");
printf("%s ", s);
compare = strncmp2(s, t, 0);
if (compare > 0) {putchar('>');}
else if (compare < 0) {putchar('<');}
else {putchar('=');}
printf(" %s\n", t);

ls = strlen(s);
lt = strlen(t);
if (ls <= lt) {min = ls, max = lt;}
else {min = lt, max = ls;}

printf("(1) Compare first %d chars: ", min);
printf("%s ", s);
compare = strncmp1(s, t, min);
if (compare > 0) {putchar('>');}
else if (compare < 0) {putchar('<');}
else {putchar('=');}
printf(" %s\n", t);

printf("(2) Compare first %d chars: ", min);
printf("%s ", s);
compare = strncmp2(s, t, min);
if (compare > 0) {putchar('>');}
else if (compare < 0) {putchar('<');}
else {putchar('=');}
printf(" %s\n", t);

printf("(1) Compare first %d chars: ", max);
printf("%s ", s);
compare = strncmp1(s, t, max);
if (compare > 0) {putchar('>');}
else if (compare < 0) {putchar('<');}
else {putchar('=');}
printf(" %s\n", t);

printf("(2) Compare first %d chars: ", max);
printf("%s ", s);
compare = strncmp2(s, t, max);
if (compare > 0) {putchar('>');}
else if (compare < 0) {putchar('<');}
else {putchar('=');}
printf(" %s\n", t);

return 0;
}
// compare at most n chars of s, t, return < 0 for s < t, 0 for s == t, > 0 for s > t:
int strncmp1(char *s, char *t, int n)
{
if (n <= 0) {return 0;} // nothing to compare

int i;

for (i = 0; (i < n) && s[i] && t[i]; i++)
{
if (s[i] != t[i]) {return s[i] - t[i];}
}

if (i < n) {return s[i] - t[i];}

return s[i-1] - t[i-1];
}
// compare at most n chars of s, t, return < 0 for s < t, 0 for s == t, > 0 for s > t:
int strncmp2(char *s, char *t, int n)
{
if (n <= 0) {return 0;} // nothing to compare

for ( ; n && (*s == *t); s++, t++, n--)
{
if (*s == '\0') {return 0;} // if (!*s) {return *s;}
}

if (n) // if (n > 0)
{return *s - *t;}

return *(s-1) - *(t-1);
}

/*
gcc strncmp.c -o strncmp
./strncmp
Type a word: ana
Type another word: anna
(1) Compare first 0 chars: ana = anna
(2) Compare first 0 chars: ana = anna
(1) Compare first 3 chars: ana < anna
(2) Compare first 3 chars: ana < anna
(1) Compare first 4 chars: ana < anna
(2) Compare first 4 chars: ana < anna

./strncmp
Type a word: card
Type another word: cardboard
(1) Compare first 0 chars: card = cardboard
(2) Compare first 0 chars: card = cardboard
(1) Compare first 4 chars: card = cardboard
(2) Compare first 4 chars: card = cardboard
(1) Compare first 9 chars: card < cardboard
(2) Compare first 9 chars: card < cardboard

./strncmp
Type a word: funny
Type another word: fun
(1) Compare first 0 chars: funny = fun
(2) Compare first 0 chars: funny = fun
(1) Compare first 3 chars: funny = fun
(2) Compare first 3 chars: funny = fun
(1) Compare first 5 chars: funny > fun
(2) Compare first 5 chars: funny > fun

./strncmp
Type a word: abracadabra
Type another word: abracadabra
(1) Compare first 0 chars: abracadabra = abracadabra
(2) Compare first 0 chars: abracadabra = abracadabra
(1) Compare first 11 chars: abracadabra = abracadabra
(2) Compare first 11 chars: abracadabra = abracadabra
(1) Compare first 11 chars: abracadabra = abracadabra
(2) Compare first 11 chars: abracadabra = abracadabra

/strncmp
Type a word: -123
Type another word: 102
(1) Compare first 0 chars: -123 = 102
(2) Compare first 0 chars: -123 = 102
(1) Compare first 3 chars: -123 < 102
(2) Compare first 3 chars: -123 < 102
(1) Compare first 4 chars: -123 < 102
(2) Compare first 4 chars: -123 < 102

./strncmp
Type a word: 123
Type another word: 1204
(1) Compare first 0 chars: 123 = 1204
(2) Compare first 0 chars: 123 = 1204
(1) Compare first 3 chars: 123 > 1204
(2) Compare first 3 chars: 123 > 1204
(1) Compare first 4 chars: 123 > 1204
(2) Compare first 4 chars: 123 > 1204

./strncmp
Type a word: 0
Type another word: 00
(1) Compare first 0 chars: 0 = 00
(2) Compare first 0 chars: 0 = 00
(1) Compare first 1 chars: 0 = 00
(2) Compare first 1 chars: 0 = 00
(1) Compare first 2 chars: 0 < 00
(2) Compare first 2 chars: 0 < 00

/strncmp
Type a word: -1
Type another word: +1
(1) Compare first 0 chars: -1 = +1
(2) Compare first 0 chars: -1 = +1
(1) Compare first 2 chars: -1 > +1
(2) Compare first 2 chars: -1 > +1
(1) Compare first 2 chars: -1 > +1
(2) Compare first 2 chars: -1 > +1
*/









Chapter_5     Exercise_5-4 BACK_TO_TOP Exercise_5-6



Comments

Popular posts from this blog

Contents

Blogger Page Margins in Contempo