0% found this document useful (0 votes)
2 views

Hsslive-xi-cs-siraj-09 String Handling and IO Functions

Chapter 9 discusses string handling in C++, explaining that strings are represented as character arrays terminated by a null character. It covers various input/output functions for strings, such as 'cin', 'gets()', and 'puts()', and highlights memory allocation for character arrays. The chapter also includes examples and questions related to string manipulation and memory usage in C++.

Uploaded by

haasinijv2008
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Hsslive-xi-cs-siraj-09 String Handling and IO Functions

Chapter 9 discusses string handling in C++, explaining that strings are represented as character arrays terminated by a null character. It covers various input/output functions for strings, such as 'cin', 'gets()', and 'puts()', and highlights memory allocation for character arrays. The chapter also includes examples and questions related to string manipulation and memory usage in C++.

Uploaded by

haasinijv2008
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Chapter 9: String Handling and I/O Functions

C++ does not have a basic data type to represent string data. Using 'char' data type we can declare one
character at a time. A string can be defined as a character array that is terminated by a null character. The
null character '\0' is stored at the end automatically. അക്ഷരങ്ങളുടെ കൂട്ടമാണ് String. ഒരു String

ന്റെ
അവസാനത്തെ ക്യാരക്ടര്‍ null character [\0] ആണ്. ഇത് കീബോഡില്‍ എന്റര്‍ കീ പ്രസ്സ് ചെയ്യുമ്പോഴാണ്
വരുന്നത്. string array യും initialize ചെയ്യാം. ഉദാ char code [5] = { 'g' , 'o', 'o', 'd' , '\0' }; So the
character arrays are declared one character longer than the largest string that can hold.
We can initialise a character array as follows.
char name[10]=”babu”; Here 10 memory locations will be allocated but the last 5 bytes will be left
unused.
char str[] = “hello world “; here size of the array is not specified
and so 12 bytes (11 bytes for string and 1for '\0' ) are needed.
To input a string and display
Memory allocation for the character array. #indlude<iostream>
memory allocation of a character array char c[6] using namespace std;
c[0] c[1] c[2] c[3] c[4] c[5] int main( )
M O H A N \0 {
char my-name[15];
Size of a char data type is one. A null character is stored at the end cout<<”Enter your name “;
of a string. cin>>my_name;
cout<<”Hello “<<my_name;
Input / Output operation of a string: Consider the program return 0;
In this example, on executing the program let the name be 'MAYA DAS ' . }
The output will be 'Hello maya' . This statement can store a string without
any white space (that is, only the first word).
A white space is treated as a separator of data. are two separate words. If we want store strings having
more than one word we can use functions like ' gets( ) ' ,. The syntax is ' gets(char_array_name) '. Here
we should include the header file 'cstdio. The 'gets( )' function helps to input of strings with white space.
C++ ല്‍ data കള്‍ input ചെയ്യുമ്പോള്‍ white space (blank space ) , data യെ വേര്‍തിരിക്കാന്‍
ഉപയോഗിക്കുന്നു. string കള്‍ input ചെയ്യുമ്പോള്‍ cin എന്ന object ആണ് ഉപയോഗിച്ചതെങ്കില്‍
ആദ്യത്തെ വാക്കിന് ശേഷമുള്ള വാക്കുകള്‍ സ്വീകരിക്കാന്‍ കഴിയാതെ വരുന്നു. ഇതിന് പരിഹാരമായി cin ന്
പകരം gets( ) എന്ന function ഉപയോഗിക്കുന്നു. Out put ചെയ്യാന്‍ puts( ) എന്ന function
ഉപയോഗിക്കുന്നു.
Similarly the function named 'puts( ) ' is used to output string data.
Syntax is puts(string_data);
The above program can be modify as follows
The output of the program will display in two lines. If we use the code To input a string and display
cout<<”hello”; cout<<name; instead of puts( ) function, the output will be #indlude<iostream>
the same line without a space. #include<cstdio>
More console functions on characters : using namespace std;
The header file ' cstdio ' must be included. int main( )
getchar( ) function: { char name[15];
it is a character function. It reads a character from the key board. cout<<”Enter your name “;
Eg: gets(name);
puts(“Hello “);
char ch;
puts(name);
ch=getchar( ); return 0;
cout<<ch; }
getch( ) function :
The typed (input) character will not be visible on the screen.
Eg:
ch= getch();
The typed variable is stored in the variable ch. Enter key need not be pressed to delimit the data.
getche( ) function :
[ get a character and echo it ] the typed character will be stored and echoed on the screen.
Eg: ch= getche();
putchar( ) function :

Displays the character on the screen.


Eg: char ch;
ch=getchar( );
putchar(ch); display the typed character on the screen.
Or putchar('A'); The character 'A' is displayed.
putch( ) function: same as above.
STREAM FUNCTIONS FOR I/O operations: The header file ' iostream ' must be included. This
functions allow the input of character and string data. Keyboard and monitors are considered as object in
C++. These functions transfers stream of bytes between memory and objects. Here the object ' cin ' is
used to refer the keyboard and the object ' cout ' is used to refer the monitor.
get( ) function: it is used to read a single character.
Syntax is cin.get(variable);
The period symbol is called dot operator. It is used between the object ' cin ' and the function.
getline( ) function : Input function for a string and enter key is the delimiter.
Eg. cin.getline(str, len, ch); where ' str ' is a character array, ' len ' is the maximum number
of characters that is to be stored, ' ch ' is the delimiting character.
put( ) function : It is used to display a character.
Eg. cout.put(ch);
write( ) function: Used to display a string.
Eg. cout.write(str, len); where ' str ' is an array and size ' len '.
Questions:
1. Consider the following C++ code If the input string is “Computer Programming”; what will be the
output ? justify your answer.

Char text[20];
cin>>text;
cout<<text;

2. Explain gets( ) and puts( ) functions ? (2)


3. Explain how allocation of string takes place in memory ? (2)
4. Which character is known as string terminator?
5. Which header file is required to use gets( ) function ?
6. How many bytes are required to store the string I LOVE MY INDIA
7. What is the difference between getch( ) and getche( ) function?
8. Differentiate between cin and getline?
9. Write a program to read a string and display the number of characters ?
10. How many bytes will be allocated in the memory to store
(a) the string “MY SCHOOL” (b) int m[3] (c). int a[3][2 ]
{ (a). 10 bytes. (b) 3 X 4 = 12 bytes. (c). 3 X 2 X 4 = 24 bytes }
11. Consider the following C++ statements.
Int A[4] = { 1, 2, 3, 4 }; int B = ( A[3] + A[0] + A[2] ) / A[1];
Predict the value of the variable B (Mark 1) [ Ans: 4 ]
12. Write a statement for the storing of the string “NO SMOKING” using a character array with name
ARR of minimum size?
(char ARR[11] = “NO SMOKING” or char ARR[ ] = “NO SMOKING” )
13. What will be the out put of the following code segment if the input is “Computer Application “?
char WORD[15];
cin>>WORD;
cout<<WORD;
( Computer - for ' cin' , a white space is treated as a separator of data )
14. The following code does not give 6 as the output. Why?
char N[ ] = “123”;
cout<<(N[0] + N [1] + N[2] );
(Since N is a character array the individual elements will be treated as characters. So addition is
not possible. ASCII value will be added. 49 + 50 + 51 = 150 )
15. Write array declaration for the following?
(a). To store the heights of 60 students in your class. (b). To store name of your school (c) To store all
odd numbers from 2 to 20.
16. Consider the following cases and predict the output?
(a). char ch[ ] = “hai”; cout<<”hello”; cout<<ch;
(b) char ch[ ] = “hai”; puts(”hello”); puts(ch);
(a). hellohai ( printing in the same line ).
(b). Prints in separate lines ) ]
17. if str[50] is a character array in C++ , what is wrong in each of the following function usages?
a). cin.gets(str); (b). cout.put(str, 50); c). str = cin.getline( ); d). cout<<puts(str);
[a)cin is not required. b)it is a character output function. c)Cannot assign value to str
d)puts( ) does not return any value]
18. Assume that str[50] is a character array in C++. Now observe the following statements:
( i ). gets str( ); (ii). Cin.get(str, 50); (iii) cin.getline(str); (iv) puts(str);
Which of the above statements are true.Choose the correct answer from the following.
(a). statements (i), (ii) & (iii) only. (b)statements (i), (ii) & (iv) only.
(c). statements (i) & (iv) only. (d). all the statements are true. [ Ans:b ]
19. Match the following.

A B C
a). getchar( ) i. String input 1. just a key stroke is enough a – iii – 4
b - iii – 1
b). getch( ) ii. String output 2. string having white spaces c–i–2
c). gets( ) iii. Character input 3. includes new line as the last character while spaces d – iv – 5
e – ii - 3
d). putchar( ) iv. Character 4. only the first character of the typed string is stored
output
e). puts( ) 5. if the parameter is 65, the output is A

20. Consider the string HELLO GOOD MORNING.


The following statements are used to read this string into the character
array text. Identify their difference. a. cin >> text; b. cin.getline(text, 50); int main( )
21. Consider the following C++ code,What will be the output if we input the string {
“ Vande Mataram” . Justify your answer. How will you correct it? char str[20 ];
cin str;
22. What is the advantage of using gets ( ) function in C++ program to input cout str;
string data. Explain with an example. }
23. Write the declaration statement for a variable 'name' in C++ to store a string of
maximum length 30. (b) Differentiate statements cin>>name; and gets(name); for
reading data to variable 'name'.
24. my_name is a variable contains a string. Write two different C++ statements to display the string
Note:

Type of data Console function Stream function operation


Character function getchar( ) cin.get( ) Character input
putchar( ) cout.put( ) Character output
String function gets() cin.getline( ), cin.get( ) String input
puts( ) cout.write( ) String output
Header file cstdio iostream

You might also like