Characters & Strings
Home Up ComputingConcepts Definitions Characters & Strings C_Formatted IO C_Structs_Enums_Files FunctionsAndMenus Arrays and Cmd Line Args Arrays & Structures Structs, Enums, Ptr Variables Ptrs. to External Files Miscellaneous Random Files

 

C Programming
Characters and Strings

This page includes some library functions for handling characters and strings. The next page has information on Formatted I/O, including conversion specifiers and escape sequences.
 

Every program is essentially a sequence of characters that, when grouped together meaningfully, is interpreted by the computer as a series of instructions to perform a task.
A character constant is an int value represented as a character in single quotes; its value is the integer value of the character in the machine's character set.
Note that the character handling functions manipulate the characters as integers.
A string is a series of characters treated as a single unit. String constants are written in double quotation marks. A string is an array of characters ending in the null character ( ' \ 0 ' ), and is accessed by the pointer to the first character in the string.
The use of function libraries is a means of achieving software reusability.

CHARACTERS AND STRINGS

When using the following functions from the character handling library,
include the  <ctype.h>  header file.


PROTOTYPE FUNCTION DESCRIPTION
int isdigit (intc) Returns a true value if c is a digit, and 0 (false) otherwise.
int isalpha (int c) Returns a true value if c is a letter, and 0 otherwise.
int isalnum (int c) Returns a true value if c is a digit or a letter, and 0 otherwise.
int isxdigit (int c) Returns a true value if c is a hexadecimal digit character, and 0 otherwise.
int islower (int c) Returns a true value if c is a lowercase letter, and 0 otherwise.
int isupper (int c) Returns a true value if c is an uppercase letter, and 0 otherwise.
int tolower (int c) If c is an uppercase letter, tolower returns c as a lowercase letter. Otherwise, tolower returns the argument unchanged.
int toupper (int c) If c is a lowercase letter, toupper returns c as an uppercase letter.  Otherwise, toupper returns the argument unchanged.
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.
int iscntrl (int c) Returns a true value if c is a control character, and 0 otherwise.
int ispunct (int c) Returns a true value if c is a printing character other than a space, a digit, or a letter, and 0 otherwise.
int isprint (int c) Returns a true value if c is a printing character including space ( ' ' ), and 0 otfherwise.
int isgraph ( int c ) Returns a true value if c is a printing character other than space ( ' ' ), and 0 otherwise.

more on characters and strings

When using the following functions from the standard input/output library,
include the <stdio.h> header file.


int getchar (void) Input the next character from the standard input and return it as an integer.
char *gets (char *s) Input characters from the standard input into the array s until a newline or end-of-file character is encountered. A terminating NULL character is appended to the array.
int putchar (int c) Print the character stored in c.
int puts(const char *s) Print the string s followed by a newline character.
int sprintf(char *s, const char *format, ...) Equivalent to printf except the output is stored in the array s instead of printing on the screen.
int sscanf(char *s, const char *format, ...) Equivalent to scanf except the input is read from the array s instead of reading from the keyboard.

STRING MANIPULATION and COMPARISON

When using the following functions from the string handling library,
include the <string.h> header file.


char *strcpy (char *s1, const char *s2) Copies the string s2 into the arrray s1. The value of s1 is returned.
char *strncpy (char *s1, const char *s2, size_t  n) Copies at most n characters of the string s2 into the array s1. The value of s1 is returned.
char *strcat (char *s1, const char *s2) Appends the string s2 to the array s1. The first character of s2 overwrites the terminating NULL character of s1. The value of s1 is returned.
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 the terminating NULL character of s1. The value of s1 is returned.
int strcmp (const  char *s1, const char *s2) Compares the string s1 to 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.
int strncmp (const char *s1, const char *s2,
size_t n)
Compares up to n characters of the string s1 to 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.
size_t strlen (const char *s) Determines the length of string s. The number of characters preceding the terminating NULL character is returned.

 

Back Home Up Next

Hot Links

 ITKnowledge

Blue Mountain Arts
Netscape
 Microsoft Home Page

 

The next page will contain information about Structures, Unions, Bit Manipulations, Enumerations, and File Processing with the C language. There are many more subjects that could be covered, but due to lack of time and space they are not included in this web site.

If you have comments or suggestions, email me at MarliJordan@ispchannel.com

This page last updated 9/6/00.