Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· openaiHugging Face Suzhen/CodeChat

Write a c++ program. User enters a string. A function prints the word ant another ine…

  1. User

    Write a c++ program. User enters a string. A function prints the word ant another ine counts them

  2. 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 printWords and countWords. The printWords function uses an istringstream to extract words from the input string and prints them one by one. The countWords function also uses an istringstream but counts the number of words using a counter.

    In the main function, we get the user input using std::getline and store it in the input string. We then call the printWords function to print the words in the string and the countWords function to get the number of words. Finally, we display the word count to the user.

  3. User

    Now do it with c-strings instead

  4. Assistant

    Certainly! Here's a modified version of the program that uses C-strings instead of std::string and std::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 strtok from <cstring> to tokenize the C-string based on the delimiter space character. The printWords function uses strtok to extract and print each word, while the countWords function uses it to count the number of words.

    In the main function, we define a const int maxLength to specify the maximum length of the input string. We then declare a character array, input, of size maxLength to store the user input using std::cin.getline. Finally, we call the printWords and countWords functions, and display the word count to the user.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.