All Exams  >   Software Development  >   Basics of C++  >   All Questions

All questions of Strings for Software Development Exam

What is the purpose of the 'getline()' function in C++?
  • a)
    It reads a line of text from the console.
  • b)
    It converts a string to lowercase.
  • c)
    It concatenates two strings.
  • d)
    It searches for a substring within a string.
Correct answer is option 'A'. Can you explain this answer?

Omar Al Haddad answered
Answer:

The getline() function in C is used to read a line of text from the console. It is a standard library function defined in the header file . The purpose of this function is to read a line of text, including spaces, from the user and store it in a character array or a string.

Working of getline() function:
1. The getline() function takes three arguments: the address of the character array where the input will be stored, the maximum number of characters to be read, and the file pointer from where the input is to be read.
2. It reads characters from the specified file pointer until it encounters a newline character or the maximum number of characters is read.
3. The newline character ('\n') is also read and stored in the character array.
4. The getline() function then appends a null character ('\0') at the end of the character array to mark the end of the string.

Example usage:
```c
#include

int main() {
char str[100]; // declare a character array to store the input
printf("Enter a line of text: ");
fflush(stdout); // flush the output buffer to display the prompt
fgets(str, sizeof(str), stdin); // use fgets() to read the line of text

printf("You entered: %s", str); // display the input

return 0;
}
```

Explanation:
In the above example, the getline() function is used to read a line of text from the user.
- The character array `str` is declared to store the input.
- The prompt "Enter a line of text: " is displayed using the printf() function.
- The fflush() function is used to flush the output buffer to ensure that the prompt is displayed before reading the input.
- The fgets() function is used to read the line of text from the user. The maximum number of characters to be read is specified as `sizeof(str)`, which ensures that the input does not exceed the size of the character array.
- Finally, the input is displayed using the printf() function.

This is how the getline() function is used to read a line of text from the console in C. It is a convenient way to handle input that includes spaces and newline characters.
1 Crore+ students have signed up on EduRev. Have you? Download the App

What is the output of the following code snippet?
string s = "Hello World";
cout << s.length() << endl;
  • a)
    11
  • b)
    10
  • c)
    12
  • d)
    9
Correct answer is option 'A'. Can you explain this answer?

Reem Al Saadi answered
Explanation:

String Length:
- The variable `s` is assigned the string "Hello World".
- The `length()` function in C++ returns the number of characters in the string.
- In the string "Hello World", there are 11 characters including the space.
Therefore, the output of the code snippet will be 11.

What is the output of the following code snippet?
```cpp
string s = "Hello World";
int index = s.find("World");
cout << index << endl;
```
  • a)
    0
  • b)
    6
  • c)
    11
  • d)
    -1
Correct answer is option 'B'. Can you explain this answer?

Sonal Yadav answered
The output of the code snippet is 6 because 's.find("World")' returns the starting index of the first occurrence of "World" in the string, which is 6 in this case.

What is the output of the following code snippet?
string str = "Hello";
cout << str[2];
  • a)
    H
  • b)
    e
  • c)
    l
  • d)
    o
Correct answer is option 'C'. Can you explain this answer?

Sonal Yadav answered
The output will be 'l' because string indexing starts from 0, so str[2] refers to the third character in the string.

What will be the output of the following code?
```cpp
string s = "abc";
cout << s.find_first_of("def");
```
  • a)
    -1
  • b)
    0
  • c)
    1
  • d)
    2
Correct answer is option 'A'. Can you explain this answer?

Sonal Yadav answered
The output of the code is -1 because 's.find_first_of("def")' searches for the first occurrence of any character in "def" within the string and returns the index. Since none of those characters are found, it returns -1.

What will be the output of the following code?
```cpp
string s = "Hello World";
cout << s.replace(6, 5, "Everyone").length();
```
  • a)
    11
  • b)
    12
  • c)
    13
  • d)
    14
Correct answer is option 'C'. Can you explain this answer?

Sonal Yadav answered
The output of the code is 13 because 's.replace(6, 5, "Everyone").length()' replaces a substring starting at index 6 with "Everyone" and then returns the length of the resulting string.

Chapter doubts & questions for Strings - Basics of C++ 2024 is part of Software Development exam preparation. The chapters have been prepared according to the Software Development exam syllabus. The Chapter doubts & questions, notes, tests & MCQs are made for Software Development 2024 Exam. Find important definitions, questions, notes, meanings, examples, exercises, MCQs and online tests here.

Chapter doubts & questions of Strings - Basics of C++ in English & Hindi are available as part of Software Development exam. Download more important topics, notes, lectures and mock test series for Software Development Exam by signing up for free.

Basics of C++

70 videos|45 docs|15 tests

Top Courses Software Development

Signup to see your scores go up within 7 days!

Study with 1000+ FREE Docs, Videos & Tests
10M+ students study on EduRev