INFORMATION FROM DR. EBRAHIMI'S C++ PROGRAMING EASY WAYS

Home                  ABOUT US                    CONTACT US                  TUTORIAL

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.

 

 

 

Text Box: #include<iostream>
using namespace std;
int main(){
char streetaddress[50];
cout<<"ENTER STREET ADDRESS: ";
cin>>streetaddress;
cout<<"STREET ADDRESS IS: "<<streetaddress<<endl;
return 0;
}//MAIN

  

 

 

 

 

 

 

 

 

 

Text Box: ENTER STREET ADDRESS: 71.charles.street.2c
STREET ADDRESS IS: 71.charles.street.2c
Text Box: Figure 17.1a – Program to take in a street address
 

 

 

 

 

 

 

Text Box: Figure 17.1b – Output of Figure 17.1a

  

 

 

 

 


 

TESTING FUNCTIONS OF ISTREAM

 

An istream has four state variables: goodbit, eofbit, failbit, and badbit that internally would be set based on the operation performed to the stream. Each of these state variables has an access function: good( ), eof( ), fail( ), and bad( ) respectively that returns the value of the state variable. In the following program, failbit is set when the invalid input is entered. The member function clear( ) resets the state variables.

Text Box: #include<iostream>
#include<cstdlib >
using namespace std;
int main(){
            double hourlyrate;
cout<<"Please enter your hourly rate: ";
            do {cin>>hourlyrate;
                if(cin.fail())
                        {
                         cout<<"Error\n";
                         exit(1);
                        }//IF
                else{cout<<"Please enter your hourly rate: ";
                        }//ELSE
            }while(1);//DO WHILE
            return 0;
}//MAIN

  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Text Box: Please enter your hourly rate: 5
Please enter your hourly rate: 6
Please enter your hourly rate: seven
Error
Text Box: Figure 17.2a – Example of cin.fail( )

  

 

 

 

 

 

 

 

Text Box: Figure 17.2b – Output of Figure 17.2a

  

 

 

 


 

PRINTING ASCII VALUES

 

The character A is 65 and character B is 66; can you figure out what character Z is? If you cannot figure it out, the following program demonstrates the ASCII code. You may not have figured out why NULL has an ASCII value of 0 and zero is 48, or why lower case character a is 97 which is larger than upper case A (65). I myself could not figure it out, but one could reason that in the early stages of computing only upper case letters were used and lower case was introduced later. The difference between lower and upper case letters is 32 and magically 32 is the ASCII code for blank space. Between upper case and lower case letters there are six special characters including bracket and backslash.

Moreover, the ASCII value for Σ is 169 and ½ is 189. You might want to try to print the extended ASCII characters and their values on your computer. This program displays the characters for the ASCII values 65 to 90 and displays the ASCII values for the lower case alphabet ‘a’ through ‘z’.

Text Box: #include<iostream>
using namespace std;
int main(){
    for (char c=65; c<=90 ; c++) 
    cout<<c<<" "; 
 
    cout<<endl;
    for (int i='a'; i<='z'; i++)
    cout<<i<<" ";
    return 0;
}//MAIN
Text Box: Figure 17.3a – Example using ASCII values
 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Text Box: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 
Text Box: Figure 17.3b – Output of Figure 17.3a
 

 

 

 

 

 

 

 


 

THREE VERSIONS OF GET( ) MEMBER FUNCTION

 

Any input data, whether it is from a keyboard or an input file, is a combination of characters. In order to extract each character, this member function of input stream istream is used in three ways. The member function get( ) with a character parameter stores the character being read in the parameter variable. Another version of get( ) has  no parameter, its return value is the character being read. Finally, the third version of get has an array of characters as its parameter where a string can be read. 

 

cin.get(c );       fin.get(c);       

 

c=cin.get( );     c=fin.get( );

 

cin.get(str);      fin.get(str);

 

Recall, fin is an object of the class ifstream; it stands for “file in” and is a nice user-convention as shown below:

 

            ifstream fin (“input.dat”);

 

 

HOW TO BUILD A STRING CHARACTER BY CHARACTER

 

In order to build a string, the function get( ) extracts one character at a time and assigns it to an array location. For example, the first character is assigned to the zero element of the array, and similarly the next character is assigned to the next element of the array. After the last character is assigned to the array, a NULL character is assigned to terminate the string. As a result, you can conclude that a string is an array of characters terminated by a NULL. It is important to note that an array of characters without NULL is not a string. Moreover, the difference between “A” and ‘A’ is that  “A” is an array of two characters consisting of ‘A’ and ‘\0’ while ‘A’ consists of a single character.

 

 

BUILDING A STRING USING get( ) AND A CHARACTER AS PARAMETER

 

The following program demonstrates how a string is created by using a get( ) function which gets a character and passes it as a parameter. For example, parameter c passes the character back in get(c). The eof( ) function checks for the end-of-file. 


 

 

Text Box: #include<iostream>
#include<cstdlib >
#include<fstream>
using namespace std;
int main(){
 char c;
 const int MAXSIZE=5;
 char str[MAXSIZE];
 int n = 0;
 do {cin.get(c);
        if(cin.eof()){ 
                             cout<<"BYE"<<endl; break; }//IF
             else{ str[n] = c;   n++; }//ELSE
 }while (n!=MAXSIZE); 
str[n]='\0';   
 cout<<"STRING IS :"<<str;
           return 0;
}//MAIN
 

  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Text Box: Figure 17.4a – Building a string using cin.get( )
Text Box: abc
STRING IS :abc  
 
abcde
STRING IS :abcde
 
 
Text Box: Figure 17.4b – Output of Figure 17.4a

 

 

 

 

 

 

 

 

 

 

 


 

get( ) MEMBER WITH IMPLICIT END-OF-FILE

 

In the following: cin.get(c), the function reads a character to variable c and returns a nonzero value. However, at the end-of-file it returns a zero that is used as the loop terminator.

Text Box: #include<cstdlib >
#include<fstream>
using namespace std;
int main(){ 
 char c;
 const int MAXSIZE=5;
 char str[MAXSIZE];
 int n = 0;
 while(cin.get(c)){
             if(n==MAXSIZE){str[n]='\0';break;}//IF
             str[n] = c; n++;}//WHILE
             cout<<"STRING IS :"<<str<<endl;
             retrun 0;
}//MAIN
Text Box: abc
STRING IS :abc  
 
abcd
STRING IS :abcd
 
Text Box: Figure 17.5a – Using cin.get( ) with implicit end-of-file
Text Box: Figure 17.5b – Output of Figure 17.5a

  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


 

int get() FUNCTION WITH NO PARAMETER RETURNS END OF FILE FLAG

Another version of the get( ) function does not carry any parameter but its return value is the character and also the end-of-file indicator. Since the end-of-file is –1, the return value is of type integer and not type character. This version is similar to the C version and can be used in situations when we want to count the number of characters or number of lines.  

Text Box: #include<iostream>
using namespace std;
int main(){
    int c;
    while ((c=cin.get())!=EOF){
    cout<<" ECHO "<<char(c)<<endl;
            }//WHILE
    cout<<" END OF FILE IS REACHED "<<endl;
     retrun 0;
}//MAIN

  

 

 

 

 

 

 

 

 

 

Text Box: hello
 ECHO h
 ECHO e
 ECHO l
 ECHO l
 ECHO o
 ECHO
Text Box: Figure 17.6a – Program using the int get( ) function

 

 

 

 

 

 

 

 


 


SKIPPING WHITE SPACE WITH get( ) 

 


 

Recall, the use of cin>>c without setting cin>>noskipws causes the white space before an input to be skipped. In contrast, the get( ) function reads the white space and to skip the white space, you must exclude the white space whenever you do not need it.

Text Box: #include<iostream>
using namespace std;
int main(){
            char c ;
            cin>>noskipws;
            while(cin>>c) 
                        cout <<c; 
return 0;
}//MAIN
Text Box: #include<iostream>
using namespace std;
int main(){
            char c ;
            while(cin.get(c)) 
                        cout <<c; 
return 0;
}//MAIN
 

  

 

 

 

 

 

 

 

 

 

Text Box: Figure 17.7a – Using cin>>noskipws
Text Box: Figure 17.7c – Using cin.get( )

  

 

Text Box: AB  CD
AB  CD
   ABCD
   ABCD
Text Box: Figure 17.7b – Output of Figure 17.7a
Text Box: AB  CD
AB  CD
   ABCD
   ABCD
Text Box: Figure 17.7d – Output of Figure 17.7c
 

 

 

 

 

 

 

 


 

COUNT THE NUMBER OF CHARACTERS: EXCLUDE THE WHITE SPACE

The following program counts the number of characters without counting the white space character (blank, new line, tab and vertical tab).

Text Box: #include<iostream>
using namespace std;
int main(){
            char c;
            int nc=0;
            while(cin.get(c)){
            if((c==' ')||(c=='\t')||(c=='\v'));
            else if(c=='\n') break;
            else nc++;}//WHILE
  cout<<"YOU ENTERED "<<nc<<" CHARACTERS"<<endl;
              return 0;
}//MAIN
Text Box: A      B CD
YOU ENTERED 4 CHARACTERS
Text Box: Figure 17.8a – Counting the number of input characters
Text Box: Figure 17.8b – Output of Figure 17.8a

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


 

ACCESSING ENTIRE LINE OR PORTION OF LINE

 

The function get( ) can access a portion of a line or an entire line of input accordingly and store it into a string variable. The general syntax for the get( ) function is get(stringvariable, maxsize, delimiter). The function get(str,5,’\n’) will read up to 5 characters of input or read up to the new line character, whichever comes first. In the following example: get(str,5,’,’); the delimiter is coma (,) instead of new line. It is important to observe that the get( ) function does not extract or store the new line or any delimiter. To solve this problem either use another get( ) function, ignore( ) function, or getline( ) function to handle the delimiter.

 

 

ignore() MEMBER FUNCTION

 

The ignore() function of input stream reads and discards up to a specified number of characters provided by its argument or stops and discards the delimiter, such as a new line.  If a delimiter is not specified, the EOF is considered the delimiter by default.

 

cin.ignore(20,’\n’);

 

In the above example, the ignore( ) function reads and discards up to 20 characters or stops when \n is reached and discards the new line. If the data is less than 20 characters long, all its characters are read. Two common uses of ignore( ) are after the get( ) string and if only certain fields of the data stream need to be extracted from the rest of the stream.

 

 

PORTION OR ENTIRE LINE: FUNCTION getline()

 

The function getline( ); can read a portion of a line or the entire line depending on the argument setting. The general form of the getline( ) function is: getline(stringvariable, maxsize, delimiter). The delimiter is a new line by default but others such as coma (,) can be used as well. Normally, maxsize is the size of the string but any other number less than maxsize can be used. The getline( ) function stops reading either when the maxsize characters is put into the array, including the null character, or upon encountering the new line, which ever comes first. It is important to note that getline( ) will extract the new line or delimiter and not save it (discards it).

 

 

DIFFERENCE BETWEEN getline( ) and get( )

 

The function getline( ) extracts the new line from the input stream and discards it while the function get( ) leaves the new line in the input stream and makes it the first character for the next input operation. It seems very confusing, but after some practice you will overcome this obstacle. Just remember, getline( )  gets the line and throws the delimiter out while the function get( ) stops with the delimiter.

Text Box: #include<iostream>
using namespace std;
int main(){
            char streetaddress[100];
            cout<<"ENTER STREET ADDRESS: ";  
            cin.getline(streetaddress,100);
            cout<<"STREET ADDRESS IS: "<<streetaddress<<endl;
            return 0;
}//MAIN

  

 

 

 

 

 

 

 

 

Text Box: Figure 17.9a – Program demonstrating the cin.getline( ) function

 

 

 


 

Text Box: ENTER STREET ADDRESS: 7 charles street 2c
STREET ADDRESS IS: 7 charles street 2c

 

 

 

Text Box: Figure 17.9b – Output of Figure 17.9a

 

 

 

 


 

 

Hosted by www.Geocities.ws

1