String Examples

/*
 * Get the length of a string
 */
int
strlen(char *s)
{
	int i = 0;
	while (s[i] != '\0') i++;
	return(i);
}

/* 
 * Copy src to dest
 */
void
strcpy(char *dest, char *src)
{
	while('\0' != (*dest++ = *src++)) ;
}

/* 
 * append to the string dest 
 */
void
strcat(char *dest, char *add)
{
	while('\0' != *dest) dest++; 
	while('\0' != (*dest++ = *add++));
}

/*
 * Compare two strings. 
 * return < 0 if s0 if s>t
 */
int
strcmp(char *s, char *t)
{
	while((*s == *t) && (*s != '\0')) s++, t++;
	return(*s - *t);
}

next slide