Below is a simple C++ program according to your request, using C-style strings. It defines two functions: printWords to print each word of the input string on a separate line, and countWords to return the number of words in the string. The program uses std::cin.getline to read a line of text into a C-string (a character array).
#include <iostream>
#include <cstring>
// Function to print words in the string
void printWords(const char* str) {
const char* delimiter = " ";
char* token;
// Create a copy of str because strtok modifies the string
char* str_copy = new char[strlen(str) + 1];
strcpy(str_copy, str);
// Tokenize the string and print each word
token = strtok(str_copy, delimiter);
while (token != nullptr) {
std::cout << token << std::endl;
token = strtok(nullptr, delimiter);
}
// Clean up the dynamic memory
delete[] str_copy;
}
// Function to count the number of words in the string
int countWords(const char* str) {
int count = 0;
const char* delimiter = " ";
char* token;
// Create a copy of str because strtok modifies the string
char* str_copy = new char[strlen(str) + 1];
strcpy(str_copy, str);
// Tokenize the string and count words
token = strtok(str_copy, delimiter);
while (token != nullptr) {
count++;
token = strtok(nullptr, delimiter);
}
// Clean up the dynamic memory
delete[] str_copy;
return count;
}
int main() {
const int MAX_LENGTH = 1024; // Define maximum length for input string
char str[MAX_LENGTH];
std::cout << "Enter a string: ";
std::cin.getline(str, MAX_LENGTH);
// Call the printWords function
std::cout << "The words in the string are:" << std::endl;
printWords(str);
// Call the countWords function and print the count of words
int wordCount = countWords(str);
std::cout << "Number of words in the string: " << wordCount << std::endl;
return 0;
}
In this example, I've used strtok to tokenize the string, which requires a non-const character array as it modifies the input string. Thus, a copy of the input is made each time before tokenizing. Please also note that strtok uses a static buffer internally, which means it's not thread-safe. If you were to make this program multi-threaded or required to handle multiple strings at once, you might need to use a thread-safe alternative such as strtok_r.
Remember that when dealing with C-style strings in the context of modern C++, it is generally recommended to use std::string due to the safety and convenience it provides. The above example is specifically tailored to your requirement of using C-strings.
If you want to read a line of input without knowing its length in advance and ensure that you read the entire line, you can use std::string and the std::getline function from the C++ standard library with a std::stringstream. Here's an alternative version of the program that does not limit the input size:
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
// Function to print words in the 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 the string
int countWords(const std::string& str) {
std::istringstream iss(str);
std::string word;
int count = 0;
while (iss >> word) {
++count;
}
return count;
}
int main() {
std::string str;
std::cout << "Enter a string: ";
std::getline(std::cin, str);
// Call the printWords function
std::cout << "The words in the string are:" << std::endl;
printWords(str);
// Call the countWords function and print the count of words
int wordCount = countWords(str);
std::cout << "Number of words in the string: " << wordCount << std::endl;
return 0;
}
In this version of the program, we're using std::string to store the user input. The std::getline(std::cin, str) function reads the entire line of text entered by the user, regardless of its length, and stores it in str.