#include <stdio.h> // for printf(), scanf()
#define LENGTH 100 // max word length
int strlen1(char*);
int strlen2(char*);
int strlen3(char*);
int main()
{
char word[LENGTH];
printf("Type a word: ");
scanf("%s", word);
printf("strlen1(%s) = %d\n", word, strlen1(word));
printf("strlen2(%s) = %d\n", word, strlen2(word));
printf("strlen3(%s) = %d\n", word, strlen3(word));
return 0;
}
int strlen1(char *s) // returns the length of string s
{
int i = 0;
while (s[i] != '\0')
{
i++;
}
return i;
}
int strlen2(char *s) // returns the length of string s
{
int i = 0;
while (*s++ != '\0')
{
i++;
}
return i;
}
int strlen3(char *s) // returns the length of string s
{
char *p = s;
while (*p) // while (*p != '\0')
{p++;}
return p-s;
}
/*
gcc strlen.c -o strlen
./strlen
Type a word: abracadabra
strlen1(abracadabra) = 11
strlen2(abracadabra) = 11
strlen3(abracadabra) = 11
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <stdio.h> // for printf(), scanf()
#define SIZE 100 // array size
void strcpy1(char*, char*);
void strcpy2(char*, char*);
int main()
{
char s[SIZE], t[SIZE];
printf("Type a word: ");
scanf("%s", t);
strcpy1(s, t);
printf("strcpy1(): %s\n", s);
strcpy2(s, t);
printf("strcpy2(): %s\n", s);
return 0;
}
void strcpy1(char *s, char *t) // copy t to s
{
int i = 0;
while (s[i] = t[i]) // while ((s[i] = t[i]) != '\0')
{i++;}
}
void strcpy2(char *s, char *t) // copy t to s
{
while (*s++ = *t++) // while ((*s++ = *t++) != '\0')
{}
}
/*
gcc strcpy.c -o strcpy
./strcpy
Type a word: abracadabra
strcpy1(): abracadabra
strcpy2(): abracadabra
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#include <stdio.h> // for printf(), scanf(), putchar()
#define SIZE 100 // array size
int strcmp1(char*, char*);
int strcmp2(char*, char*);
int main()
{
char s[SIZE], t[SIZE];
int compare;
printf("Type a word: ");
scanf("%s", s);
printf("Type another word: ");
scanf("%s", t);
printf("%s ", s);
compare = strcmp1(s, t);
if (compare > 0) {putchar('>');}
else if (compare < 0) {putchar('<');}
else {putchar('=');}
printf(" %s\n", t);
printf("%s ", s);
compare = strcmp2(s, t);
if (compare > 0) {putchar('>');}
else if (compare < 0) {putchar('<');}
else {putchar('=');}
printf(" %s\n", t);
return 0;
}
// compare s, t, return < 0 for s < t, 0 for s == t, > 0 for s > t:
int strcmp1(char *s, char *t)
{
int i;
for (i = 0; s[i] && t[i]; i++) // (s[i] != '\0') && (t[i] != '\0')
{
if (s[i] != t[i]) {return s[i] - t[i];}
}
return s[i] - t[i];
}
// compare s, t, return < 0 for s < t, 0 for s == t, > 0 for s > t:
int strcmp2(char *s, char *t)
{
for ( ; *s == *t; s++, t++)
{
if (*s == '\0') {return 0;} // if (!*s) {return *s;}
}
return *s - *t;
}
/*
gcc strcmp.c -o strcmp
./strcmp
Type a word: ana
Type another word: anna
ana < anna
ana < anna
./strcmp
Type a word: card
Type another word: cardboard
card < cardboard
card < cardboard
./strcmp
Type a word: funny
Type another word: fun
funny > fun
funny > fun
./strcmp
Type a word: abracadabra
Type another word: abracadabra
abracadabra = abracadabra
abracadabra = abracadabra
/strcmp
Type a word: -123
Type another word: 102
-123 < 102
-123 < 102
./strcmp
Type a word: 123
Type another word: 1204
123 > 1204
123 > 1204
./strcmp
Type a word: 0
Type another word: 00
0 < 00
0 < 00
/strcmp
Type a word: -1
Type another word: +1
-1 > +1
-1 > +1
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
Exercise 5-3. Write a pointer version of the function strcat() that we showed in Chapter_2; strcat(s,t) copies the string t to the end of s.
#include <stdio.h> // for printf(), scanf()
#include <string.h> // for strcpy()
#define SIZE 100 // array size
void strcat1(char*, char*);
void strcat2(char*, char*);
int main()
{
char s1[SIZE*2], s2[SIZE*2], t[SIZE];
printf("Type a word: ");
scanf("%s", s1);
printf("Type another word: ");
scanf("%s", t);
printf("strcat1(%s, %s): ", s1, t);
strcpy(s2,s1);// save old s1
strcat1(s1,t); // only s1 changes
printf("%s\n", s1);
printf("strcat2(%s, %s): ", s2, t);
strcat2(s2,t); // only s2 changes
printf("%s\n", s2);
return 0;
}
// concatenate t to the end of s; s must be big enough
void strcat1(char *s, char *t)
{
int i = 0, j = 0;
// find the end of s:
while (s[i]) // while (s[i] != '\0')
{i++;}
while (s[i] = t[j]) // while((s[i] = t[j]) != '\0')
{i++, j++;} // copy ending '\0', then exit
}
// concatenate t to the end of s; s must be big enough
void strcat2(char *s, char *t)
{
// find the end of s:
while (*s) // while (*s != '\0')
{s++;}
while (*s++ = *t++) // while ((*s++ = *t++) != '\0')
{} // copy ending '\0', then exit
}
/*
gcc strcat.c -o strcat
./strcat
Type a word: hocus-
Type another word: pocus
strcat1(hocus-, pocus): hocus-pocus
strcat2(hocus-, pocus): hocus-pocus
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
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.
#include <stdio.h> // for printf(), scanf()
#define SIZE 100 // array size
int strend(char *, char *); // first string ends with the second ? 1 : 0
int main()
{
char s[SIZE*2], t[SIZE];
printf("Type a string: ");
scanf("%s", s);
printf("Type another string: ");
scanf("%s", t);
printf("\"%s\" %s \"%s\"\n", s, strend(s,t) ? "ends with" : "doesn't end with", t);
return 0;
}
int strend(char *s, char *t) // first string ends with the second ? 1 : 0
{
int lens = 0, lent = 0; // length of s, t
while (*s) {lens++, s++;}
while (*t) {lent++, t++;}
if (lens < lent) {return 0;}
while (lent-- && *s-- == *t--) {}
if (*s != *t) {return 0;}
return 1;
}
/*
gcc strend.c -o strend
./strend
Type a string: anna
Type another string: ana
"anna" doesn't end with "ana"
./strend
Type a string: ana
Type another string: anna
"ana" doesn't end with "anna"
./strend
Type a string: anna
Type another string: anna
"anna" ends with "anna"
./strend
Type a string: card
Type another string: cardboard
"card" doesn't end with "cardboard"
./strend
Type a string: cardboard
Type another string: card
"cardboard" doesn't end with "card"
./strend
Type a string: cardboard
Type another string: board
"cardboard" ends with "board"
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
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.
#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
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#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
*/
*****************************************************************************************
*****************************************************************************************
*****************************************************************************************
#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
*/