are collection of characters. Unfortunately C does not has a primitive data type for strings and therefore an array of characters is used to represent a string. The following code snippet illustrates a string declaration. Notice that since a string is represented as an array of characters, it can be assigned a value using the equal s operator only when during the declaration. Consider the following two examples. The first example is correct while the second one is wrong: To assign a text value to a string, the function strcpy has to be used as shown in the example below. Note that in order to use this function, the header file (library) should be included in your program. This library contains other important functions that are related to string handling. Matthew Xuereb 2012/2013 1
In C, a string always ends with the null (\0) character. So if for instance the following string is declared: char name[10] = Joe ; the following will be stored in memory: Notice the null character (\0) at element 3. It is very important that every string will be long enough to allow C to put the null character at the end. Note that if there is not enough space for the null character then C will not know which is the end of the string an it can therefore display garbage characters on the screen when displayed. Some modern C compilers (such as the one used by MS Visual C++ Express) will generate and error. C standard string functions The basic C language comes with a variety of standard functions for processing strings. The following table and examples illustrates the mostly used functions with an example: Function Description Library strcpy Copies one string to another. <string.h> strlen Returns the length of a string. <string.h> strcat Adds the characters of one string to the end of another. <string.h> strcmp Compares to strings - case sensitive. <string.h> strnicmp Compares to strings - case insensitive. <string.h> strcspn Returns the position of a character in the string from a specified substring. <string.h> atoi Calculates the integer value of a string. <stdlib.h> atof Calculates the float value of a string. <stdlib.h> Matthew Xuereb 2012/2013 2
strcpy Copies one string to another. see example above strlen Returns the length of a string. strcat Adds the characters of one string to the end of another. Matthew Xuereb 2012/2013 3
strcmp Compares to strings - case sensitive. strnicmp Compares to strings - case insensitive. Note that with the function strnicmp, an extra argument is required. This argument indicates up till which number to compare case insensitive. Matthew Xuereb 2012/2013 4
strcspn Returns the position of a character in the string from a specified substring. atoi and atof These two functions return the integer and float values of a string respectively. C accessing a string as an array of characters As in C a string is an array of characters, it can be manipulated and accessed character by character. In the following example a string is accessed character by character and each character is displayed separately on a new line: Matthew Xuereb 2012/2013 5
Converting a string to uppercase In C, there is a function that can be used to convert a given character to uppercase. This is the toupper() function. It can be used in conjunction with a for loop to convert a particular string to uppercase. Consider the following example: Note that in order to use the toupper() function, the library ctype.h has to be included in the program as shown above. This library has a number of functions that can be used on a character and these are explained below. ctype.h The ctype library has two types of functions the to functions are used to convert the character to another character while the is functions checks for particular properties of a character. The following table lists the functions found in this library: Function isalnum(char) isalpha(char) iscntrl(char) Description Checks if a character is an alphabetic letter or a digit Checks if a character is an alphabetic letter Checks if a character is a control character Matthew Xuereb 2012/2013 6
isdigit(char) Checks if a character is a digit isgraph(char) Checks if a character is a pointing character except for the space character islower(char) Checks if a character is lowercase isprint(char) Checks if a character is a character that can be displayed on the screen ispunct(char) Checks if a character is a punctuation character isspace(char) Checks if a character is a is a which space isupper(char) Checks if a character is uppercase isxdigit(char) Checks if a character is a hexadecimal digit tolower(char) Converts a character to lowercase toupper(char) Converts a character to uppercase Consider the following example that checks whether an inputted character is a digit or an alphabetic character, or otherwise: Matthew Xuereb 2012/2013 7