Plus One Computer Science Chapter Wise Questions and Answers Chapter 9 String Handling and I/O Functions

Kerala Plus One Computer Science Chapter Wise Questions and Answers Chapter 9 String Handling and I/O Functions

Plus One String Handling and I/O Functions One Mark Questions and Answers

Question 1.
To read a single character for gender i.e. ‘m’ or ‘f’ ________ function is used.
Answer:
(a) getch()
(b) getchar()
(c) gets()
(d) getline()
Answer:
(b) getchar()

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

Question 3.
To use cin and cout, which header file is needed?
(a) iostream
(b) cstdio
(c) input
(d) output
Answer:
(a) iostream

Question 4.
Predict the output of the following code snippet.
#include<cstdio>
int mainO
{
char name[ ] = “ADELINE”;
for(int i=0; name[i]!=’\0′;i++)
putchar(name[i]);
}
Answer:
The output is “ADELINE”.

Question 5.
From the following which is equivalent to the function getc(stdin).
(a) putchar()
(b) gets()
(c) getchar()
(d) puts()
Answer:
(c) getchar()

Question 6.
From the following which is equivalent to the function putc(ch, stdout).
(a) putchar(ch)
(b) ch = gets()
(c) ch = getchar()
(d) puts(ch )
Answer:
(a) 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()

Question 8.
To read a string _______ function is used.
(a) puts()
(b) putchar()
(c) gets()
(d) getchar()
Answer:
(b) gets()

Question 9.
To print a string _______ function is used.
(a) puts()
(b) putchar()
(c) gets()
(d) getchar()
Answer:
(b) puts()

Question 10.
Consider the following code snippet.
main()
{
char str[80];
gets(str);
for(int i=0. len=0;str[il!=’\0′;i++.len++);
cout<<“The length of the string is ” <<len;
}
Select the equivalent forthe under lined statement 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)

Question 11.
Arjun wants to read a string with spaces from the following which is suitable.
(а) cin>>
(b) cin.getline(str,80)
(c) str = getc(stdin)
(d) none of these
Answer:
(b) cin.getline(str,80)

Question 12.
State whether the following statement is true or false. The ‘<<‘ insertion operator stops reading a string when it encounters a space.
Answer:
True

Question 13.
_________ function is used to copy a string to another variable. (SAY-2016) (1)
Answer:
strcpy();

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 statement cin>>name and gets (name) for reading data to the variable ‘name’. (SAY-2016)

Answer:
1. char name[31];(One for null(\0) character).

OR

cin>> does not allows space. It will take characters up to the space and characters after space will be truncated . Here space is the delimiter. Consider the following code snippet that will take the input upto the space.
#include<iostream>
using namespace std;
int main()
{
char name[20];
cout<<“Enter your name:”;
cin>>name;
cout<<“Hello “<<name;
}
If you input a name “Alvis Emerin” then the output will be Hello Alvis. The string after space is truncated.

2. gets(): This function is used to get a string from the keyboard including spaces. Considerthe 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;
}
If you input a name “Alvis Emerin” then the output will be Hello Alvis Emerin.

Question 15.
What is the advantage of using gets() function in the C++ program to input string data? Explain with an example.
Answer:
gets() function is used to get a string from the keyboard including spaces. 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;
}
If you input a name “Alvis” then the output is Hello Alvis.

Plus One String Handling and I/O Functions Two Mark Questions and Answers

Question 1.
In a C++ program, you forgot to include the header file iostream. What are the possible errors occur in that Program? Explain?
Answer:
Prototype error. To use cin and cout the header file iostream is a must.

Question 2.
Categorise the following into three according to their relationship
iostream, cstdio, gets(), puts(), getchar(), putchar(), getline(), write(), cin, cout.
Answer:
Plus One Computer Science Chapter Wise Questions and Answers Chapter 9 String Handling and I/O Functions 2M Q2

Question 3.
Pick the odd one out from the following and give reason.
gets(), getline(), getch() getchar().
Answer:
getline() – It is a stream function whereas the others are console functions.

Question 4.
My_name is a variable contains a string. Write two different C++ statements to display the string. (SAY-2016) (2)
Answer:

  1. cout<<my_name;
  2. puts(my_name);

Question 5.
Suggest most suitable built-in function in C++ to perform the following tasks: (MARCH-2016) (2)

  1. To find the answer for 53
  2. To find the number of characters in the string “KERALA” “HAPPY NEW YEAR”
  3. To get back the number 10 if the argument is 100.

Answer:

  1. pow(5,3);
  2. strlen(“KERALA”)
  3. tolower(‘M’)
  4. sqrt(100);

Question 6.
Read the following C++ statements:
charstr[50];
cin>>str;
cout<<str;
During execution, if the string given as input is “GREEN COMPUTING”, the output will be only the word “GREEN”. Give reason for this. What modification is required to get the original string as output? (SCERT SAMPLE -1) (2)
Answer:
cin>>word;
cout<<word;
It displays “HAPPY” because cin takes characters upto the space. That is space is the delimiter for cin. The string after space is truncated. To resolve this use gets() function.

Because gets() function reads character upto the enter key.
Hence gets(word);
puts(word);
Displays “HAPPY NEW YEAR”

Question 7.
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. (2)
Answer:
gets() function is used to get a string from the keyboard including spaces. To use gets () function the header file cstdio must be included. It reads the characters upto the enter key pressed by the user.
eg:
char name[20];
cout << “Enter your name”;
gets(name);
cout<< “Hello”<< name;
When the user gives Alvis Emerin. It displays as “Hello Alvis Emerin”.

Plus One String Handling and I/O Functions Three Mark Questions and Answers

Question 1.
Suresh wants to print his name and native place using a C++ program. The program should accept name and native place first.
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,80);
cout<<“Enter your place”;
cin.getline(place,80);
cout<<“Your name is puts(name);
cout<<“Your place is puts(place);
}

Question 2.
“Programming is Fun”. Write a C++ program to read a string like this in lower case and print it in UPPER CASE. With out using toupper() library function.
Answer:
using namespace std;
#include<cstdio>
int main()
{
char line[80];
int i;
puts(Enter the string to convert”);
gets(line);
for(i=0;line[i]!=’\0′;i++)
if (Iine[i]>=97 && line[i]<=122)
line[i]=line[i] – 32;
puts(line);
}

Question 3.
An assignment Kumar has written a C++ program which reads a line of text and print 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;

Question 4.
What will be the output of the following code if the user enter the value “GOOD MORNING”.
1. char string [80];
gets(string);
cout<<string;

2. char string [80];
cin>>string;
cout<<string;

3. charch;
ch = getchar();
cout<<ch;

4. char string [80];
cin.getline(string,9);
cout<<string;
Answer:

  1. GOOD MORNING
  2. GOOD
  3. G
  4. GOOD MORN

Question 5.
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:
Here cin and cout are used so the header file iostream must be included.

Question 6.
Write a program to display the following output.
A
BB
CCC
#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;
}
}

Question 7.
Distinguish getchar and gets.
Answer:
getchar is a character function but gets is a string function. The header file cstdio.h must be included. It reads a character from the keyboard.
Eg.
char ch;
ch = getchar();
cout<<ch;
gets is used to read a string from the keyboard. It reads the characters upto enter key. The header file cstdio must be included.
char str[80J;
cout<<“Enter a string”;
gets(str);

Question 8.
Distinguish putch and puts.
Answer:
putch is a character function but puts is a string function. The header file cstdio must be included. It prints a character to the monitor.
Eg:
char ch;
ch = getc(stdin);
putch(ch);
puts is used to print a string. The header file stdio.h must be included.
charstr[80];
puts(“Entera string”);
gets(str);
puts(str);

Question 9.
Write a program to check whether a string is palindrome or not. (A string is said to be palindrome if it is the same as the string constituted by reversing the characters of the original string. eg: “MALAYALAM”, “MADAM”, “ARORA”, “DAD”, etc.)
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”;
}

Question 10.
Explain multi-character function.
Answer:
getline() and write() functions are multi character functions:
1. getline() It reads a line of text that ends with a newline character. It reads white spaces also.
eg:
char line[80];
cin.getline(line,80);

2. write() It is used to display a string.
Eg.
char line[80];
cin.getline(line,80);
cout.write(line,80);

Question 11.
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 is in the string is “<< vowel;
}

Question 12.
Distinguish between get() and put() functions.
Answer:
get() function:
get() is an input function. It is used to read a single character and it does not ignore the white spaces and newline character.
Syntax is cin.get(variable);
eg: char ch;
cin.get(ch);

put() function:
put() is an output function. It is used to print a character.
Syntax is cout.put(variable);
eg:
charch;
cin.get(ch);
cout.put(ch);

Question 13.
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 consonent = 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!=’0’&& ch!=’U’)
consonent++;
}
cout<<“The number of consonants is “<<consonent;
}

Question 14.
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;
}

Question 15.
Describe in detail the unformatted console I/O functions.
Answer:
1. Single character functions: This function is used to read or print a character at a time,
(i) getchar():
It reads a character from the keyboard and store it in a character variable.
eg:
char ch;
ch=getchar();

(ii) putchar():
This function is used to print a character on the screen.
eg:
char ch;
ch = getchar();
putchar(ch);

2. String functions This function is used to read or print a string.
(i) gets():
This function is used to read a string from the keyboard and store it in a character variable.
eg:
charstr[80];
gets(str);

(ii) puts():
This function is used to display a string on the screen.
eg:
char str[80];
gets(str);
puts(str);

Question 16.
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<<“The number of alphabets is “<<Ualpha+Lalpha<<
” the number of Uppercase letters is “<<Ualpha<< ” the number of Lowercase letters is “<<Lalpha<<” the number of digits is “<<digit<<” the special characters is “<<special<<” and the number of white spaces is “<<wspace;
}

Question 17.
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;
}

Question 18.
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++)
if(str[i]>=65 && str[i]<=90 || str[i]> = 97 && str[i]<=122)
switch(str[i])
{
case ‘a’:
str[i] = str[i]-32;
break;
case ‘e’:
str[i] = str[i]-32;
break;
case ‘i’:
str[i] = str[i]-32;
break;
case ‘o’: .
str[i] = str[i]-32;
break;
case ‘u’:
str[i] = str[i]-32;
}
cout<<str;
}

Question 19.
Write a program to input a string and display its reversed string using console I/O functions only. For example, if the input is “AND” the output should “DNA”.
Answer:
#include<iostream>
using namespace std;
int main()
{
char str[40],rev[40];
int len.ij;
cout<<“Enter a string:”;
cin>>str;
for(len=0;str[len]!=’\0′;len++);
for(i=0,j=len-1 ;i<len;i++,j–)
rev[il=str[j];
rev[i]=’\0′;
cout<<“The reversed string is “<<rev;
}

Question 20.
Write a program to input a word(say COMPUTER) and create a triangle as follows.
C
C O
C O M
C O M P
C O M P U
C O M P U T
C O M P U T E
C O M P U T E R
Answer:
#include<iostream>
#include<cstring>//for strlen()
using namespace std;
int main()
{
charstr[20];
cout<<“enter a word(eg.COMPUTER):”;
cin>>str;
int ij;
for(i=0;i<strlen(str);i++)
{
for(j=0;j<=i;j++)
cout<<str[j]<<“\t”;
cout<<endl;
}
}

Question 21.
Write a program to input a line of text and display the first characters 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;
charstr[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];
}

Plus One Computer Science Chapter Wise Questions and Answers