Official Kerala Board Solutions for Class 11 Computer Science: Chapter 09 String Handling and I/O Functions
Explore reliable textbook solutions for Chapter 09 String Handling and I/O Functions tailored for Class 11 learners. Utilizing these Computer Science answers ensures thorough preparation and strengthens foundational knowledge before final Kerala Board evaluations.
Chapter-wise Solutions for Computer Science: Chapter 09 String Handling and I/O Functions
Navigate directly to the solved Computer Science textbook exercises using the digital viewer below. Each solution includes detailed step-by-step explanations, allowing students to instantly cross-check their work and identify areas requiring further revision.
One Mark Questions and Answers
Question 1: To read a single character for gender, i.e. 'm' or 'f', ______ function is used.
(a) getch()
(b) getchar()
(c) gets()
(d) getline()
Answer:
(b) getchar()
Exam Tips:
a) getchar() reads one character; gets() and getline() read a whole string.
Question 2: To use getchar(), putchar(), gets() and puts(), which header file is used?
(a) iostream
(b) cstdio
(c) input
(d) output
Answer:
(b) cstdio
Exam Tips:
a) Console functions (getchar, putchar, gets, puts) need cstdio; stream objects (cin, cout) need iostream.
Question 3: To use cin and cout, which header file is needed?
(a) iostream
(b) cstdio
(c) input
(d) output
Answer:
(a) iostream
Exam Tips:
a) Remember: cin/cout → iostream, gets/puts → cstdio, strlen/strcpy → cstring.
Question 4: Predict the output of the following code snippet.
#include<cstdio>
int main()
{
char name[] = "ADELINE";
for(int i=0; name[i]!='\0'; i++)
putchar(name[i]);
}
Answer:
The output is ADELINE
Exam Tips:
a) The loop prints one character at a time until the null character '\0', so the whole word is printed on one line without spaces.
Question 5: Which of the following is equivalent to the function getc(stdin)?
(a) putchar()
(b) gets()
(c) getchar()
(d) puts()
Answer:
(c) getchar()
Exam Tips:
a) getc(stdin) reads one character from the standard input (keyboard), which is exactly what getchar() does.
Question 6: Which of the following is equivalent to the function putc(ch, stdout)?
(a) putchar(ch)
(b) ch = gets()
(c) ch = getchar()
(d) puts(ch)
Answer:
(a) putchar(ch)
Exam Tips:
a) putc(ch, stdout) writes one character to the standard output (screen), the same as putchar(ch).
Question 7: To print a single character at a time, which function is used?
(a) puts()
(b) putchar()
(c) gets()
(d) getchar()
Answer:
(b) putchar()
Exam Tips:
a) "put" functions are for output and "get" functions are for input; "char" means one character.
Question 8: To read a string, ______ function is used.
(a) puts()
(b) putchar()
(c) gets()
(d) getchar()
Answer:
(c) gets()
Exam Tips:
a) Check that the option letter matches the function name you write; a wrong letter can cost the mark.
Question 9: To print a string, ______ function is used.
(a) puts()
(b) putchar()
(c) gets()
(d) getchar()
Answer:
(a) puts()
Exam Tips:
a) puts() prints a string and moves the cursor to the next line automatically.
Question 10: Consider the following code snippet.
main()
{
char str[80];
gets(str);
for(int i=0, len=0; str[i]!='\0'; i++, len++);
cout<<"The length of the string is "<<len;
}
Select the equivalent of the for statement (underlined) from the following.
(a) int len = strlen(str)
(b) int len = strcmp(str)
(c) int len = strcount(str)
(d) None of these
Answer:
(a) int len = strlen(str)
Exam Tips:
a) strlen() returns the number of characters in a string, not counting '\0'; it needs the header file cstring.
Question 11: Arjun wants to read a string with spaces. Which of the following is suitable?
(a) cin>>
(b) cin.getline(str,80)
(c) str = getc(stdin)
(d) None of these
Answer:
(b) cin.getline(str,80)
Exam Tips:
a) cin>> stops at a space, so use cin.getline() or gets() to read strings with spaces.
Question 12: State whether the following statement is true or false: The '>>' extraction operator stops reading a string when it encounters a space.
Answer:
True
Exam Tips:
a) >> (extraction) is used with cin for input; << (insertion) is used with cout for output. Do not mix them up.
Question 13: ______ function is used to copy a string to another variable.
Answer:
strcpy()
Exam Tips:
a) strcpy(destination, source) copies the source string into the destination; it needs the header file cstring.
Question 14:
1. Write the declaration statement for a variable 'name' in C++ to store a string of maximum length 30.
2. Differentiate between the statements cin>>name and gets(name) for reading data into the variable 'name'.
Answer:
1. char name[31];
(One extra location is needed for the null character '\0'.)
2. cin>>name does not accept spaces. It takes characters only up to the first space; the characters after the space are cut off. Here, space is the delimiter.
Example:
char name[20];
cout<<"Enter your name:";
cin>>name;
cout<<"Hello "<<name;
If the input is "Alvis Emerin", the output is: Hello Alvis
gets(name) reads a string from the keyboard including spaces, up to the Enter key. It needs the header file cstdio.
Example:
char name[20];
cout<<"Enter your name:";
gets(name);
cout<<"Hello "<<name;
If the input is "Alvis Emerin", the output is: Hello Alvis Emerin
Exam Tips:
a) For a string of length 30, declare size 31; forgetting the space for '\0' is a common mistake.
b) In part 2, use the same input for both examples so the difference in output is clear.
Question 15: What is the advantage of using the gets() function in a C++ program to input string data? Explain with an example.
Answer:
The gets() function reads a string from the keyboard including spaces (up to the Enter key), whereas cin>> stops at the first space.
Example:
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
char name[20];
cout<<"Enter your name:";
gets(name);
cout<<"Hello "<<name;
}
If the input is "Alvis Emerin", the output is: Hello Alvis Emerin
Exam Tips:
a) State the advantage first (it accepts spaces), then give the example.
b) Include the header file cstdio in your program when using gets().
Two Mark Questions and Answers
Question 16: In a C++ program, you forgot to include the header file iostream. What errors may occur in the program? Explain.
Answer:
The program will give compilation errors wherever cin and cout are used. The compiler reports that cin and cout are not declared (undeclared identifier / prototype error), because these objects are declared in the header file iostream. So, to use cin and cout, including iostream is a must.
Exam Tips:
a) Say that it is a compile-time error, not a run-time error.
b) Mention that cin and cout are defined in iostream.
Question 17: Categorise the following into three groups according to their relationship.
iostream, cstdio, gets(), puts(), getchar(), putchar(), getline(), write(), cin, cout
Answer:
| Header file | Input function | Output function |
|---|---|---|
| iostream | cin | cout |
| cstdio | gets() | puts() |
| getchar() | putchar() | |
| getline() | write() |
Exam Tips:
a) Use clear group names: header files, input functions and output functions.
b) Pair each input function with its matching output function (gets–puts, getchar–putchar, getline–write).
Question 18: Pick the odd one out from the following and give the reason.
gets(), getline(), getch(), getchar()
Answer:
getline(). It is a stream function (used as cin.getline()), whereas the others are console functions.
Exam Tips:
a) The reason carries half the marks; always write it.
Question 19: my_name is a variable that contains a string. Write two different C++ statements to display the string.
Answer:
1. cout<<my_name;
2. puts(my_name);
Exam Tips:
a) cout.write(my_name, strlen(my_name)); is also a valid answer.
b) End each C++ statement with a semicolon.
Question 20: Suggest the most suitable built-in function in C++ to perform the following tasks:
1. To find the answer for \(5^3\)
2. To find the number of characters in the string "KERALA"
3. To get back the number 10 if the argument is 100
Answer:
1. pow(5,3)
2. strlen("KERALA")
3. sqrt(100)
Exam Tips:
a) pow() and sqrt() need the header file cmath; strlen() needs cstring.
b) strlen("KERALA") returns 6; the null character is not counted.
Question 21: Read the following C++ statements:
char str[50];
cin>>str;
cout<<str;
During execution, if the input is "GREEN COMPUTING", the output will be only the word "GREEN". Give the reason for this. What modification is needed to get the original string as output?
Answer:
It displays only "GREEN" because cin>> takes characters only up to the space; space is the delimiter for cin. The part of the string after the space is cut off.
To solve this, use the gets() function, which reads characters up to the Enter key:
gets(str);
puts(str);
Now the output is "GREEN COMPUTING".
Exam Tips:
a) Give both the reason (space is the delimiter) and the modification.
b) cin.getline(str,50); is also an accepted modification.
Question 22: Suppose M[5][5] is a 2D array that contains the elements of a square matrix. Write C++ statements to find the sum of the diagonal elements.
Answer:
int sum = 0;
for(int i=0; i<5; i++)
sum = sum + M[i][i];
cout<<"Sum of diagonal elements = "<<sum;
(For the other diagonal, add M[i][4-i] in the same loop.)
Exam Tips:
a) Diagonal elements have equal row and column index, so one loop with M[i][i] is enough.
b) Initialise sum to 0 before the loop; forgetting this gives a wrong result.
Three Mark Questions and Answers
Question 23: Suresh wants to print his name and native place using a C++ program. The program should accept the name and native place first and print them as:
Name is: Suresh Kumar
Address is: Alappuzha
Answer:
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
char name[20], place[20];
cout<<"Enter your name: ";
cin.getline(name,20);
cout<<"Enter your place: ";
cin.getline(place,20);
cout<<"Name is: ";
puts(name);
cout<<"Address is: ";
puts(place);
}
Exam Tips:
a) Use cin.getline() or gets() because "Suresh Kumar" has a space.
b) The size given in getline() should not be more than the array size.
c) Include both iostream and cstdio since cout and puts() are used.
Question 24: "Programming is Fun". Write a C++ program to read a string like this in lower case and print it in UPPER CASE, without using the toupper() library function.
Answer:
#include<cstdio>
using namespace std;
int main()
{
char line[80];
int i;
puts("Enter the string to convert");
gets(line);
for(i=0; line[i]!='\0'; i++)
if(line[i]>=97 && line[i]<=122)
line[i] = line[i] - 32;
puts(line);
}
Exam Tips:
a) ASCII codes of 'a' to 'z' are 97 to 122; subtracting 32 gives the uppercase letter.
b) Check the range first so that spaces and other characters are not changed.
c) Write the header file before using namespace std;.
Question 25: As an assignment, Kumar has written a C++ program which reads a line of text and prints the number of vowels in it. What will be his program code?
Answer:
#include<cstdio>
#include<cctype>
#include<iostream>
using namespace std;
int main()
{
char line[80];
int i, vowel=0;
puts("Enter a string");
gets(line);
for(i=0; line[i]!='\0'; i++)
switch(tolower(line[i]))
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
vowel++;
}
cout<<"The number of vowels is "<<vowel;
}
Exam Tips:
a) Using tolower() lets you check only lowercase vowels and still count uppercase ones.
b) Cases without break fall through to vowel++, which is why one increment is enough.
c) tolower() needs the header file cctype.
Question 26: What will be the output of the following code if the user enters "GOOD MORNING"?
1. char string[80];
gets(string);
cout<<string;
2. char string[80];
cin>>string;
cout<<string;
3. char ch;
ch = getchar();
cout<<ch;
4. char string[80];
cin.getline(string,9);
cout<<string;
Answer:
1. GOOD MORNING
2. GOOD
3. G
4. GOOD MOR
Exam Tips:
a) cin.getline(str,n) reads at most n-1 characters; the space also counts as a character.
b) getchar() reads only the first character.
c) cin>> stops at the space, while gets() reads the full line.
Question 27: Consider the following code snippet.
int main()
{
int n;
cout<<"Enter a number";
cin>>n;
cout<<"The number is "<<n;
}
Write down the names of the header files that must be included in this program.
Answer:
Only cin and cout are used here, so the header file iostream must be included:
#include<iostream>
using namespace std;
Exam Tips:
a) Give the reason (cin and cout are used) along with the header file name.
b) Do not add unnecessary header files like cstdio here.
Question 28: Write a program to display the following output.
A
BB
CCC
Answer:
#include<iostream>
using namespace std;
int main()
{
char str[] = "ABC";
int i, j;
for(i=0; i<3; i++)
{
for(j=0; j<=i; j++)
cout<<str[i];
cout<<endl;
}
}
Exam Tips:
a) The outer loop selects the row (and the letter); the inner loop prints that letter i+1 times.
b) Use cout<<endl; after the inner loop to move to the next line.
c) Trace the loops for i = 0, 1, 2 to check the output.
Question 29: Distinguish between getchar and gets.
Answer:
getchar() is a character function: it reads a single character from the keyboard. The header file cstdio must be included.
Example:
char ch;
ch = getchar();
cout<<ch;
gets() is a string function: it reads a string from the keyboard, including spaces, up to the Enter key. The header file cstdio must be included.
Example:
char str[80];
cout<<"Enter a string";
gets(str);
Exam Tips:
a) The main difference is one character (getchar) versus a whole string (gets).
b) Give a small example for each.
Question 30: Distinguish between putch and puts.
Answer:
putch() is a character function: it prints a single character on the monitor. It is declared in the header file conio.h.
Example:
char ch;
ch = getc(stdin);
putch(ch);
puts() is a string function: it prints a string on the monitor and moves to the next line. The header file cstdio must be included.
Example:
char str[80];
puts("Enter a string");
gets(str);
puts(str);
Exam Tips:
a) One prints a single character, the other prints a whole string; state this first.
b) The standard character output function in cstdio is putchar(); putch() is from conio.h.
Question 31: Write a program to check whether a string is a palindrome or not. (A string is a palindrome if it is the same as the string formed by reversing its characters, e.g. "MALAYALAM", "MADAM", "ARORA", "DAD".)
Answer:
#include<iostream>
using namespace std;
int main()
{
char str[40];
int len, i, j;
cout<<"Enter a string:";
cin>>str;
for(len=0; str[len]!='\0'; len++);
for(i=0, j=len-1; i<len/2; i++, j--)
if(str[i]!=str[j])
break;
if(i==len/2)
cout<<str<<" is palindrome";
else
cout<<str<<" is not palindrome";
}
Exam Tips:
a) Compare the first and last characters, moving inwards; only len/2 comparisons are needed.
b) If the loop finishes without break, i equals len/2 and the string is a palindrome.
c) Trace with "MADAM" to check your logic.
Question 32: Explain multi-character functions.
Answer:
getline() and write() are multi-character (stream) functions.
1. getline(): It reads a line of text that ends with a newline character. It reads white spaces also.
Example:
char line[80];
cin.getline(line,80);
2. write(): It is used to display a string. The second argument gives the number of characters to display.
Example:
char line[80];
cin.getline(line,80);
cout.write(line, strlen(line));
Exam Tips:
a) Name both functions and give the syntax: cin.getline(string, size) and cout.write(string, length).
b) Mention that getline() accepts spaces.
Question 33: Read a string and print the number of vowels.
Answer:
#include<cstdio>
#include<cctype>
#include<iostream>
using namespace std;
int main()
{
char line[80];
int i, vowel=0;
puts("Enter a string");
gets(line);
for(i=0; line[i]!='\0'; i++)
switch(tolower(line[i]))
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
vowel++;
}
cout<<"The number of vowels in the string is "<<vowel;
}
Exam Tips:
a) Show the prompt before reading input.
b) Each case label ends with a colon, not a semicolon; this is a common syntax error.
Question 34: Distinguish between the get() and put() functions.
Answer:
get() function:
get() is an input function. It reads a single character and does not ignore white spaces and the newline character.
Syntax: cin.get(variable);
Example:
char ch;
cin.get(ch);
put() function:
put() is an output function. It prints a single character.
Syntax: cout.put(variable);
Example:
char ch;
cin.get(ch);
cout.put(ch);
Exam Tips:
a) get() is used with cin and put() with cout; write the syntax for both.
b) Mention that get(), unlike cin>>, also reads spaces.
Question 35: Write a program to read a string and print the number of consonants.
Answer:
#include<iostream>
#include<cstdio>
#include<cctype>
using namespace std;
int main()
{
char str[40], ch;
int consonant=0, i;
cout<<"Enter a string:";
gets(str);
for(i=0; str[i]!='\0'; i++)
{
ch = toupper(str[i]);
if(ch>='B' && ch<='Z')
if(ch!='E' && ch!='I' && ch!='O' && ch!='U')
consonant++;
}
cout<<"The number of consonants is "<<consonant;
}
Exam Tips:
a) The range 'B' to 'Z' already leaves out 'A'; then exclude E, I, O and U.
b) Converting to uppercase first means you need to check only one case.
c) Be careful not to type the digit 0 for the letter O.
Question 36: Write a program to read a string and print the number of spaces.
Answer:
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
char str[40];
int space=0, i;
cout<<"Enter a string:";
gets(str);
for(i=0; str[i]!='\0'; i++)
if(str[i]==32)
space++;
cout<<"The number of spaces is "<<space;
}
Exam Tips:
a) 32 is the ASCII code of a space; you can also write str[i]==' '.
b) Use gets() and not cin>>, otherwise no spaces will be read.
Question 37: Describe in detail the unformatted console I/O functions.
Answer:
1. Single character functions: These functions read or print one character at a time.
(i) getchar(): It reads a character from the keyboard and stores it in a character variable.
Example:
char ch;
ch = getchar();
(ii) putchar(): It prints a character on the screen.
Example:
char ch;
ch = getchar();
putchar(ch);
2. String functions: These functions read or print a string.
(i) gets(): It reads a string from the keyboard and stores it in a character array.
Example:
char str[80];
gets(str);
(ii) puts(): It displays a string on the screen.
Example:
char str[80];
gets(str);
puts(str);
Exam Tips:
a) Divide the answer into two groups: single character functions and string functions.
b) Give one example for each function.
c) Mention that all four need the header file cstdio.
Question 38: Write a program to input a string and find the number of uppercase letters, lowercase letters, digits, special characters and white spaces.
Answer:
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
char str[100];
int i, digit=0, Ualpha=0, Lalpha=0, special=0, wspace=0;
cout<<"Enter a string:";
gets(str);
for(i=0; str[i]!='\0'; i++)
if(str[i]>=48 && str[i]<=57)
digit++;
else if(str[i]>=65 && str[i]<=90)
Ualpha++;
else if(str[i]>=97 && str[i]<=122)
Lalpha++;
else if(str[i]==' ' || str[i]=='\t')
wspace++;
else
special++;
cout<<"Uppercase letters: "<<Ualpha<<endl;
cout<<"Lowercase letters: "<<Lalpha<<endl;
cout<<"Digits: "<<digit<<endl;
cout<<"Special characters: "<<special<<endl;
cout<<"White spaces: "<<wspace;
}
Exam Tips:
a) Remember the ASCII ranges: digits 48–57, uppercase 65–90, lowercase 97–122.
b) Use an else-if ladder so each character is counted only once.
c) Print each count on a separate line to keep the output clear.
Question 39: Write a program to count the number of words in a sentence.
Answer:
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
int i, words=1;
char str[80];
cout<<"Enter a string\n";
gets(str);
for(i=0; str[i]!='\0'; i++)
if(str[i]==32)
words++;
cout<<"The number of words is "<<words;
}
Exam Tips:
a) Number of words = number of spaces + 1 (when words are separated by single spaces), so start the counter at 1.
b) Use gets() to read the full sentence with spaces.
Question 40: Write a program to input a string and replace all lowercase vowels by the corresponding uppercase letters.
Answer:
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
char str[100];
int i;
cout<<"Enter a string:";
gets(str);
for(i=0; str[i]!='\0'; i++)
switch(str[i])
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
str[i] = str[i] - 32;
}
cout<<str;
}
Exam Tips:
a) Subtracting 32 from a lowercase letter's ASCII code gives its uppercase letter.
b) Grouping the cases together avoids repeating the same statement five times.
Question 41: Write a program to input a string and display its reverse using console I/O functions only. For example, if the input is "AND", the output should be "DNA".
Answer:
#include<iostream>
using namespace std;
int main()
{
char str[40], rev[40];
int len, i, j;
cout<<"Enter a string:";
cin>>str;
for(len=0; str[len]!='\0'; len++);
for(i=0, j=len-1; i<len; i++, j--)
rev[i] = str[j];
rev[i] = '\0';
cout<<"The reversed string is "<<rev;
}
Exam Tips:
a) Do not forget rev[i] = '\0'; at the end, or garbage characters will be printed.
b) Find the length first, then copy characters from the last to the first.
Question 42: Write a program to input a word (say COMPUTER) and create a triangle as follows.
C
CO
COM
COMP
COMPU
COMPUT
COMPUTE
COMPUTER
Answer:
#include<iostream>
#include<cstring> //for strlen()
using namespace std;
int main()
{
char str[20];
int i, j;
cout<<"Enter a word (e.g. COMPUTER):";
cin>>str;
for(i=0; i<strlen(str); i++)
{
for(j=0; j<=i; j++)
cout<<str[j];
cout<<endl;
}
}
Exam Tips:
a) The inner loop prints str[j] (from the start), not str[i]; this gives C, CO, COM and so on.
b) Include cstring for strlen().
Question 43: Write a program to input a line of text and display the first character of each word. Use only console I/O functions. For example, if the input is "Save Water, Save Nature", the output should be "SWSN".
Answer:
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
int i;
char str[80];
cout<<"Enter a string\n";
gets(str);
if(str[0]!=32)
cout<<str[0];
for(i=0; str[i]!='\0'; i++)
if(str[i]==32 && str[i+1]!=32)
cout<<str[i+1];
}
Exam Tips:
a) The first word has no space before it, so print str[0] separately.
b) The condition str[i+1]!=32 avoids printing a space when there are two spaces together.
c) Trace your program with the given example to check the output "SWSN".
Free study material for Computer Science
Computer Science Class 11 Curriculum Solutions: Chapter 09 String Handling and I/O Functions
Textbook Solutions for Class 11 Computer Science Chapter 09 String Handling and I/O Functions
Review comprehensive exercise answers for Class 11 Computer Science Chapter 09 String Handling and I/O Functions. Fully updated to match current Kerala Board syllabus guidelines, these textbook solutions help students verify their work and maintain accurate study notes.
Mastering Theoretical and Practical Questions
Beyond providing final answers, these guides offer step-by-step breakdowns for complex queries in the Class 11 Computer Science module. This approach helps students balance theoretical depth with practical problem-solving skills required for Kerala Board exams.
Effective Self-Study and Homework Assistance
Frequent review of these structured answers builds strong analytical capabilities and response efficiency. Maximize your academic readiness by combining these textbook solutions with our curated study materials and mock evaluations for Class 11 Computer Science.
FAQs
The complete and updated Kerala Plus One Computer Science Chapter 9 String Handling and I/O Functions Questions and Answers is available for free on StudiesToday.com. These solutions for Class 11 Computer Science are as per latest Kerala Board curriculum.
Yes, our experts have revised the Kerala Plus One Computer Science Chapter 9 String Handling and I/O Functions Questions and Answers as per 2026 exam pattern. All textbook exercises have been solved and have added explanation about how the Computer Science concepts are applied in case-study and assertion-reasoning questions.
Toppers recommend using Kerala Board language because Kerala Board marking schemes are strictly based on textbook definitions. Our Kerala Plus One Computer Science Chapter 9 String Handling and I/O Functions Questions and Answers will help students to get full marks in the theory paper.
Yes, we provide bilingual support for Class 11 Computer Science. You can access Kerala Plus One Computer Science Chapter 9 String Handling and I/O Functions Questions and Answers in both English and Hindi medium.
Yes, you can download the entire Kerala Plus One Computer Science Chapter 9 String Handling and I/O Functions Questions and Answers in printable PDF format for offline study on any device.