TRENDING NOW

no image
This Blog is particularly related to different programming subjects. It helps students of MCA, BCA, B.Tech, M.Tech, M.Sc(IT), PGDCA.
Character-Oriented I/O

We have discussed token-oriented input, performed when you use the operator  >> on streams and file streams.  Token-oriented input reads a token, an item (generally) separated from the next token by whitespace characters. We have also discussed line-oriented input, performed by getline(), which reads an entire line of input.  C++ has a third kind of I/O, character-oriented I/O, which reads in or prints out one character at a time.  Character-oriented I/O is performed by the functions get() and put().

The get() Function
The  stream input function specifically designed for input of a single character value is get().  In its most common form, this function reads a single character (including a whitespace character; remember that whitespace characters are spaces, tabs and newline characters) from the keyboard and returns it.  Typically, the value returned is assigned to a variable of type char.  

The general form of a call to get() to read a character from an input stream infile into a variable ch:

ch = infile.get();

• The stream can be a file stream, like infile, or it can be cin.
• If it attempts to read past the end of input, the stream variable gets a value indicating failure.

Why Do We Need get()?
You might ask why we need the get() function, since the extraction operator >> is capable of reading a character from a stream. The answer is that the extraction operator will not read whitespace characters. Suppose you want to read all the characters in this line and count them:

This is the string.
If you were to read this string character by character with the >> operator and count the characters, you would read in 16 characters. If you were to read it character by character with get(), you would read in 19 characters, counting the spaces. (And if you were to read it with getline(), you'd read the whitespace characters, but you'd read the whole string at once; and you couldn't count the characters as they are being read in.) 

no image
This Blog is particularly related to different programming subjects. It helps students of MCA, BCA, B.Tech, M.Tech, M.Sc(IT), PGDCA.
Data Type string::size_type

Many C++ string member functions return a numeric value which is not of type int.  The member functions size() and length() return a value which has type string::size_type. This is an unsigned integer type. (An unsigned integer can never be negative.) Suppose I want to store the length in a variable. I must declare the variable. It is more precise to declare it as type string::size_type rather than int. Note that to use this data type, size_type must be preceded by the word string and two colons:

string state = "New York";
string city = "Cincinnati";
string::size_type num1, num2 ;

num1 = state.length();  num1 is 8 (the space is counted)
num2 = city.size();     num2 is 10

Notes:
• We are using the names of the strings--state and city--to indicate what string object [variable] the member function should act upon. [to whom the function belongs?]
• It is safest to use string::size_type when referring to string sizes or to positions in a string.
no image
This Blog is particularly related to different programming subjects. It helps students of MCA, BCA, B.Tech, M.Tech, M.Sc(IT), PGDCA.
String functions:

The string class is a pre-defined class. Think of it as a type like int or double.)  
Many pre-defined classes in C++ have functions associated with them, called methods or member functions of the class. Member functions define the operations that can be performed on [?by] objects of the class. We've already used several member functions of other classes: in Chapter 3, we introduced the iostream member functions cout.precision() and cout.width(). These member functions work on cout, which is an object of the iostream class. We also introduced the fstream member functions open() and close() which work on files, objects of the fstream class. 

A string variable is an object of the string class.  Among its most useful member functions are length(), substr(), find(), insert(), and erase().

The length of a string is the number of characters currently stored in the string.  There are two member functions that will return this value: length() and size(). (They do the same thing.)

To call a member function, we use dot notation:   object.method();  
You’ve seen
cout.precision()
cout.width()
   
Now we’ll have 
city.length()   

Example:
string city=”Queens”; 
cout << city.length(); 
Will print 6, the number of characters stored in city.
no image
This Blog is particularly related to different programming subjects. It helps students of MCA, BCA, B.Tech, M.Tech, M.Sc(IT), PGDCA.
String operationsConcatenation and Comparing Strings

String operations in C++ are performed using some of the same operators that are used for arithmetic operations. The symbols are interpreted slightly differently in some cases. We've already seen the use of the = operator for assignment. 

Concatenating Strings:
It is possible to join two or more strings together using the + operator. Rather than addition, this use of the + operator joins one string to the end of another; this operation is called concatenation.
string str1, str2, str3;
str1 = "yesterday";
str2 = "morning";
str3 = str1+ " " + str2;
cout << str3 << endl; 
String str3 has the value "yesterday morning", and that value is sent to cout. 
Notes:
• It is necessary to concatenate a space between the two words to produce a space in the resulting string.  
• At least one of the operands must be a variable. [Useless to concatenate two literals.]
• The use of an operator (like +) for two different actions is called operator overloading. [not responsible this term]
string str = "train"; 
str = str + 's';
str has the value "trains". 
Note that it is also possible to use the += operator to perform concatenation:
str += 's';

Comparing Strings
You can compare two strings using the standard relational operators (<, <=, >, >=, ==, and !=). Two strings are the same (equal to each other) if they have the same number of characters and if each corresponding character matches: e.g., "cat" is the same as "cat" but not the same as "act", "Cat" or even as "cat " (with a space at the end). 
string str1 = "cat";
string str2 = "dog";
if (str1 == str2)
cout << "alike" << endl;
else
cout << "different" << endl;
   
This will, of course, print "different"  because "cat" is not the same string as "dog".  
Strings can be compared to determine whether one is greater than or less than the other. 
no image
This Blog is particularly related to different programming subjects. It helps students of MCA, BCA, B.Tech, M.Tech, M.Sc(IT), PGDCA.
Declaring, Initializing, Printing, and Reading Strings

Now we’ll learn to store string values. C++ has a string class. Think of it as a type like int or double. This class allows one to declare objects (which we will treat like variables of data type string) and provides methods (or member functions) for manipulating strings. To use strings, you must #include<string>. The string header file includes the prototypes for the string functions, just as cmath contains the prototypes for mathematical functions, and iostream contains the prototypes for I/O functions.

Declaring, Initializing, Printing, and Reading Strings:

#include <iostream>
#include <string>
int main()
{
string str1, str2;
str1 = "Hello";
str2 = "Harry";
cout << str1 <<  ",  " < <  str2 << endl;
return 0;
}
This will print "Hello, Harry" and go to a new line.

You can 
• Initialize a string in the declaration
• Assign the value of one string to another, 
• Send a string to cout 
• Read in a string from cin – don’t need to type the quotes

But cin stops reading as soon as a whitespace (space, tab, or newline) character is encountered. This is quite limiting if you want to read in someone's full name or a sentence. 

getline(input_stream, string_variable, delimiting_character);

getline() reads characters (including whitespace characters) from the input_stream into the specified string_variable until the delimiting_character is encountered. The third parameter is optional. If it is omitted, the default is the newline character.

string name;
    
getline(cin,name);
cout << name << endl;

reads until it encounters the newline (i.e. the user hits "Enter").  The ‘\n’ is read but not stored in name.
The optional third parameter can be any character used as a delimiter. getline() reads until it encounters the delimiter, which is not stored in the string.

string first, second;
    
getline(cin, first,',');
getline(cin, second,'D');

Thus, if the line typed in was "Hello, John Doe" followed by a newline character, first would get the value "Hello" and second would get " John ", including space before 'J' and the space after 'n'. Neither string would include the comma because that was the delimiter.

ignore() Warning, warning, warning: 
Not in the book, but necessary: if you try to use getline after >>, it will read whatever is left on the line, even if it is just a newline character. You have to use the function ignore() (in  <iostring>) to ignore the rest of the line and go to the beginning of the next line.

cin.ignore(numchars,char);
ignore() skips the number of characters specified or all of the characters up to and including the character specified by the second parameter, whichever comes first.
For example,

cin.gnore(80,’\n’);
skips 80 characters or goes to the beginning of the next line.
Use infile.ignore(100,’\n’); if you are reading from infile rather than the keyboard.
This Blog is particularly related to different programming subjects. It helps students of MCA, BCA, B.Tech, M.Tech, M.Sc(IT), PGDCA.


The data type is a way to classify various types of data such as integer, string, etc. which determines the values that can be used with the corresponding type of data, the type of operations that can be performed on the corresponding type of data.The data in the data structures are processed by certain operations. The particular data structure is chosen largely depends on the frequency of the operation that needs to be performed on the data structure.
This Blog is particularly related to different programming subjects. It helps students of MCA, BCA, B.Tech, M.Tech, M.Sc(IT), PGDCA.


In computer science, a linked list is a linear collection of data elements, in which linear order is not given by their physical placement in memory. Each pointing to the next node by means of a pointer. It is a data structure consisting of a group of nodes which together represent a sequence.