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

String Handling

Uploaded by

kuttukuttan126
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

String Handling

Uploaded by

kuttukuttan126
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

very_short_answers:

- question: "What will the output of the statement: putchar(97); be?"


answer: "a"
explanation: "ASCII value 97 corresponds to the character 'a'."

- question: "Distinguish between console functions and stream functions."


answer:
console_functions: "Directly interact with the console for input and output, e.g., getchar(), putchar()."
stream_functions: "Handle input/output using streams, allowing redirection, e.g., cin, cout."

- question: "Write a C++ statement to input the string 'Computer' using get() function."
answer: "cin.get(str, sizeof(str));"

- question: "Write down the output of the following code segment: puts('hello'); puts('friends');"
answer: |
hello
friends

short_answer_type:
- question: "Predict the output of the following code segment:
char str[] = 'Program';
for (int i=0; str[i] != '\\0'; ++i) { putchar(str[i]); putchar('–'); }"
answer: "P–r–o–g–r–a–m–"

- question: "Write a program to input a word (say COMPUTER) and create a triangle as follows:
C
CO
COM
COMP
COMPU
COMPUT
COMPUTE
C O M P U T E R"
code: |
#include <iostream>
using namespace std;
int main() {
string word;
cin >> word;
for (int i = 0; i < word.length(); ++i) {
for (int j = 0; j <= i; ++j) {
cout << word[j] << " ";
}
cout << endl;
}
return 0;
}

- question: "Write a program to input a line of text and display the first characters of each word. Use only console I/O functions."
code: |
#include <iostream>
using namespace std;
int main() {
char text[100];
cout << "Enter a line of text: ";
cin.getline(text, 100);
cout << text[0];
for (int i = 1; text[i] != '\0'; ++i) {
if (text[i - 1] == ' ' && text[i] != ' ') {
cout << text[i];
}
}
return 0;
}

- question: "Write a program to check whether a string is palindrome or not."


code: |
#include <iostream>
using namespace std;
int main() {
string str, rev;
cout << "Enter a string: ";
cin >> str;
rev = string(str.rbegin(), str.rend());
if (str == rev) {
cout << "Palindrome";
} else {
cout << "Not a palindrome";
}
return 0;
}

long_answer_type:
- question: "Explain the console I/O functions with examples."
answer: |
Console I/O functions interact directly with the console:
- `getchar()`: Reads a single character from the input.
- `putchar()`: Outputs a single character.
- `gets()`: Reads a string until a newline character.
- `puts()`: Outputs a string with a newline.
Example:
```cpp
#include <iostream>
using namespace std;
int main() {
char ch;
cout << "Enter a character: ";
ch = getchar();
putchar(ch);
return 0;
}
```

- question: "Briefly describe the stream I/O functions with examples."


answer: |
Stream I/O functions use streams to handle input and output:
- `cin`: For input.
- `cout`: For output.
- `getline()`: Reads a line of text.
- `write()`: Writes data with a specified length.
Example:
```cpp
#include <iostream>
using namespace std;
int main() {
string name;
cout << "Enter your name: ";
cin >> name;
cout << "Hello, " << name << "!";
return 0;
}
```

You might also like