String Fundamentals
LIBRARY FUNCTIONS
- Character handling functions are found in ctype (isdigit, isupper, tolower,)
- String manipulation functions are available in string
strcpy (str_var, str_exp) copy str_exp to str_var
strcat(str_var, str_exp) concatenate str_exp onto str_var
strlen(string) length of string Excluding \0
strcmp(str1, str2) -- compare strings. Returns neg. number if str1 < str2, 0 if equal, pos. number if str1 > str2.
strncpy(str_var, str_exp, n) copy at most n characters of str_exp to str_var
strncmp(str1, str2,n) compare at most n characters.
strchr (string, char) find position of char in string. Returns the address
strtok(str_exp, char) parses str1 into tokens . Returns the NEXT sequence of characters contained in str1, up to but not including the delimeter char
- The strlen function does not include the \0
- A = "hello" is not correct, instead use: strcpy(A, "hello")
- The strncpy may not place a \0 at the end of the string
- To append strings, use strcat
- Descriptions:
- strcpy (string1, "hello");
Copies hello and \0 into string1 hello\0
- x = strlen (string1);
Returns the length of string1 (5), not including the \0
- strncpy(string2, string1, 5);
Copies at most 5 characters of string1("hello") into string2
- strcat(string2, " bye");
Appends "bye\0" to string2
- strncat(string2, string1, 3);
Appends 3 characters of string1 onto string2 hello byehel
- To compare strings, use:
ans = strcmp(s1,s2)
If s1 = = s2 ans is 0
If s1 < s2 ans is < 0
If s1 > s2 ans is > 0
Character Routines (ctype)
- isalpha(char) -- alphabetic
- isupper(char) upper case
- islower(char) lower case
- isdigit(char) 0-9
- isascii(char) is it ascii
- isspace(char) is it blank
- isprint(char) printable?
- iscntrl(char) control character
- ispunct(char) punctuation
- toupper(char) -- convert character to uppercase
- tolower(char) convert to lower case
- toascii(int) convert to character
- Old Conversion Routines
- atoi(string) convert string of ascii digits to an integer number
- atof(string) convert string of ascii digits to a floating point
- itoa(integer) convert integer to a string of ascii digits