diff --git a/public/consolidated/cpp.json b/public/consolidated/cpp.json index 71066e57..a955eb38 100644 --- a/public/consolidated/cpp.json +++ b/public/consolidated/cpp.json @@ -49,12 +49,37 @@ ], "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" } ] }, { "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.", 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..4f73ff0c --- /dev/null +++ b/snippets/cpp/math-and-numbers/odd-even-check.md @@ -0,0 +1,28 @@ +--- +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 +--- + +```cpp + +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] +} + +``` + 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