Write a c++ program. User enters a string. A function prints the word ant another ine…
- User
Write a c++ program. User enters a string. A function prints the word ant another ine counts them
- Assistant
Here's a C++ program that allows the user to enter a string. The program then prints the words in the string and counts the number of words.
#include <iostream> #include <string> #include <sstream> // Function to print the words in a string void printWords(const std::string& str) { std::istringstream iss(str); std::string word; while (iss >> word) { std::cout << word << std::endl; } } // Function to count the number of words in a string int countWords(const std::string& str) { int count = 0; std::istringstream iss(str); std::string word; while (iss >> word) { count++; } return count; } int main() { std::string input; std::cout << "Enter a string: "; std::getline(std::cin, input); std::cout << "Words in the string:" << std::endl; printWords(input); int wordCount = countWords(input); std::cout << "Number of words: " << wordCount << std::endl; return 0; }In this program, we define two functions
printWordsandcountWords. TheprintWordsfunction uses anistringstreamto extract words from the input string and prints them one by one. ThecountWordsfunction also uses anistringstreambut counts the number of words using a counter.In the
mainfunction, we get the user input usingstd::getlineand store it in theinputstring. We then call theprintWordsfunction to print the words in the string and thecountWordsfunction to get the number of words. Finally, we display the word count to the user. - User
Now do it with c-strings instead
- Assistant
Certainly! Here's a modified version of the program that uses C-strings instead of
std::stringandstd::istringstream:#include <iostream> #include <cstring> // Function to print the words in a C-string void printWords(const char* str) { char* word = strtok(const_cast<char*>(str), " "); while (word != nullptr) { std::cout << word << std::endl; word = strtok(nullptr, " "); } } // Function to count the number of words in a C-string int countWords(const char* str) { int count = 0; char* word = strtok(const_cast<char*>(str), " "); while (word != nullptr) { count++; word = strtok(nullptr, " "); } return count; } int main() { const int maxLength = 100; char input[maxLength]; std::cout << "Enter a string: "; std::cin.getline(input, maxLength); std::cout << "Words in the string:" << std::endl; printWords(input); int wordCount = countWords(input); std::cout << "Number of words: " << wordCount << std::endl; return 0; }In this modified version, we use
strtokfrom<cstring>to tokenize the C-string based on the delimiter space character. TheprintWordsfunction usesstrtokto extract and print each word, while thecountWordsfunction uses it to count the number of words.In the
mainfunction, we define aconst int maxLengthto specify the maximum length of the input string. We then declare a character array,input, of sizemaxLengthto store the user input usingstd::cin.getline. Finally, we call theprintWordsandcountWordsfunctions, and display the word count to the user.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.