codescracker


c++

C++ Character String Functions



« Previous Tutorial Next Tutorial »


C++ treats a strings as a null-terminated array of characters. In a standard implementation, the string functions require the header file string.h to provide their prototypes. The character functions use ctype.h as their header file.

The header file string.h defines the size_t type, which is essentially the same as unsigned.

C++ Character Functions

Following table summarizes some important character functions. Remember, to use these functions, you must include the header file ctype.h :

Function Description
int isalnum(int ch) The isalnum() function returns nonzero if its argument is a letter or a digit.
If the character is not an alphanumeric, isalnum() returns zero.
int isalpha(int ch) This function returns nonzero if ch is an alphabet, otherwise it returns zero.
int isdigit(int ch) Returns nonzero if ch is a digit (i.e., 0-9).
Otherwise it returns zero.
int islower(int ch) Returns nonzero if ch is a lowercase letter ;
otherwise it returns zero.
int isupper(int ch) This function returns nonzero if ch is uppercase ;
otherwise it returns zero.
int toupper(int ch) Returns the uppercase equivalent of ch if ch is a letter;
otherwise, ch is returned unchanged.
int tolower(int ch) Returns the lowercase equivalent of ch if ch is a letter;
otherwise ch is returned unchanged.

Notice - In all the above functions, the argument type is int. The reason being that internally characters are processed numerically i.e., through their ASCII codes.

String Functions

Following table summarizes some important string functions. For these functions, string.h must be included in the program :

Function Description
char ∗strcat(char ∗str1, const char ∗str2) This functions concatenates a copy of str2 to str1 and terminates str1 with a null.
str1 should be large enough to hold both its original contents and those of str2.
int strcmp(const char ∗str1, const char ∗str2) This functions alphabetically compares two strings and returns -ve value if str1 is less than str2;
0 if str1 is equal to str2; and >0 (+ve value) if str1 is greater than str2.
char ∗strcpy(char ∗str1, const char ∗str2) Copies the contents of str2 into str1.
int strlen(char ∗str) Returns the length of the null-terminated string pointed to by str.
The null is not counted.

Examples

Here are some C++ programs listed, that you may like:


« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test