Introduction to Algorithms and Data Structures Lecture 6 - Stringing Along - Character and String Manipulation
What are Strings? Character data is stored as a numeric code that represents that particular character. The most commonly used code is ASCII (American Standard Code for Information Interchange). A collection of characters that are read and written together to form words, numbers and so on are called strings. While strings are a built-in data type in some programming languages, this is not the case in C++.
How does C/C++ handle strings? In C, strings are stored as arrays of characters, where the end of the string is marked with an ASCII value of 0 (the NUL character). This is usually written as \0. Manipulating strings in C requires a little skill and the use of a library of functions, which begin str.
The C String Library The string library includes several functions that are quite useful: strcpy(s, t) - copies the contents of t into s strcat(s, t) - concatenates the contents of t to the strlen(s) end of s - returns the length of s strcmp(s, t) - returns a positive integer if s follows t in collating sequence - returns a negative integer if s precedes t in collating sequence - returns 0 if s and t are equivalent
strcpy strcpy is used to assign a value to a character string. The correct format is: strcpy(target_string,source_string); The statement #include <string.h> must appear above the first appearance of strcpy.
An Example of strcpy #include #include <iostream.h> <string.h> // Necessary if you're // using strcpy const int stringlen = 10; // main() - Show 2 examples of strcpy int main(void) { //Declare two string variables char s[stringlen], t[stringlen];
cout << "Type a word\t?"; cin >> t; strcpy(s, t);// Copy t's contents into s cout << s << endl; //It also works with literal strings strcpy(t, "something else"); cout << t << endl; } return(0);
strcat strcat is used to concatenate the contents of one character string to the end of another string. The correct format is: strcat(target_string,source_string); As in strcpy, the source can be a variable or a literal and the statement #include <string.h> must appear above the first appearance of strcat.
An Example of strcat #include #include <iostream.h> <string.h> // Necessary if you're // using strcat const int stringlen = 10; char s[stringlen], t[stringlen]; // main() - Show 2 examples of strcpy int main(void) { //Initialize the two strings strcpy(s, "Some"); strcpy(t, "thing");
//Concatenate the string strcat(s, t); cout << s << endl; //Concatenate the string again strcat(s, " else"); cout << s << endl; } return(0); Output Something Something else
strlen Occasionally you need to know how long a string is. strlen returns (as an integer value) the length of the string: #include <iostream.h> #include <string.h> const int stringlen = 10; int { } char main(void) s[stringlen]; strcpy(s, "Some"); cout << strlen(s) << endl; return(0);
strcmp strcmp returns an integer value that tells you which string comes first in the collating sequence: strcmp(s, t) is positive if s comes first strcmp(s, t) is negative if t comes first strcmp(s, t) is 0 if s and t are equivalent
An Example ofstrcmp #include <iostream.h> #include <string.h> const int stringlen = 10; int main(void) { char s[stringlen], t[stringlen], r[stringlen]; strcpy(s, "some"); strcpy(t, "thing"); strcpy(r, "rat");
//Is s > t? cout << strcmp(s, t) << endl; //Is s > r? cout << strcmp(s, r) << endl; } return(0); The output: -1 1
The ctype.h Library Another C/C++ library that is very useful is declared in the header file ctype.h. Most of the functions return a boolean value determining whether the character is of a certain type. These functions include: isalnum (c) true if c is alphanumeric isalpha (c) true if c is a letter (of either case) isdigit (c) true if c is a digit (0 through 9) islower (c) true if c is a lower case letter isspace (c) true if c is a white space character isupper (c) true if c is an upper case letter
isalpha(c) isalpha returns true if the character passed as a parameter is a letter; otherwise it returns false. The preprocessor #include <ctype.h> must appear above the first use of isalpha.
An Example of isalpha(c) #include #include isalpha <iostream.h> <ctype.h> // Necessary for using int main(void) { char c; } // Keep asking until you get a letter do { cout << "Enter a letter\t?"; cin >> c; } while (!isalpha(c)); return(0);
islower (c) islower returns true if the character parameter is a lower case letter; in all other cases (including upper case letters), it returns false. islower requires #include <ctype.h> islower allows the user to treat lower case and upper case letters differently.
An Example of islower #include <iostream.h> #include <ctype.h> // Necessary for using islower const int stringlen = 25; void allupper(char s[]); //A function that uses islower
// main() - A driver for allupper int main(void) { char s[stringlen]; //Input the word, convert to upper case // and print cout << "Type a word\t?"; cin >> s; allupper(s); cout << s << endl; } return(0);
// allupper() - Convert a string to entirely // upper case void allupper(char s[]) { int i; } // Convert any lower case character // to upper case for (i = 0; s[i]!= 0; i++) if (islower(s[i])) s[i] = s[i] - 'a' + 'A';
isupper(c) isupper returns true if the character parameter is a upper case letter; in all other cases (including lower case letters), it returns false. isupper requires #include <ctype.h> isupper allows the user to treat lower case and upper case letters differently.
An Example of isupper() // alllower() - Convert a string to entirely // lower case void allupper(char s[]) { int i; } // Convert any upper case character // to lower case for (i = 0; s[i]!= 0; i++) if (isupper(s[i])) s[i] = s[i] - A' + a';
isdigit(c) isdigit returns true if the character parameter is a digit; in all other cases, it returns false. isdigit requires #include <ctype.h> isdigit allows the user to read in a numeric input as a string, check to ensure it s a number and then convert it to a numeric data type. The string can be converted to int using atoi or to float using atof.
An Example using isdigit #include <iostream.h> // Necessary for using isdigit #include <ctype.h> // Necessary for using itoa #include <stdlib.h> const int stringlen = 25; int allnumeric(char s[]); // Is the number // solely digits?
// main() - A driver for allnumeric int main(void) { char numberstring[stringlen]; int x = 9999; //Input the word, convert to upper case // and print cout << "Type a number\t?"; cin >> numberstring; if (allnumeric(numberstring)) x = atoi(numberstring); cout << numberstring << " is equal to << x << endl; } return(0);
enum bool {false, true}; // Turbo C++ needs this // allnumeric() - Is the number solely digits? int allnumeric(char s[]) { bool answer = false; int i; for (i = 0; s[i]!= '\0'; i++) if (!isdigit(s[i])) break; if (s[i] == '\0') return(true); } return(false);
isalnum(c) isalum returns true if the parameter is either alphabetic or numeric (hence the name). It is useful in writing text scanners to spot variable names or in database programs to spot item codes.
isspace(c) isspace returns true if the parameter is a white space character (blank, newline or tab). It helps in spotting where words begin and end.
toupper(c)and tolower(c) A programmer needn t write their own function to convert characters from lower case to upper case or vice versa. There are standard functions in the ctype.h to do this: toupper(c) converts a lower case character to upper case; leaves it alone if it is anything else. tolower(c) converts an upper case character to lower case; leaves it alone if it is anything else.
An Example Using toupper(c)and tolower(c) #include #include <iostream.h> <ctype.h> //Handles up to a line of text const int stringlen = 81; // A prototype for the function void changecase(char s[]);
// main() - A driver program for ChangeCase int main(void) { char instring[stringlen]; //Read a word from the keyboard cout << "Enter a word\t?"; cin >> instring; } // Change the case of every letter and print changecase(instring); cout << "\"" << instring << "\"" << endl; return(0);
// ChangeCase() - Change the case of every // character in a string void changecase(char s[]) { int i; } // Character by character, look for lower case // character to convert to upper case; // otherwise convert to lower case for (i = 0; s[i]!='\0'; i++) if (islower(s[i])) s[i] = toupper(s[i]); else s[i] = tolower(s[i]);
The C++ String Class
A Very Simple String Example // Please note that there is no ".h" #include <iostream> #include <string> // Make the computer aware of standard // identifiers such as string using namespace std; int main(void) { string s = "concatenate"; } cout << s << endl; return(0);
Another Very Simple String Example #include #include <iostream> <string> using namespace std; int main(void) { string s("concatenate"), t; t = s; cout << t << endl; } return(0);
Initializing, Reading and Writing Strings
An Example of String Input/Output #include #include <iostream> <string> using namespace std; int main(void) { string s, t; //Prompt the user - read in two strings // separated by white space cout << "Enter two words with a space in << between\t?";
cin >> s; cin >> t; } cout << "\"" << s << "\"\t\"" << t << "\"" << endl; return(0);
Concatenating and Trimming Strings
An Example of Concatenation #include #include <iostream> <string> using namespace std; // Concatenating - Joining two character strings int main(void) { string r, s, t; // Prompt the user and read two words cout << "Enter a word\t?"; cin >> r;
cout << "Enter another word\t?"; cin >> s; } // Concatentate the words with a // blank in the middle t = r + " " + s; cout << "\"" << t << "\"\t" << endl; return(0);
Searching For a Substring
Searching for One of a Set of Characters
Retrieving A Single Character
Example: Writing Changing a Form Letter