Plus One Computer Science Notes Chapter 9 String Handling and I/O Functions

Students can Download Chapter 9 String Handling and I/O Functions Notes, Plus One Computer Science Notes helps you to revise the complete Kerala State Syllabus and score more marks in your examinations.

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”.

Plus One Computer Science Notes Chapter 9 String Handling and I/O Functions

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.
Plus One Computer Science Notes Chapter 9 String Handling and IO Functions 1
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;
}

Plus One Computer Science Notes Chapter 9 String Handling and I/O Functions

More console functions:
Plus One Computer Science Notes Chapter 9 String Handling and IO Functions 2

Stream functions for I / O operations:
Somefunctions 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.
Plus One Computer Science Notes Chapter 9 String Handling and IO Functions 3
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

Plus One Computer Science Notes Chapter 9 String Handling and I/O Functions

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.
Plus One Computer Science Notes Chapter 9 String Handling and IO Functions 4