From 439ca978bac8ffcfc6cd59bf18593302713b7fab Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Tue, 26 Jan 2021 22:00:25 +0000 Subject: Challenge 001: fix Perl and Forth, add Basic, C, C++, Lua and Python solutions --- challenge-001/paulo-custodio/cpp/ch-1.cpp | 34 +++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 challenge-001/paulo-custodio/cpp/ch-1.cpp (limited to 'challenge-001/paulo-custodio/cpp/ch-1.cpp') diff --git a/challenge-001/paulo-custodio/cpp/ch-1.cpp b/challenge-001/paulo-custodio/cpp/ch-1.cpp new file mode 100644 index 0000000000..cda2637b4f --- /dev/null +++ b/challenge-001/paulo-custodio/cpp/ch-1.cpp @@ -0,0 +1,34 @@ +/* +Challenge 001 + +Challenge #1 +Write a script to replace the character ‘e’ with ‘E’ in the string +‘Perl Weekly Challenge’. Also print the number of times the character ‘e’ +is found in the string. +*/ + +#include +#include + +int replace_e(std::string& text) { + int count = 0; + for (auto& c : text) { + if (c == 'e') { + c = 'E'; + count++; + } + } + return count; +} + +int main(int argc, char* argv[]) { + std::string text; + for (int i = 1; i < argc; i++) { + text += argv[i]; + text += " "; + } + if (!text.empty()) + text.pop_back(); // remove last space + + std::cout << replace_e(text) << " " << text << std::endl; +} -- cgit From b849218f211b83137245da638645e58f494de51b Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Tue, 26 Jan 2021 22:01:40 +0000 Subject: remove tabs --- challenge-001/paulo-custodio/cpp/ch-1.cpp | 38 +++++++++++++++---------------- 1 file changed, 19 insertions(+), 19 deletions(-) (limited to 'challenge-001/paulo-custodio/cpp/ch-1.cpp') diff --git a/challenge-001/paulo-custodio/cpp/ch-1.cpp b/challenge-001/paulo-custodio/cpp/ch-1.cpp index cda2637b4f..057288b514 100644 --- a/challenge-001/paulo-custodio/cpp/ch-1.cpp +++ b/challenge-001/paulo-custodio/cpp/ch-1.cpp @@ -2,8 +2,8 @@ Challenge 001 Challenge #1 -Write a script to replace the character ‘e’ with ‘E’ in the string -‘Perl Weekly Challenge’. Also print the number of times the character ‘e’ +Write a script to replace the character ‘e’ with ‘E’ in the string +‘Perl Weekly Challenge’. Also print the number of times the character ‘e’ is found in the string. */ @@ -11,24 +11,24 @@ is found in the string. #include int replace_e(std::string& text) { - int count = 0; - for (auto& c : text) { - if (c == 'e') { - c = 'E'; - count++; - } - } - return count; + int count = 0; + for (auto& c : text) { + if (c == 'e') { + c = 'E'; + count++; + } + } + return count; } int main(int argc, char* argv[]) { - std::string text; - for (int i = 1; i < argc; i++) { - text += argv[i]; - text += " "; - } - if (!text.empty()) - text.pop_back(); // remove last space - - std::cout << replace_e(text) << " " << text << std::endl; + std::string text; + for (int i = 1; i < argc; i++) { + text += argv[i]; + text += " "; + } + if (!text.empty()) + text.pop_back(); // remove last space + + std::cout << replace_e(text) << " " << text << std::endl; } -- cgit