Kerala Plus One Computer Science Notes Chapter 9 String Handling and I/O Functions
Summary
String handling using arrays:
A string is a combination of characters hence char data type is used to store string. A string should be enclosed in double quotes. In C++ a variable is to be declared before it is used.Eg. “BVM HSS KALPARAMBU”.
Memory allocation for strings:
To store “BVM” an array of char type is used. We have to specify the size. Remember each and every string is end with a null (\0) character. So we can store only size- 1 characters in a variable. Please note that \0 is treated as a single character. \0 is also called as the delimiter.
char school_name[4]; By this we can store a maximum of three characters.
Consider the following declarations
- char my_name[10] = ”Andrea”;
- char my_name2[ ] = ”Andrea”;
- char str[ ] = ”Hello World”
In the first declaration 10 Bytes will be allocated but it will use only 6 + 1 (one for ‘\0’) = 7 Bytes the remaining 3 Bytes will be unused. But in the second declaration the size of the array is not mentioned so only 7 Bytes will be allocated and used hence no wastage of memory.
Similarly in the third declaration the size of the array is also not mentioned so only 12( one Byte for space and one Byte for ‘\0’) Bytes will be allocated and used hence no wastage of memory
Input/output operations on strings:
Consider the following code
#include<iostream>
using namespace std;
int main()
{
char name[20];
cout<<“Enter your name:”;
cin>>name;
cout<<“Hello “<<name;
}
If you run the program you will get the prompt as follows
Enter your name: Alvis Emerin
The output will be displayed as follows and the “Emerin” will be truncated.
Hello Alvis
This is because of cin statement that will take upto the space. Here space is the delimiter. To resolve this gets() function can be used. To use gets() and puts() function the header file stdio.h must be included. gets() function is used to get a string from the keyboard including spaces.
puts() function is used to print a string on the screen. Consider the following code snippet that will take the input including the space.
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
char name[20];
cout<<“Enter your name:”;
gets(name);
cout<<“Hello “<<name;
}
More console functions:
Stream functions for I / O operations:
Some functions that are available in the header file iostream.h to perforrn I/O operations on character and strings(stream of characters). It transfers streams of bytes between memory and objects. Keyboard and monitor are considered as the objects in C++.
Input functions:
The input functions like get( )(to read a character from the keyboard) and getline() (to read a line of characters from the keyboard) is used with cin and dot(.) operator.
eg:
#include<iostream>
using namespace std;
int main()
{
char str[80],ch=’z’;
cout<<“enter a string that end with z:”;
cin.getline(str,80,ch);
cout<<str;
}
If you run the program you will get the prompt as follows
Enter a string that end with z: Hi I am Jobi. I am a teacher. My school is BVM HSS The output will be displayed as follows and the string after ‘z’ will be truncated.
Hi, I am Jobi. I am a teacher
Output function:
The outputt functions like put() (to print a character on the screen) and write() (to print a line of characters on the screen) is used with cout and dot(.) operator.