PLUS ONE COMPUTER SCIENCE
First Year Computer Science Previous Questions Chapter wise ..
Chapter-9 String Handling and I/O Functions
#include<iostream>
using namespace std;
int main()
{
char str[20];
cin>>str;
cout<<str;
}
What will be the output if we input the string “Vande Mataram”. Justify your answer.
2. What is the advantage of using gets() function in C++ program to input string data?
Explain with an example.
The gets() function enables the user to enter some characters followed by the enter key. All the characters entered by the user get stored in a character array. The null character is added to the array to make it a string. The gets() allows the user to enter the space-separated strings.
3. a) Write the declaration statement for a variable ‘name’ in C++ to store a string of maximum length 30.
b) Differentiate the statements cin>>name; and gets(name); for reading data to the variable ‘name’.
Ans. a) char name[30];
b). Usually the input device is the keyboard. cin is the instance of the class istream and is used to read input from the standard input device which is usually keyboard.The extraction operator(>>) is used along with the object cin for reading inputs. The extraction operator extracts the data from the object cin which is entered using the keboard.
A string can be input using the statement cin>>name; This statement can store a string without any white space (that is, only one word). If we want store strings containing white spaces (strings having more than one word) we can use gets() function.That is gets(name);
4. Consider the following C++ statements:
char word[20];
cin>>word;
cout<<word;
gets(word);
puts(word);
If the string entered is “HAPPY NEW YEAR”, predict the output and justify your answer.
Ans. "HAPPY NEW YEAR" 17 characters including white spaces between the word,/0 is represent end of the string and 3 white spaces after the word .That is total 20 characters of the word.
5. a) my_name is a variable contains a string. Write two different C++ statements to display the string.
b) …… function is used to copy a string to another variable.
Ans.
b) strcpy( )
6. Read the following code:
char str[30];
cin >> str;
cout << str;
If we give the input “Green Computing”, we get the output “Green”. Why is it so? How can you correct that?
To change the