/* This program uses code from "Algorithms in C, Third Edition," * by Robert Sedgewick, Addison-Wesley, 1998. */ #include /* return the length of string s, i.e., the number of * characters up to and not including the first occurrence of * the NULL character. */ int strlen1(char * s) { int counter = 0; while (s[counter] != 0) { counter++; } return counter; } /* copy the contents of source to the target string. */ void strcpy1(char * target, char * source) { int counter = 0; while (source[counter] != 0) { target[counter] = source[counter]; counter++; } } /* ASCII-based comparison of the two strings. * Return values: * 0, if the two srings have the same contents. * less than 0, if the ASCII code of s1 is less than the ASCII code * of s2 at the first position where s1 and s2 differ. * greater than 0, if the ASCII code of s1 is greater than the ASCII code * of s2 at the first position where s1 and s2 differ. */ int strcmp1_unoptimized(char * s1, char * s2) { int counter = 0; while ((s1[counter] != 0) && (s2[counter] != 0)) { if (s1[counter] != s2[counter]) { return s1[counter] - s2[counter]; } counter++; } return s1[counter] - s2[counter]; } /* ASCII-based comparison of the two strings. * Return values: * 0, if the two srings have the same contents. * less than 0, if the ASCII code of s1 is less than the ASCII code * of s2 at the first position where s1 and s2 differ. * greater than 0, if the ASCII code of s1 is greater than the ASCII code * of s2 at the first position where s1 and s2 differ. */ int strcmp1(char * s1, char * s2) { int counter = 0; while (s1[counter] != 0) { if (s1[counter] != s2[counter]) { break; } counter++; } return s1[counter] - s2[counter]; } /* ASCII-based comparison of the first N letters of two strings. * Return values: * 0, if the two srings have the same contents. * less than 0, if the ASCII code of s1 is less than the ASCII code * of s2 at the first position where s1 and s2 differ. * greater than 0, if the ASCII code of s1 is greater than the ASCII code * of s2 at the first position where s1 and s2 differ. */ int strncmp1(char * s1, char * s2, int N) { int counter; for (counter = 0; counter < N; counter++) { if ((s1[counter] == 0) || (s2[counter] == 0) || (s1[counter] != s2[counter])) { return s1[counter] - s2[counter]; } } return 0; } char * strcat1(char * a, char * b) { int a_index = strlen(a); int b_index = 0; while(1) { char current = b[b_index]; a[a_index] = current; if (current == 0) { break; } a_index++; b_index++; } return a; }