A string is any sequence of characters enclosed in double quotes. For example, "hello world", "C++ programming class", and "x = y + 6" are all strings.
A string is stored as an array of characters terminated by a special end-of-string marker called the null character. The null character, represented by the escape sequence \0, is the sentinel marking the of the string. The figure below illustrates how the string "Hello World" is stored in memory.
H | e | l | l | o | W | o | r | l | d | \0 |
A string can be created by using a pointer or an array of characters.
Using pointer char * string_name;
where: string_name is a pointer.
Example: char *lastname;
char * firstname;
Using array char string_name[size];
where: size is the number of characters including the null character \0 stored in the array..
Example: char lastname[30];
char firstname[20];
STRING DECLARATION AND INITIALIZATION
A string can be declared and initialized by using a pointer or an array of characters.
Using pointer char * string_name = "string_value";
where: string_name is a pointer.
Example: char *lastname = "Nguyen";
char * firstname = "Phuong";
Using array char string_name[size] = "string-value";
where: size is the number of characters including the null character \0 stored in the array..
Example: char lastname[30] = "Nguyen";
char firstname[20] = "Phuong";
Table below lists the commonly available library functions for string input and output
C++ Routine | Description |
cout | String output to the screen |
cin | String input from the keyboard, but cin does not input a space character. It stops input a string when it reads a space character. |
cin.getline(str, length,char) | String input from the keyboard. cin.getline( ) read a space character. str - a string of character pointer or a character array. length - an integer constant or a variable indicating the maximum number of input characters including the null character. char - an optional character constant or variable specifying the terminating character. If this optional third argument is omitted, the default terminating character is the newline (\n) character. Pressing the enter key generates a newline character. A statement such as cin.getline(message,80,'x') will stop accepting characters whenever the x key is pressed. |
chr = cin.get( ) | Input single character from the keyboard. chr - a character constant variable |
Example | Output |
#include<iostream.h> void main( ) {char message1[80]; char *message2; cout <<"Enter a string for message1: \n"; cin.getline(message1,80); cout << "Enter a string for message2: \n"; cin.getline(message2,80); cout <<message1<< " and " <<message2; } |
Enter a string for message1: Good morning Enter a string for message2: have a nice day Good morning and have a nice day |
Extensive collections of string-handling functions are included with all C++ compilers. The common of these are listed below. To call these functions, you need to include the header file <string.h> in your program.
String copy
strcpy(string1,string2) - Copies string2 to string1. String1 needs to have enough space to store string2. The
strcpy will overwrite string1.
Example: char message1[80] ="Good morning", message2[80]="Hello World";
cout <<message1<<endl;
strcpy(message1,message2);
cout << message1;
Output
Good morning
Hello World
String concatenation
strcat(string1,string2) - concatenates string2 to string1. String1 needs to have enough space to append
string2.
Example: char message1[80] ="Good morning", message2[80]=" and have a nice day";
cout <<message1<<endl;
strcat(message1,message2);
cout << message1<<endl;
cout << message2;
Output
Good morning
Good morning and have a nice day
and have a nice day
String comparison
strcmp(string1, string2) - Compares string1 to string2. Returns a negative integer if string1<string2, 0 if string1
is equal to string2, and a positive integer if string1 > string2.
Example: char message1[80] = "It's a cow";
char *mesaage2 = "It's a Cow";
if ( strcmp(string1,string2) )
cout << "string 1 is greater than string2";
else
cout << "string1 is smaller than string2";
Ouput
string 1 is greater than string2
Note: When strcmp compare the character c in string1 with the character C in string2. The character c is greater than the character C because the asscii value of character c is 99 and the asscii value of character C is only 67. See the Appendix B ASCII character set in the back of your text book.
String length
strlen(string1) - Return the length of the string, excluding the null character
Example: char message[80] = "Hello world";
int i;
i = strlen(message);
cout << i << " characters";
Output
11 characters
Note: a space is counted as one character.
C++ provides several functions that allow you to test and manipulate character data. The function prototypes are found in the header file name <ctype.h>. Remember to add the line #include <ctype.h> in program that use these functions. The table below lists and describes the character functions. Each function expects one integer argument - the ASCII value of the character to be tested. Each function returns a non-zero value (true) if the condition tested is true and 0 (false) if the condition tested is false.
C++ Functions | Description |
isalpha(character) | Returns a nonzero number if the character is a letter ('A' - 'Z', 'a' - 'z'); otherwise it returns zero. |
isalnum(character) | Returns a nonzero number if the character is a letter ('A' - 'Z', 'a' - 'z', or '0' - '9'; otherwise it returns zero. |
isdigit(character) | Returns a nonzero number if the character is digit (0 through 9); otherwise it returns a zero. |
isspace(character) | Returns a nonzero number if the character is a whitespace (tab, space, newline); otherwise it returns a zero. |
isupper(character) | Returns a nonzero number if the character is uppercase; otherwise it returns a zero. |
islower(character) | Returns a nonzero number if the character is lowercase; otherwise it returns a zero. |
toupper(character) | Return the uppercase equivalent if the character is lowercase; otherwise it returns the character unchanged. |
tolower(character) | Return the lowercase equivalent if the character is uppercase; otherwise it returns the character unchanged. |
C++ Functions | Description |
toupper(character) | Return the uppercase equivalent if the character is lowercase; otherwise it returns the character unchanged. |
tolower(character) | Return the lowercase equivalent if the character is uppercase; otherwise it returns the character unchanged. |
atoi(string) | Converts an ASCII string to an integer (include #<stdlib.h> in your program) |
atof(string) | Converts an ASCII string to an float (include #<stdlib.h> in your program) |
The example below will convert each lowercase character of a string to uppercase character and vice versa.
Example |
#include<iostream.h> #include<ctype.h> #include<string.h> void main( 0 { char name[20]; cout<<"Enter your name:\n "; cin.getline(name,20); for( int i = 0; i < strlen(name) ; i++) { if (islower(name[i]) ) //convert to uppercase name[i] = toupper(name[i]); else //convert to lowercase name[i] = tolower(name[i]); } //Display the result cout << "The conversion is:\n"; cout << name << endl; } |
Output |
Enter your name: Phuong D. Nguyen The conversion is: pHUONG d. nGUYEN |
You can rewrite the example above using the pointer.
Example |
#include<iostream.h> #include<ctype.h> #include<string.h> void main( 0 { char *name; cout<<"Enter your name:\n "; cin.getline(name,20); for( int i = 0; i < strlen(name) ; i++) { if (islower(*(name + i) ) ) //convert to uppercase *( name + i) = toupper(*(name + i)); else //convert to lowercase *( name + i) = tolower(*(name + i)); } //Display the result cout << "The conversion is:\n"; cout << name << endl; } |
Output |
Enter your name: Phuong D. Nguyen The conversion is: pHUONG d. nGUYEN |