~hamblingreen/practical-cpp

0c9013977d80c04b5ff649443831bb71cd9714a0 — Bobby Hamblin 7 months ago f638888
chapter 9 WIP
A 09/strbegins/Makefile => 09/strbegins/Makefile +13 -0
@@ 0,0 1,13 @@
# Makefile for program strbegins

# Turn on debugging
CXXFLAGS=-g

strbegins:strbegins.cpp
	$(CXX) $(CXXFLAGS) $^ -o $@

clean:strbegins
	rm $^

run:strbegins
	./$^

A 09/strbegins/strbegins.cpp => 09/strbegins/strbegins.cpp +48 -0
@@ 0,0 1,48 @@
/****************************************
* word-count -- count words in a string	*
* Usage: Input sentence at prompt	*
****************************************/

#include <iostream>


/****************************************
* count_words -- count words in a	*
*   string				*
* Parameters:				*
*   sentence -- string to count words	*
* Returns:				*
*   number of words in string		*
****************************************/
int count_words(std::string sentence) {
	int word_count = 0; // word count

	// increment word count if sentence contains text
	if(sentence != "")
		word_count++;

	// iterate through each character in string, incrementing word count for each
	for(int i = 0; i < sentence.length(); i++) {
		if(sentence[i] == ' ')
			word_count++;
	}

	return(word_count);
}

int main() {
	std::string sentence; // sentence to count
	int words; // number of words in sentence

	int count_words(std::string sentence); // word count function prototype

	// print program name
	std::cout << "word-count\n";

	// get and display word count
	std::cout << "Input sentence to count: ";
	std::getline(std::cin, sentence);
	words = count_words(sentence);
	std::cout << "The sentence input has " << words << " words\n";
}


A 09/strbegins/strbegins.md => 09/strbegins/strbegins.md +14 -0
@@ 0,0 1,14 @@
# strbegins

> Count words in a string
> Users' Specification
> Jan 20, 2023 Bobby Hamblin

Counts the number of words in an input string. A word is defined as a
collection of characters terminated by a space.

## Example

> Enter sentence to count: *This is an example sentence *
> The sentence input has 5 words


A 09/word-count/Makefile => 09/word-count/Makefile +13 -0
@@ 0,0 1,13 @@
# Makefile for program word-count

# Turn on debugging
CXXFLAGS=-g

word-count:word-count.cpp
	$(CXX) $(CXXFLAGS) $^ -o $@

clean:word-count
	rm $^

run:word-count
	./$^

A 09/word-count/word-count.cpp => 09/word-count/word-count.cpp +48 -0
@@ 0,0 1,48 @@
/****************************************
* word-count -- count words in a string	*
* Usage: Input sentence at prompt	*
****************************************/

#include <iostream>


/****************************************
* count_words -- count words in a	*
*   string				*
* Parameters:				*
*   sentence -- string to count words	*
* Returns:				*
*   number of words in string		*
****************************************/
int count_words(std::string sentence) {
	int word_count = 0; // word count

	// increment word count if sentence contains text
	if(sentence != "")
		word_count++;

	// iterate through each character in string, incrementing word count for each
	for(int i = 0; i < sentence.length(); i++) {
		if(sentence[i] == ' ')
			word_count++;
	}

	return(word_count);
}

int main() {
	std::string sentence; // sentence to count
	int words; // number of words in sentence

	int count_words(std::string sentence); // word count function prototype

	// print program name
	std::cout << "word-count\n";

	// get and display word count
	std::cout << "Input sentence to count: ";
	std::getline(std::cin, sentence);
	words = count_words(sentence);
	std::cout << "The sentence input has " << words << " words\n";
}


A 09/word-count/word-count.md => 09/word-count/word-count.md +14 -0
@@ 0,0 1,14 @@
# word-count

> Count words in a string
> Users' Specification
> Jan 20, 2023 Bobby Hamblin

Counts the number of words in an input string. A word is defined as a
collection of characters terminated by a space.

## Example

> Enter sentence to count: *This is an example sentence *
> The sentence input has 5 words