Q: How do I read a string input from the user in C++?
To read a string input from the user in C++, you can use the std::getline function from the <string> header.
Here's an example code snippet:
#include <iostream>
#include <string>
int main() {
std::string name;
std::cout << "Enter your name: ";
std::getline(std::cin, name);
std::cout << "Hello, " << name << "!" << std::endl;
return 0;
}
In this example, std::getline reads the entire line of input, including any spaces, and stores it in the name variable.
Q: How do I limit the length of the user input string?
If you want to limit the length of the user input string, you can manually check the length and take appropriate action.
Here's an example code snippet:
#include <iostream>
#include <string>
int main() {
std::string name;
std::cout << "Enter your name (up to 10 characters): ";
std::getline(std::cin, name);
if (name.length() > 10) {
std::cout << "Name exceeds the limit." << std::endl;
} else {
std::cout << "Hello, " << name << "!" << std::endl;
}
return 0;
}
In this example, the code checks if the length of the input string is greater than 10 characters and displays an error message if it is.
Q: How do I convert a string to an integer or a floating-point number?
To convert a string to an integer, you can use the std::stoi function, and to convert a string to a floating-point number, you can use the std::stof or std::stod functions.
Here's an example code snippet:
#include <iostream>
#include <string>
int main() {
std::string numberStr;
std::cout << "Enter a number: ";
std::getline(std::cin, numberStr);
int numberInt = std::stoi(numberStr);
float numberFloat = std::stof(numberStr);
std::cout << "Integer: " << numberInt << std::endl;
std::cout << "Float: " << numberFloat << std::endl;
return 0;
}
In this example, std::stoi converts the string to an integer, while std::stof converts the string to a floating-point number.
Important Interview Questions and Answers on C++ User Input Strings
Q: How can you read a string from the user in C++?
You can use the getline() function to read a string from the user.
Here's an example:
#include <iostream>
#include <string>
int main() {
std::string userInput;
std::cout << "Enter a string: ";
std::getline(std::cin, userInput);
std::cout << "You entered: " << userInput << std::endl;
return 0;
}
Q: How can you determine the length of a string entered by the user?
You can use the length() or size() member function of the string class to determine the length of a string.
Here's an example:
#include <iostream>
#include <string>
int main() {
std::string userInput;
std::cout << "Enter a string: ";
std::getline(std::cin, userInput);
std::cout << "Length of the string: " << userInput.length() << std::endl;
return 0;
}
Q: How can you check if a string entered by the user is empty?
You can use the empty() member function of the string class to check if a string is empty.
Here's an example:
#include <iostream>
#include <string>
int main() {
std::string userInput;
std::cout << "Enter a string: ";
std::getline(std::cin, userInput);
if (userInput.empty()) {
std::cout << "The string is empty." << std::endl;
} else {
std::cout << "The string is not empty." << std::endl;
}
return 0;
}
Q: How can you convert a string entered by the user to uppercase or lowercase?
You can use the toupper() and tolower() functions from the <cctype> header to convert a string to uppercase or lowercase.
Here's an example:
#include <iostream>
#include <string>
#include <cctype>
int main() {
std::string userInput;
std::cout << "Enter a string: ";
std::getline(std::cin, userInput);
// Convert to uppercase
for (char& c : userInput) {
c = std::toupper(c);
}
std::cout << "Uppercase string: " << userInput << std::endl;
// Convert to lowercase
for (char& c : userInput) {
c = std::tolower(c);
}
std::cout << "Lowercase string: " << userInput << std::endl;
return 0;
}
Q: How can you tokenize a string entered by the user based on a delimiter?
You can use the std::stringstream class along with the getline() function to tokenize a string based on a delimiter.
Here's an example:
#include <iostream>
#include <string>
#include <sstream>
int main() {
std::string userInput;
std::cout << "Enter a string: ";
std::getline(std::cin, userInput);
std::stringstream ss(userInput);
std::string token;
while (std::getline(ss, token, ',')) { // Assuming comma (',') as the delimiter
std::cout << "Token: " << token << std::endl;
}
return 0;
}