From 7ce77a757c37ce897d2ac982fabb36e52a638dd1 Mon Sep 17 00:00:00 2001 From: "Hamster [bot]" Date: Sat, 1 Feb 2025 21:42:37 +0530 Subject: [PATCH] Upload 3 C++ Programs (#10033) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Vedansh Co-authored-by: Manish ✨ --- C++/NumberLife.cxx | 95 ++++++++++++++++++++++++++++++++++++++ C++/StringModify.cpp | 103 ++++++++++++++++++++++++++++++++++++++++++ C++/StringReverse.cpp | 14 ++++++ 3 files changed, 212 insertions(+) create mode 100644 C++/NumberLife.cxx create mode 100644 C++/StringModify.cpp create mode 100644 C++/StringReverse.cpp diff --git a/C++/NumberLife.cxx b/C++/NumberLife.cxx new file mode 100644 index 0000000..d1d747a --- /dev/null +++ b/C++/NumberLife.cxx @@ -0,0 +1,95 @@ +#include +#include +#include +#include +#include +#include + +using namespace std; + +// Function to generate a random password +string generatePassword(int length) { + const string characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_+=-`~[]\{}|;':\",./<>?"; + random_device rd; + mt19937 gen(rd()); + uniform_int_distribution<> dist(0, characters.size() - 1); + + string password = ""; + for (int i = 0; i < length; ++i) { + password += characters[dist(gen)]; + } + return password; +} + +// Function to check if a number is prime (basic implementation) +bool isPrime(int number) { + if (number <= 1) return false; + for (int i = 2; i * i <= number; ++i) { + if (number % i == 0) return false; + } + return true; +} + +int main() { + cout << "\nWelcome to the Super Cool C++ Program!\n"; + + int choice; + do { + cout << "\nChoose an option:\n"; + cout << "1. Generate a Random Password\n"; + cout << "2. Check if a Number is Prime\n"; + cout << "3. Sort a List of Numbers\n"; + cout << "4. Exit\n"; + cout << "Enter your choice: "; + cin >> choice; + + switch (choice) { + case 1: { + int length; + cout << "Enter desired password length: "; + cin >> length; + string password = generatePassword(length); + cout << "Generated Password: " << password << endl; + break; + } + case 2: { + int number; + cout << "Enter a number to check for primality: "; + cin >> number; + if (isPrime(number)) { + cout << number << " is a prime number.\n"; + } else { + cout << number << " is not a prime number.\n"; + } + break; + } + case 3: { + int numCount; + cout << "How many numbers do you want to sort? "; + cin >> numCount; + + vector numbers(numCount); + cout << "Enter the numbers:\n"; + for (int i = 0; i < numCount; ++i) { + cin >> numbers[i]; + } + + sort(numbers.begin(), numbers.end()); // Using STL sort + + cout << "Sorted Numbers:\n"; + for (int num : numbers) { + cout << num << " "; + } + cout << endl; + break; + } + case 4: + cout << "Exiting program. Goodbye!\n"; + break; + default: + cout << "Invalid choice. Please try again.\n"; + } + } while (choice != 4); + + return 0; +} \ No newline at end of file diff --git a/C++/StringModify.cpp b/C++/StringModify.cpp new file mode 100644 index 0000000..6e642be --- /dev/null +++ b/C++/StringModify.cpp @@ -0,0 +1,103 @@ +#include +#include +#include +#include +#include // For character manipulation + +using namespace std; + +// Function to convert a string to uppercase +string toUpper(string str) { + string result = str; + for (char &c : result) { + c = toupper(c); + } + return result; +} + +// Function to check if a string is a palindrome (ignoring case and spaces) +bool isPalindrome(string str) { + string cleanStr = ""; + for (char c : str) { + if (isalnum(c)) { // Keep only alphanumeric characters + cleanStr += tolower(c); // Convert to lowercase + } + } + string reversedStr = cleanStr; + reverse(reversedStr.begin(), reversedStr.end()); + return cleanStr == reversedStr; +} + +// Function to count the frequency of each letter in a string +void countLetterFrequency(string str) { + vector frequencies(26, 0); // Initialize counts to 0 for each letter (a-z) + + for (char c : str) { + if (isalpha(c)) { + char lowerC = tolower(c); + frequencies[lowerC - 'a']++; + } + } + + cout << "Letter Frequencies:\n"; + for (int i = 0; i < 26; ++i) { + if (frequencies[i] > 0) { + cout << (char)('a' + i) << ": " << frequencies[i] << endl; + } + } +} + + + +int main() { + cout << "\nWelcome to the String Manipulation Program!\n"; + + int choice; + do { + cout << "\nChoose an option:\n"; + cout << "1. Convert String to Uppercase\n"; + cout << "2. Check if String is a Palindrome\n"; + cout << "3. Count Letter Frequencies\n"; + cout << "4. Exit\n"; + cout << "Enter your choice: "; + cin.ignore(); // Clear the newline character from previous input + cin >> choice; + cin.ignore(); // Clear the newline character from previous input + + + switch (choice) { + case 1: { + string inputString; + cout << "Enter a string: "; + getline(cin, inputString); // Use getline to handle spaces + cout << "Uppercase: " << toUpper(inputString) << endl; + break; + } + case 2: { + string inputString; + cout << "Enter a string: "; + getline(cin, inputString); + if (isPalindrome(inputString)) { + cout << "\"" << inputString << "\" is a palindrome.\n"; + } else { + cout << "\"" << inputString << "\" is not a palindrome.\n"; + } + break; + } + case 3: { + string inputString; + cout << "Enter a string: "; + getline(cin, inputString); + countLetterFrequency(inputString); + break; + } + case 4: + cout << "Exiting program. Goodbye!\n"; + break; + default: + cout << "Invalid choice. Please try again.\n"; + } + } while (choice != 4); + + return 0; +} \ No newline at end of file diff --git a/C++/StringReverse.cpp b/C++/StringReverse.cpp new file mode 100644 index 0000000..79a26cc --- /dev/null +++ b/C++/StringReverse.cpp @@ -0,0 +1,14 @@ +#include +#include +#include + +int main() { + string input; + std::cout << "Enter a string to reverse: "; + getline(cin, input); + std::reverse(input.begin(), input.end()); + + std::cout << "Reversed string: " << input << std::endl; + + return 0; +} \ No newline at end of file