ch2-strcat (Concatenate strings)

Chapter_2     Exercise_2-3     squeeze Exercise_2-4







strcat.c     K&R, p. 48         download


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

#define LENGTH 100 // max word length

void strCat(char[], char[]); // strcat() is declared in string.h, included by stdio.h

int main()
{
char s[LENGTH*2], t[LENGTH];

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

printf("strCat(%s, %s): ", s, t);
strCat(s,t); // only s[] changes
printf("%s\n", s);

return 0;
}
// concatenate t[] to the end of s[]; s[] must be big enough
void strCat(char s[], char t[])
{
int i = 0, j = 0;

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

while ((s[i++] = t[j++]) != '\0')
{} // copy ending '\0', then exit
}
/*
gcc strcat.c -o strcat
./strcat
Type a word: hocus-
Type another word: pocus
strCat(hocus-, pocus): hocus-pocus
*/









Chapter_2     Exercise_2-3     squeeze BACK_TO_TOP Exercise_2-4



Comments

Popular posts from this blog

Contents

Blogger Page Margins in Contempo