CHAPTER 17

CHARACTER MANIPULATIONS, STRING CLASS, AND IOSTREAM

 

Each strike or multiple strikes on the keyboard represents a character: a letter, a digit, or a symbol. However, some characters are not labeled on the keyboard and

 some of them are not printable. Each character is represented by a numeric code that was agreed upon and standardized by a committee and this is known as

ASCII code. Characters are the building blocks of any data in a computer, whether they are integers, doubles, or strings. In fact, one of the first jobs of a system

programmer is to convert a sequence of characters to its intended meaning. For example, 1, 2, and 3 (one, two, three) becomes 123 (one hundred twenty three).

Similarly, a sequence of characters represents a word, several words form a sentence, and a combination of sentences generates a story.

A C–style string is an array of characters that terminates with a null as its last element. While you can initialize an array to a constant string such as char name[]=”ebrahimi”, you cannot treat the string as other built-in types such as int or double by using  = for assignment or comparison  (relational) operators such as > between two strings. Library functions strcpy( ) and strncpy( ) are used to copy (assign) one string to another. Similarly, library functions strcmp( ) and strncmp( ) are used to compare two strings for equality, less than or greater than.  Originally in C, string-handling functions were introduced in string.h and later on in C++, in cstring, include directive (header file).  Upon the introduction of string as STL in 1994, strings are now treated as other basic data types as a class with many more capabilities. String class provides a rich set of members for string manipulation that surpasses the traditional C-style null-terminated string. However, let us not forget that when it comes to speed, some of the C-style functions supersede the functions of string class. With the string class, operations are done in the same way as other operation on integer or float data types. Since string class is a STL container, many other components like iterators and algorithms work and makes   string manipulation and pattern matching easier because you don’t have to built the function from scratch.

 

 

STRIPPING THE WHITE SPACE WITH CONSOLE INPUT - cin

 

White space is considered a space, a tab, a vertical tab, or a new line. When receiving input using cin (an object of istream, which is also known as console input), the leading white space to an input is ignored. In several situations, ignoring white space is useful, but there are cases where white space is part of the data, such as a street address, where we do not want to break the address into separate units. One solution is to replace the white space with punctuation such as a dot (.) as demonstrated by the program below.  Additional characters to replace blank spaces can be cumbersome; you may argue that this is not a desirable solution and wish to know an alternative.

 

 

 

 

#include<iostream.h>

 

void main(){

char streetaddress[50];

cout<<"ENTER STREET ADDRESS: ";

cin>>streetaddress;

cout<<"STREET ADDRESS IS: "<<streetaddress<<endl;

}//MAIN