// lang.c // Author: Josh Holtrop // Date: 12/30/04 // Modified: 12/30/04 #include "lang.h" /* strcmp compares two strings * Returns: * 0 if the strings are equal * <0 if the second string is less than the first * >0 if the second string is greater than the first */ int strcmp(char *str1, char *str2) { while (*str1 || *str2) { if (*str1 != *str2) return *str2 - *str1; str1++; str2++; } return 0; } /* strncmp compares up to n characters of two strings * Returns: * 0 if the strings are equal * <0 if the second string is less than the first * >0 if the second string is greater than the first */ int strncmp(char *str1, char *str2, int n) { while (n > 0 && (*str1 || *str2)) { if (*str1 != *str2) return *str2 - *str1; str1++; str2++; n--; } return 0; } /* str_change changes a certain character in a string * to a different character * Returns: how many characters were changed */ int str_change(char *str, char ch1, char ch2) { int count = 0; while (*str) { if (*str == ch1) { *str = ch2; count++; } str++; } return count; } // counts the occurrences of lookfor in str int str_count(char *str, char lookfor) { int count = 0; while (*str) { if (*str == lookfor) count++; str++; } return count; } // split the string into substrings by the splitchar, return number of substrings int str_split(char *str, char splitchar) { if (!(*str)) return 0; int subs = 1; while (*str) { if (*str == splitchar) { *str = 0; subs++; } str++; } return subs; } // advance the string pointer to the next substring, return a pointer to the next substring char *str_advance(char *str) { char *next = str; while (*next) next++; // advance pointer to end of this substring (null character) return next + 1; } // concatentate src onto the end of dest void strcat(char *dest, char *src) { while (*dest) dest++; strcpy(dest, src); }