From b2caf4ed5d65e094e1e79957935e708962c13fec Mon Sep 17 00:00:00 2001 From: beast177 Date: Thu, 2 Jan 2025 15:42:18 +0530 Subject: [PATCH 1/6] added odd even number check --- snippets/cpp/math-and-numbers/odd-even-check.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 snippets/cpp/math-and-numbers/odd-even-check.md diff --git a/snippets/cpp/math-and-numbers/odd-even-check.md b/snippets/cpp/math-and-numbers/odd-even-check.md new file mode 100644 index 00000000..5cdd4cc2 --- /dev/null +++ b/snippets/cpp/math-and-numbers/odd-even-check.md @@ -0,0 +1,15 @@ +--- +title: cpp, number, bitwise, even, odd +description : This code demonstrates the fastest way to check if a number is even or odd using bitwise operations. It's more efficient than using the modulor operator (%) since bitwise operations are performed at the CPU level. +tags : cpp, number, even, odd, bitwise +author : Beast177 +--- + +```cpp + +bool isEven(int num) { + return !(num & 1); +} + +``` + From d1f218b6e229e0255ed29b528de6e2fedec1b2cc Mon Sep 17 00:00:00 2001 From: beast177 Date: Thu, 2 Jan 2025 18:40:37 +0530 Subject: [PATCH 2/6] added usage --- .../cpp/math-and-numbers/odd-even-check.md | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/snippets/cpp/math-and-numbers/odd-even-check.md b/snippets/cpp/math-and-numbers/odd-even-check.md index 5cdd4cc2..d0348713 100644 --- a/snippets/cpp/math-and-numbers/odd-even-check.md +++ b/snippets/cpp/math-and-numbers/odd-even-check.md @@ -1,8 +1,8 @@ --- title: cpp, number, bitwise, even, odd -description : This code demonstrates the fastest way to check if a number is even or odd using bitwise operations. It's more efficient than using the modulor operator (%) since bitwise operations are performed at the CPU level. -tags : cpp, number, even, odd, bitwise -author : Beast177 +description: This code demonstrates the fastest way to check if a number is even or odd using bitwise operations. It's more efficient than using the modulor operator (%) since bitwise operations are performed at the CPU level. +tags: cpp, number, even, odd, bitwise +author: Beast177 --- ```cpp @@ -11,5 +11,18 @@ bool isEven(int num) { return !(num & 1); } +// usage -- [odd-even] + +#include + +int main(){ + + int num1 = 2; + int num2 = 3; + + std::cout << num1 << " is " << (isEven(num1) ? "Even" : "Odd") << "\n"; // output - [Even] + std::cout << num2 << " is " << (isEven(num2) ? "Even" : "Odd") << "\n"; // output - [Odd] +} + ``` From a6dcad20fe052d95412d0a1014ac618e6da43ce8 Mon Sep 17 00:00:00 2001 From: beast177 Date: Fri, 3 Jan 2025 10:03:50 +0530 Subject: [PATCH 3/6] Added appropriate title --- snippets/cpp/math-and-numbers/odd-even-check.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/cpp/math-and-numbers/odd-even-check.md b/snippets/cpp/math-and-numbers/odd-even-check.md index d0348713..4f73ff0c 100644 --- a/snippets/cpp/math-and-numbers/odd-even-check.md +++ b/snippets/cpp/math-and-numbers/odd-even-check.md @@ -1,5 +1,5 @@ --- -title: cpp, number, bitwise, even, odd +title: Even-Odd Check description: This code demonstrates the fastest way to check if a number is even or odd using bitwise operations. It's more efficient than using the modulor operator (%) since bitwise operations are performed at the CPU level. tags: cpp, number, even, odd, bitwise author: Beast177 From f53a33e8bb86db070d1e3809239b5853ab4447aa Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Fri, 3 Jan 2025 04:34:38 +0000 Subject: [PATCH 4/6] Update consolidated snippets --- public/consolidated/cpp.json | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/public/consolidated/cpp.json b/public/consolidated/cpp.json index 71066e57..5b665d50 100644 --- a/public/consolidated/cpp.json +++ b/public/consolidated/cpp.json @@ -49,6 +49,20 @@ ], "contributors": [], "code": "bool is_prime(int n) {\n if (n < 2) return false;\n if (n == 2 || n == 3) return true;\n if (n % 2 == 0) return false;\n for (int i = 3; i * i <= n; i += 2) {\n if (n % i == 0) return false;\n }\n return true;\n}\n\n// Usage\n#include \n\nint main() {\n std::cout << is_prime(29) << std::endl; // Output: 1\n return 0;\n}\n" + }, + { + "title": "Even-Odd Check", + "description": "This code demonstrates the fastest way to check if a number is even or odd using bitwise operations. It's more efficient than using the modulor operator (%) since bitwise operations are performed at the CPU level.", + "author": "Beast177 ", + "tags": [ + "cpp", + "number", + "even", + "odd", + "bitwise" + ], + "contributors": [], + "code": "\nbool isEven(int num) {\n return !(num & 1);\n}\n\n// usage -- [odd-even]\n\n#include \n\nint main(){\n\n int num1 = 2;\n int num2 = 3;\n\n std::cout << num1 << \" is \" << (isEven(num1) ? \"Even\" : \"Odd\") << \"\\n\"; // output - [Even] \n std::cout << num2 << \" is \" << (isEven(num2) ? \"Even\" : \"Odd\") << \"\\n\"; // output - [Odd]\n}\n\n" } ] }, From d36f0f083c16eaca1899d39de9d4f38aaaef7cb6 Mon Sep 17 00:00:00 2001 From: beast177 Date: Fri, 3 Jan 2025 10:15:55 +0530 Subject: [PATCH 5/6] Count Occurrences --- .../string-manipulation/count-occurrences.md | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 snippets/cpp/string-manipulation/count-occurrences.md diff --git a/snippets/cpp/string-manipulation/count-occurrences.md b/snippets/cpp/string-manipulation/count-occurrences.md new file mode 100644 index 00000000..c3c7871a --- /dev/null +++ b/snippets/cpp/string-manipulation/count-occurrences.md @@ -0,0 +1,32 @@ +--- +title: Count Occurrences +description: Counts occurrences of a substring, uses modern C++ 20 +author: beast177 +tags: cpp,string +--- + +```cpp +#include +#include +#include + +static size_t countOccurrences(std::string_view text, std::string_view substr) { + size_t count = 0; + size_t pos = 0; + while ((pos = text.find(substr, pos)) != std::string_view::npos) { + ++count; + pos += substr.length(); + } + return count; +} + +// use case -- + +int main(){ + // Count and replace + std::string text = "Hello, world"; + + auto count = countOccurrences(text, "l"); + std::cout << std::format("Count of 'l': {}\n", count); // output -> Count of 'l' : 3 +} +``` \ No newline at end of file From 70074342084d0df97a5f07d28fd3eb5a1996300a Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Fri, 3 Jan 2025 04:46:53 +0000 Subject: [PATCH 6/6] Update consolidated snippets --- public/consolidated/cpp.json | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/public/consolidated/cpp.json b/public/consolidated/cpp.json index 5b665d50..a955eb38 100644 --- a/public/consolidated/cpp.json +++ b/public/consolidated/cpp.json @@ -69,6 +69,17 @@ { "categoryName": "String Manipulation", "snippets": [ + { + "title": "Count Occurrences", + "description": "Counts occurrences of a substring, uses modern C++ 20", + "author": "beast177", + "tags": [ + "cpp", + "string" + ], + "contributors": [], + "code": "#include \n#include \n#include \n\nstatic size_t countOccurrences(std::string_view text, std::string_view substr) {\n size_t count = 0;\n size_t pos = 0;\n while ((pos = text.find(substr, pos)) != std::string_view::npos) {\n ++count;\n pos += substr.length();\n }\n return count;\n}\n\n// use case --\n\nint main(){\n // Count and replace\n std::string text = \"Hello, world\";\n\n auto count = countOccurrences(text, \"l\");\n std::cout << std::format(\"Count of 'l': {}\\n\", count); // output -> Count of 'l' : 3\n}\n" + }, { "title": "Reverse String", "description": "Reverses the characters in a string.",