String Handling Summary:
String is an Array ending in NULL - ‘\0’
string.h and stdlib.h; - string functions
ctype.h; - character functions
Initialize using array or pointer notation:
·
char *s = "abc";
· char yourString[11] = "Do Be Do";
· char myString[] = "Do Be Do";
Copying strings:
·
Not: s = “Hello”
· Instead, strcpy or strncpy or sprintf
Comparing strings:
·
Not s1 == s2
· Instead strcmp or strncmp ; 0 = equal;
Length:
· strlen – up to first null
· printf: remember %s prints the string up to the null; watch nulls inside strings or missing null at end of string
Extract parts from string: sscanf
Convert string to number:
· atoi – returns an integer stopping at the first non digit
· strtod, strtol,strtoul – send both a pointer to the string and a pointer to a pointer to a string. It returns a pointer to the unconverted portion of first string.
Command line arguments: character array, with 0 as program path
Read String from Standard Input:
· getchar: get characters one at a time until equal EOF integer (waits for Enter)
· fgets: read one string from input, up to newline character, appending NULL
· scanf: remember “%19s” to not get to limit input, and you place the NULL
· scanf_s: safer, pass the length
Write string to Output:
· putchar to put a single character and
· puts to print one unformatted string
·
printf –
formatted output
List:
Library |
Function |
Purpose |
string.h |
char *strcpy(
char *s1, const char *s2 ) |
Copies string s2 into array s1. The value of
s1 is returned. |
string.h |
char *strncpy(
char *s1, const char *s2, size_t n ) |
Copies at most n characters of string s2
into array s1. The value of s1 is returned. |
string.h |
char *strcat(
char *s1, const char *s2 ) |
Appends string s2 to array s1. The first character
of s2 overwrites the terminating null character of s1. The value of s1 is
returned. |
string.h |
char *strncat(
char *s1, const char *s2, size_t n ) |
Appends at most n characters of string s2 to
array s1. The first character of s2 overwrites |
string.h |
int strcmp( const char *s1, const char *s2 ) |
Compares the string s1 with the string s2.
The function returns 0, less than 0 or greater than 0 if s1 is equal to, less
than or greater than s2, respectively. |
string.h |
int strncmp( const char *s1, const char *s2, size_t n ) |
Compares up to n characters of the string s1
with the string s2. The function returns 0, less than 0 or greater |
Stdio.h |
int getchar( void ) |
Inputs the next character from the standard
input and returns it as an integer. |
Stdio.h |
char *fgets(
char *s, int n, FILE *stream) |
Inputs characters from
the specified stream into the array s until a newline or end-of-file
character is encountered, or until n - 1 bytes are read.
In this chapter, we specify the stream as stdin—the
standard input stream, which is typically used to read characters from the
keyboard. A terminating null character is appended to the array. Returns the
string that was read into s. |
Stdio.h |
int putchar( int c ) |
Prints the character stored in c and returns
it as an integer. |
Stdio.h |
int puts( const char *s ) |
Prints the string s followed by a newline
character. Returns a non-zero integer if successful,
or EOF if an error occurs. |
Stdio.h |
int sprintf( char *s, const char *format, ... ) |
Equivalent to printf,
except the output is stored in the array s instead of printed on the screen.
Returns the number of characters written to s, or EOF if an error occurs. |
Stdio.h |
int sscanf( char *s, const char *format, ... ) |
Equivalent to scanf,
except the input is read from the array s rather than from the keyboard.
Returns the number of items successfully read by the function,
or EOF if an error occurs. |
Stdlib.h |
double atof( const char *nPtr
) |
Converts the string nPtr
to double. |
Stdlib.h |
int atoi( const char *nPtr
) |
Converts the string nPtr
to int. |
Stdlib.h |
long atol( const char *nPtr
) |
Converts the string nPtr
to long int. |
Stdlib.h |
double strtod( const char *nPtr,
char **endPtr
) |
Converts the string nPtr to double. |
Stdlib.h |
long strtol( const char *nPtr,
char **endPtr,
int base ) |
Converts the string nPtr to long. |
Stdlib.h |
unsigned long strtoul( const char *nPtr,
char **endPtr,
int base ) |
Converts the string nPtr to unsigned long. |
Ctype.h |
int isdigit( int c ) |
Returns a true value if c is a digit and 0
(false) otherwise. |
Ctype.h |
int isalpha( int c ) |
Returns a true value if c is a letter and 0
otherwise. |
Ctype.h |
int isalnum( int c ) |
Returns a true value if c is a digit or a
letter and 0 otherwise. |
Ctype.h |
int isxdigit( int c ) |
Returns a true value if c is a hexadecimal
digit character and 0 |
Ctype.h |
int islower( int c ) |
Returns a true value if c is a lowercase
letter and 0 otherwise. |
Ctype.h |
int isupper( int c ) |
Returns a true value if c is an uppercase
letter and 0 otherwise. |
Ctype.h |
int tolower( int c ) |
If c is an uppercase letter, tolower returns c as a lowercase letter. |
Ctype.h |
int toupper( int c ) |
If c is a lowercase letter, toupper returns c as an uppercase letter. |
Ctype.h |
int isspace( int c ) |
Returns a true value if c is a white-space
character—newline ('\n'), space (' '),
form feed ('\f'), carriage return ('\r'), horizontal tab ('\t') or vertical
tab ('\v')—and 0 otherwise |
Prototypes from: Dietel, Paul and Harvey Dietel. C How to Program. New York: Pearson, 2013