130 likes | 155 Views
Learn how to read in characters and perform operations on character data in C++. Explore different methods and functions to read and manipulate characters efficiently and effectively.
E N D
CS161 Introduction to Computer Science Character Data
How do I read in Characters? • Until now, we have read in character data using the following format: char ch; cin >> ch; • This skips leading whitespace, • reads in 1 character and stores that character in variable ch
How do I read in Characters? • An alternate form of reading in characters come in the form of the cin.get function: char ch; ch = cin.get(); //or cin.get(ch); this reads in the next character from the input buffer, even if that next character is whitespace!
How do I read in Characters? • There are two different versions of the get function that read single characters. • We can supply no arguments to this function and receive the next character in the input stream as the returned value. • Or, we can supply a single character argument and receive the next character in the input stream through the argument (via call by reference).
How do I read in Characters? • The advantage of using either of these functions is that we are able to read and store whitespace characters. • If the next character in the input stream is a space, the character received will be a ' '. • If the next character in the input stream is a newline, the character received will be a '\n', etc. • Therefore, we can read and echo a single line of text exactly as the user types it in.
How do I read in Characters? • The following two loops are equivalent and demonstrate the differences that we must adjust for when using these two versions of the get function: while ((ch = cin.get()) != '\n') cout << ch; while (cin.get(ch) && ch != '\n') //accomplishes the same cout << ch;
How do I read in Characters? • For the most part, it is purely style which version of the get function we use. • However, there are times when one version is more efficient than the other. • For example, when no argument is supplied, the character read is the residual value and therefore can be used within a greater expression.
How do I read in Characters? • This version works efficiently when we want to loop, flushing the input buffer until a newline is encountered: //flush buffer until newline while (cin.get() != '\n'); • This is handy when there is a possibility of the user entering more characters than are expected • It reads and "throws out" all additional characters up until (and including) the newline.
How do I read in Characters? • On the other hand, the one argument version of the get function is useful when we want to detect whether or not the input operation has been successful or not. • It returns a success or fail indication. It returns a non-zero value (i.e., an object of class istream) if the character is read correctly and a zero if the input operation fails or an end of file occurs.
How do I read in Characters? • The following loop demonstrates this by reading characters (and echoing them) until end of file. char ch; //read until end of file (input fails) while (cin.get(ch)) cout <<ch;
Operations on Character Data • With external libraries we can peform a number of operations on character data • We can determine if a character is alphabetic, punctuation, a digit, upper case, or lower case using the ctype.h library • All of the functions in the ctype.h library use call by value, which means none of the arguments can be modified by the functions
Operations on Character Data • The ctype.h functions most useful are: • isalpha(ch) which returns true (non-zero) if the value is either an upper or lowercase character and false (zero) otherwise. • isdigit(ch) which returns true if the character represents one of the digits between 0-9 and false otherwise. • ispunct(ch) which returns true if the character is any printable character, except a space, digit, or alphabetic and false otherwise. • islower(ch) which returns true if the character is a lower case alphabetic character and false otherwise. • isupper(ch) which returns true if the character is an upper case alphabetic character and false otherwise. • Plus many more!
Operations on Character Data • The ctype.h functions also include those we learned about earlier this term: tolower and toupper. • It is important to realize that these functions are also pass by value • Which means it is impossible for tolower and toupper to modify the value of the argument; the character you send in as an argument remains unchanged • Therefore, when using tolower and toupper, you must either • Use them directly in a logical expression as we learned earlier, or • Save the results in a variable: char result, ch; result = tolower(ch);