Skip to content

Added modules source files for chapter 7 examples and exercises #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions Examples/Modules/Chapter 07/Ex7_01.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Concatenating strings

import <iostream>;
import <string>;

int main( )
{
std::string first; // Stores the first name
std::string second; // Stores the second name

std::cout << "Enter your first name: ";
std::cin >> first; // Read first name

std::cout << "Enter your second name: ";
std::cin >> second; // Read second name

std::string sentence { "Your full name is " }; // Create basic sentence
sentence += first + " " + second + "."; // Augment with names

std::cout << sentence << std::endl; // Output the sentence
std::cout << "The string contains " // Output its length
<< sentence.length( ) << " characters." << std::endl;
}
24 changes: 24 additions & 0 deletions Examples/Modules/Chapter 07/Ex7_01A.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Concatenating characters and strings
// (exactly the same as Ex07_01, except that we use ' ' and '.' instead of " " and ".")

import <iostream>;
import <string>;

int main( )
{
std::string first; // Stores the first name
std::string second; // Stores the second name

std::cout << "Enter your first name: ";
std::cin >> first; // Read first name

std::cout << "Enter your second name: ";
std::cin >> second; // Read second name

std::string sentence { "Your full name is " }; // Create basic sentence
sentence += first + ' ' + second + '.'; // Augment with names

std::cout << sentence << std::endl; // Output the sentence
std::cout << "The string contains " // Output its length
<< sentence.length( ) << " characters." << std::endl;
}
37 changes: 37 additions & 0 deletions Examples/Modules/Chapter 07/Ex7_02.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Accessing characters in a string

import <iostream>;
import <string>;

#include <cctype>

int main( )
{
std::string text; // Stores the input
std::cout << "Enter a line of text:\n";

std::getline(std::cin, text); // Read a line including spaces

unsigned vowels { }; // Count of vowels
unsigned consonants { }; // Count of consonants

for ( size_t i { }; i < text.length( ); ++i )
{
if ( std::isalpha(text[i]) ) // Check for a letter
{
switch ( std::tolower(text[i]) ) // Convert to lowercase
{
case 'a': case 'e': case 'i': case 'o': case 'u':
++vowels;
break;

default:
++consonants;
break;
}
}
}

std::cout << "Your input contained " << vowels << " vowels and "
<< consonants << " consonants." << std::endl;
}
40 changes: 40 additions & 0 deletions Examples/Modules/Chapter 07/Ex7_02A.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Accessing characters in a string
// (same as Ex7_02, except that this version uses the more convenient range-based for loop)

import <iostream>;
import <string>;

#include <cctype> // for std::isalpha() and tolower()

int main( )
{
std::string text; // Stores the input

std::cout << "Enter a line of text:\n";
std::getline(std::cin, text); // Read a line including spaces

unsigned vowels { }; // Count of vowels
unsigned consonants { }; // Count of consonants

for ( const char ch : text )
{
if ( std::isalpha(ch) ) // Check for a letter
{
switch ( std::tolower(ch) ) // Convert to lowercase
{
case 'a': case 'e': case 'i': case 'o': case 'u':
++vowels;
break;

default:
++consonants;
break;
}
}
}

std::cout << "Your input contained "
<< vowels << " vowels and "
<< consonants << " consonants."
<< std::endl;
}
58 changes: 58 additions & 0 deletions Examples/Modules/Chapter 07/Ex7_03.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Comparing strings

import <iostream>; // For stream I/O
import <format>; // For string formatting
import <string>; // For the string type
import <vector>; // For the vector container

int main( )
{
std::vector<std::string> names; // Vector of names
std::string input_name; // Stores a name

for ( ; ; ) // Indefinite loop (stopped using break)
{
std::cout << "Enter a name followed by Enter (leave blank to stop): ";
std::getline(std::cin, input_name); // Read a name and...

if ( input_name.empty( ) ) break; // ...if it's not empty...

names.push_back(input_name); // ...add it to the vector
}

// Sort the names in ascending sequence
bool sorted { };
do
{
sorted = true; // remains true when names are sorted
for ( size_t i { 1 }; i < names.size( ); ++i )
{
if ( names[i - 1] > names[i] )
{ // Out of order - so swap names
names[i].swap(names[i - 1]);
sorted = false;
}
}
}
while ( !sorted );

// Find the length of the longest name
size_t max_length { };

for ( const auto& name : names )
if ( max_length < name.length( ) )
max_length = name.length( );

// Output the sorted names 5 to a line
const size_t field_width { max_length + 2 };
size_t count { };

std::cout << "In ascending sequence the names you entered are:\n";
for ( const auto& name : names )
{
std::cout << std::format("{:>{}}", name, field_width); // Right-align + dynamic width
if ( !(++count % 5) ) std::cout << std::endl;
}

std::cout << std::endl;
}
15 changes: 15 additions & 0 deletions Examples/Modules/Chapter 07/Ex7_04.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Searching within strings

import <iostream>;
import <string>;

int main( )
{
std::string sentence { "Manners maketh man" };
std::string word { "man" };

std::cout << sentence.find(word) << std::endl; // Outputs 15
std::cout << sentence.find("Ma") << std::endl; // Outputs 0
std::cout << sentence.find('k') << std::endl; // Outputs 10
std::cout << sentence.find('x') << std::endl; // Outputs std::string::npos
}
28 changes: 28 additions & 0 deletions Examples/Modules/Chapter 07/Ex7_05.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Searching within substrings

import <iostream>;
import <string>;

int main( )
{
std::string text; // The string to be searched
std::string word; // Substring to be found

std::cout << "Enter the string to be searched and press Enter:\n";
std::getline(std::cin, text);

std::cout << "Enter the string to be found and press Enter:\n";
std::getline(std::cin, word);

size_t count { }; // Count of substring occurrences
size_t index { }; // String index

while ( (index = text.find(word, index)) != std::string::npos )
{
++count;
index += word.length( ); // Advance by full word (discards overlapping occurrences)
}

std::cout << "Your text contained " << count << " occurrences of \""
<< word << "\"." << std::endl;
}
40 changes: 40 additions & 0 deletions Examples/Modules/Chapter 07/Ex7_06.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import <iostream>;
import <format>;
import <string>;
import <vector>;

int main( )
{
std::string text; // The string to be searched

std::cout << "Enter some text terminated by *:\n";
std::getline(std::cin, text, '*');

const std::string separators { " ,;:.\"!?'\n" }; // Word delimiters
std::vector<std::string> words; // Words found
size_t start { text.find_first_not_of(separators) }; // First word start index

while ( start != std::string::npos ) // Find the words
{
size_t end { text.find_first_of(separators, start + 1) }; // Find end of word

if ( end == std::string::npos ) // Found a separator?
end = text.length( ); // No, so set to end of text

words.push_back(text.substr(start, end - start)); // Store the word
start = text.find_first_not_of(separators, end + 1); // Find first character of next word
}

std::cout << "Your string contains the following " << words.size( ) << " words:\n";

size_t count { }; // Number output

for ( const auto& word : words )
{
std::cout << std::format("{:15}", word);

if ( !(++count % 5) )
std::cout << std::endl;
}
std::cout << std::endl;
}
36 changes: 36 additions & 0 deletions Examples/Modules/Chapter 07/Ex7_07.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Replacing words in a string

import <iostream>;
import <string>;

int main( )
{
std::string text; // The string to be modified
std::cout << "Enter a string terminated by *:\n";
std::getline(std::cin, text, '*');

std::string word; // The word to be replaced
std::cout << "Enter the word to be replaced: ";
std::cin >> word;

std::string replacement; // The word to be substituted
std::cout << "Enter the string to be substituted for " << word << ": ";
std::cin >> replacement;

if ( word == replacement ) // Verify there's something to do
{
std::cout << "The word and its replacement are the same.\n"
<< "Operation aborted." << std::endl;
return 1;
}

size_t start { text.find(word) }; // Index of 1st occurrence of word

while ( start != std::string::npos ) // Find and replace all occurrences
{
text.replace(start, word.length( ), replacement); // Replace word
start = text.find(word, start + replacement.length( ));
}

std::cout << "\nThe string you entered is now:\n" << text << std::endl;
}
24 changes: 15 additions & 9 deletions Examples/Modules/Chapter 13/Ex13_12/Message.cppm
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
module;

#include <cstring> // For std::strlen() and std::strcpy()

// stop Visual Studio from whinging about strcpy being unsafe
// https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-3-c4996
#pragma warning(disable : 4996)

export module message;

export class Message
{
public:
explicit Message(const char* text = "")
: m_text(new char[std::strlen(text) + 1]) // Caution: include the null character!
{
std::strcpy(m_text, text); // Mind the order: strcpy(destination, source)!
}
~Message() { delete[] m_text; }
explicit Message(const char* text = "")
: m_text(new char[std::strlen(text) + 1]) // Caution: include the null character!
{
std::strcpy(m_text, text); // Mind the order: strcpy(destination, source)!
}
~Message( ) { delete[ ] m_text; }

Message& operator=(const Message& message); // Assignment operator
Message& operator=(const Message& message); // Assignment operator

const char* getText() const { return m_text; }
const char* getText( ) const { return m_text; }

private:
char* m_text;
char* m_text;
};
24 changes: 14 additions & 10 deletions Examples/Modules/Chapter 13/Ex13_12A/Message.cppm
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
module;

#include <cstring> // For std::strlen() and std::strcpy()

#pragma warning(disable : 4996)

export module message;

export class Message
{
public:
explicit Message(const char* text = "")
: m_text(new char[std::strlen(text) + 1]) // Caution: include the null character!
{
std::strcpy(m_text, text); // Mind the order: strcpy(destination, source)!
}
~Message() { delete[] m_text; }
explicit Message(const char* text = "")
: m_text(new char[std::strlen(text) + 1]) // Caution: include the null character!
{
std::strcpy(m_text, text); // Mind the order: strcpy(destination, source)!
}
~Message( ) { delete[ ] m_text; }

Message(const Message& message); // Copy constructor
Message& operator=(const Message& message); // Copy assignment operator
Message(const Message& message); // Copy constructor
Message& operator=(const Message& message); // Copy assignment operator

const char* getText() const { return m_text; }
const char* getText( ) const { return m_text; }

private:
char* m_text;
char* m_text;
};
Loading