Kerala Plus One Computer Science Notes Chapter 9 String Handling and I/O Functions
Different console functions are available to perform input/output operations on strings.
String handling using Arrays
A string is a combination of characters. So a character array can be used to store a string, since it is a sequence of characters.
Memory allocation for Strings
The memory required depends upon the number of characters stored. A null character ‘\o’ is stored at the end of the string. This character is used as the string terminator and added at the end automatically. So the memory required to store a string will be equal to the number of characters in the string plus one byte for null character.
eg., char str[ ] = “Hello”;
Here the size of the array is not specified and hence only 6 bytes will be allocated (5 bytes for the string and 1 for ‘\o’).
Input/Output operations on strings
The function gets() is a console input function used to accept a string of characters including white spaces from the standard input device (keyboard) and store it in a character array. Its syntax is:
When we use this function, we have to include the library file stdio.h in the program.
A console function named puts() to output string data. It is used to display a string data on the standard output device (monitor). Its syntax is:
The string constant or variable (character array name) to be displayed should be provided to this function.
Console functions for character I/O
These functions require the inclusion of header file catdlo (stdio.h in Turbo C++ IDE) in the program. They are:
- getchar( ) function: This function returns the character that is input through the keyboard and stored in the variable.
eg.. char ch = getchar();
- putchar() function: This function displays the character given as the argument on the standard output unit (monitor).
Stream functions for I/O operations
These are input/output operations on character and strings. These are available in the header file iostream. These functions are generally called stream functions since they allow a stream of bytes (data) to flow between memory and objects.
1. Input functions: These functions allow the input of character and string data.
i. get( ): It can accept a single character or multiple characters (string) through the keyboard. To accept a string, an array name and size are to be given as arguments.
eg., ch=cin.get(ch); cin.get(ch). cin.get(str,10);
ii. getline( ): It accepts a string through the keyboard. The delimiter will be Enter key, the number of characters or a specified character.
eg., cin.getline(str,len); cin.getline(str,len,ch);
Here len is integer type that represents maximum number of characters that can be stored.
2. Output functions:
Allow a stream of > bytes to flow from memory into an output object.
i. put( ): It is used to display a character constant or the content of a character variable given as argument,
eg., cout.put(ch); cout.put(‘B’);
ii. write(): This function displays the string contained in the argument,
eg., char str=”hello”; cout.write(str,10);