?? standard.shar
字號(hào):
X int len1, len2;XX if (s1 == NULL || s2 == NULL) {X Error("NULL string passed to Concat");X }X len1 = strlen(s1);X len2 = strlen(s2);X s = CreateString(len1 + len2);X strcpy(s, s1);X strcpy(s + len1, s2);X return (s);X}XXchar IthChar(string s, int i)X{X int len;XX if (s == NULL) Error("NULL string passed to IthChar");X len = strlen(s);X if (i < 0 || i > len) {X Error("Index outside of string range in IthChar");X }X return (s[i]);X}XXstring SubString(string s, int p1, int p2)X{X int len;X string result;XX if (s == NULL) Error("NULL string passed to SubString");X len = strlen(s);X if (p1 < 0) p1 = 0;X if (p2 >= len) p2 = len - 1;X len = p2 - p1 + 1;X if (len < 0) len = 0;X result = CreateString(len);X strncpy(result, s + p1, len);X result[len] = '\0';X return (result);X}XXstring CharToString(char ch)X{X string result;XX result = CreateString(1);X result[0] = ch;X result[1] = '\0';X return (result);X}XXint StringLength(string s)X{X if (s == NULL) Error("NULL string passed to StringLength");X return (strlen(s));X}XXstring CopyString(string s)X{X string newstr;XX if (s == NULL) Error("NULL string passed to CopyString");X newstr = CreateString(strlen(s));X strcpy(newstr, s);X return (newstr);X}XX/* Section 2 -- String comparison functions */XXbool StringEqual(string s1, string s2)X{X if (s1 == NULL || s2 == NULL) {X Error("NULL string passed to StringEqual");X }X return (strcmp(s1, s2) == 0);X}XXint StringCompare(string s1, string s2)X{X if (s1 == NULL || s2 == NULL) {X Error("NULL string passed to StringCompare");X }X return (strcmp(s1, s2));X}XX/* Section 3 -- Search functions */XXint FindChar(char ch, string text, int start)X{X char *cptr;XX if (text == NULL) Error("NULL string passed to FindChar");X if (start < 0) start = 0;X if (start > strlen(text)) return (-1);X cptr = strchr(text + start, ch);X if (cptr == NULL) return (-1);X return ((int) (cptr - text));X}XXint FindString(string str, string text, int start)X{X char *cptr;XX if (str == NULL) Error("NULL pattern string in FindString");X if (text == NULL) Error("NULL text string in FindString");X if (start < 0) start = 0;X if (start > strlen(text)) return (-1);X cptr = strstr(text + start, str);X if (cptr == NULL) return (-1);X return ((int) (cptr - text));X}XX/* Section 4 -- Case-conversion functions */XXstring ConvertToLowerCase(string s)X{X string result;X int i;XX if (s == NULL) {X Error("NULL string passed to ConvertToLowerCase");X }X result = CreateString(strlen(s));X for (i = 0; s[i] != '\0'; i++) result[i] = tolower(s[i]);X result[i] = '\0';X return (result);X}XXstring ConvertToUpperCase(string s)X{X string result;X int i;XX if (s == NULL) {X Error("NULL string passed to ConvertToUpperCase");X }X result = CreateString(strlen(s));X for (i = 0; s[i] != '\0'; i++) result[i] = toupper(s[i]);X result[i] = '\0';X return (result);X}XX/* Section 5 -- Functions for converting numbers to strings */XXstring IntegerToString(int n)X{X char buffer[MaxDigits];XX sprintf(buffer, "%d", n);X return (CopyString(buffer));X}XXint StringToInteger(string s)X{X int result;X char dummy;XX if (s == NULL) {X Error("NULL string passed to StringToInteger");X }X if (sscanf(s, " %d %c", &result, &dummy) != 1) {X Error("StringToInteger called on illegal number %s", s);X }X return (result);X}XXstring RealToString(double d)X{X char buffer[MaxDigits];XX sprintf(buffer, "%G", d);X return (CopyString(buffer));X}XXdouble StringToReal(string s)X{X double result;X char dummy;XX if (s == NULL) Error("NULL string passed to StringToReal");X if (sscanf(s, " %lg %c", &result, &dummy) != 1) {X Error("StringToReal called on illegal number %s", s);X }X return (result);X}XX/* Private functions */XX/*X * Function: CreateStringX * Usage: s = CreateString(len);X * -----------------------------X * This function dynamically allocates space for a string ofX * len characters, leaving room for the null character at theX * end.X */XXstatic string CreateString(int len)X{X return ((string) GetBlock(len + 1));X}END_OF_FILEif test 5382 -ne `wc -c <'cslib/strlib.c'`; then echo shar: \"'cslib/strlib.c'\" unpacked with wrong size!fi# end of 'cslib/strlib.c'fiif test -f 'cslib/strlib.h' -a "${1}" != "-c" ; then echo shar: Will not clobber existing file \"'cslib/strlib.h'\"elseecho shar: Extracting \"'cslib/strlib.h'\" \(7407 characters\)sed "s/^X//" >'cslib/strlib.h' <<'END_OF_FILE'X/*X * File: strlib.hX * Version: 1.0X * Last modified on Fri Jul 15 14:10:40 1994 by erobertsX * -----------------------------------------------------X * The strlib.h file defines the interface for a simpleX * string library. In the context of this package, stringsX * are considered to be an abstract data type, which meansX * that the client relies only on the operations defined forX * the type and not on the underlying representation.X */XX/*X * Cautionary note:X * ----------------X * Although this interface provides an extremely convenientX * abstraction for working with strings, it is not appropriateX * for all applications. In this interface, the functions thatX * return string values (such as Concat and SubString) do soX * by allocating new memory. Over time, a program that usesX * this package will consume increasing amounts of memoryX * and eventually exhaust the available supply. If you areX * writing a program that runs for a short time and stops,X * the fact that the package consumes memory is not a problem.X * If, however, you are writing an application that must runX * for an extended period of time, using this package requiresX * that you make some provision for freeing any allocatedX * storage.X */XX#ifndef _strlib_hX#define _strlib_hXX#include "genlib.h"XX/* Section 1 -- Basic string operations */XX/*X * Function: ConcatX * Usage: s = Concat(s1, s2);X * --------------------------X * This function concatenates two strings by joining them endX * to end. For example, Concat("ABC", "DE") returns the stringX * "ABCDE".X */XXstring Concat(string s1, string s2);XX/*X * Function: IthCharX * Usage: ch = IthChar(s, i);X * --------------------------X * This function returns the character at position i in theX * string s. It is included in the library to make the typeX * string a true abstract type in the sense that all of theX * necessary operations can be invoked using functions. CallingX * IthChar(s, i) is like selecting s[i], except that IthCharX * checks to see if i is within the range of legal indexX * positions, which extend from 0 to StringLength(s).X * IthChar(s, StringLength(s)) returns the null characterX * at the end of the string.X */XXchar IthChar(string s, int i);XX/*X * Function: SubStringX * Usage: t = SubString(s, p1, p2);X * --------------------------------X * SubString returns a copy of the substring of s consistingX * of the characters between index positions p1 and p2,X * inclusive. The following special cases apply:X *X * 1. If p1 is less than 0, it is assumed to be 0.X * 2. If p2 is greater than the index of the last stringX * position, which is StringLength(s) - 1, then p2 isX * set equal to StringLength(s) - 1.X * 3. If p2 < p1, SubString returns the empty string.X */XXstring SubString(string s, int p1, int p2);XX/*X * Function: CharToStringX * Usage: s = CharToString(ch);X * ----------------------------X * This function takes a single character and returns aX * one-character string consisting of that character. TheX * CharToString function is useful, for example, if youX * need to concatenate a string and a character. SinceX * Concat requires two strings, you must first convertX * the character into a string.X */XXstring CharToString(char ch);XX/*X * Function: StringLengthX * Usage: len = StringLength(s);X * -----------------------------X * This function returns the length of s.X */XXint StringLength(string s);XX/*X * Function: CopyStringX * Usage: newstr = CopyString(s);X * ------------------------------X * CopyString copies the string s into dynamically allocatedX * storage and returns the new string. This function is notX * ordinarily required if this package is used on its own,X * but is often necessary when you are working with more thanX * one string package.X */XXstring CopyString(string s);XX/* Section 2 -- String comparison functions */XX/*X * Function: StringEqualX * Usage: if (StringEqual(s1, s2)) ...X * -----------------------------------X * This function returns TRUE if the strings s1 and s2 areX * equal. For the strings to be considered equal, everyX * character in one string must precisely match theX * corresponding character in the other. Uppercase andX * lowercase characters are considered to be different.X */XXbool StringEqual(string s1, string s2);XX/*X * Function: StringCompareX * Usage: if (StringCompare(s1, s2) < 0) ...X * -----------------------------------------X * This function returns a number less than 0 if string s1X * comes before s2 in alphabetical order, 0 if they are equal,X * and a number greater than 0 if s1 comes after s2. TheX * ordering is determined by the internal representation usedX * for characters, which is usually ASCII.X */XXint StringCompare(string s1, string s2);XX/* Section 3 -- Search functions */XX/*X * Function: FindCharX * Usage: p = FindChar(ch, text, start);X * -------------------------------------X * Beginning at position start in the string text, thisX * function searches for the character ch and returns theX * first index at which it appears or -1 if no match isX * found.X */XXint FindChar(char ch, string text, int start);XX/*X * Function: FindStringX * Usage: p = FindString(str, text, start);X * ----------------------------------------X * Beginning at position start in the string text, thisX * function searches for the string str and returns theX * first index at which it appears or -1 if no match isX * found.X */XXint FindString(string str, string text, int start);XX/* Section 4 -- Case-conversion functions */XX/*X * Function: ConvertToLowerCaseX * Usage: s = ConvertToLowerCase(s);X * ---------------------------------X * This function returns a new string with allX * alphabetic characters converted to lower case.X */XXstring ConvertToLowerCase(string s);XX/*X * Function: ConvertToUpperCaseX * Usage: s = ConvertToUpperCase(s);X * ---------------------------------X * This function returns a new string with allX * alphabetic characters converted to upper case.X */XXstring ConvertToUpperCase(string s);XX/* Section 5 -- Functions for converting numbers to strings */XX/*X * Function: IntegerToStringX * Usage: s = IntegerToString(n);X * ------------------------------X * This function converts an integer into the correspondingX * string of digits. For example, IntegerToString(123)X * returns "123" as a string.X */XXstring IntegerToString(int n);XX/*X * Function: StringToIntegerX * Usage: n = StringToInteger(s);X * ------------------------------X * This function converts a string of digits into an integer.X * If the string is not a legal integer or contains extraneousX * characters, StringToInteger signals an error condition.X */XXint StringToInteger(string s);XX/*X * Function: RealToStringX * Usage: s = RealToString(d);X * ---------------------------X * This function converts a floating-point number into theX * corresponding string form. For example, callingX * RealToString(23.45) returns "23.45". The conversion isX * the same as that used for "%G" format in printf.X */XXstring RealToString(double d);XX/*X * Function: StringToRealX * Usage: d = StringToReal(s);X * ---------------------------X * This function converts a string representing a real numberX * into its corresponding value. If the string is not aX * legal floating-point number
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -