Skip to content

Commit

Permalink
Upload 3 C++ Programs (#10033)
Browse files Browse the repository at this point in the history

Co-authored-by: Vedansh <[email protected]>
Co-authored-by: Manish ✨ <[email protected]>
  • Loading branch information
3 people authored Feb 1, 2025
1 parent faa511f commit 7ce77a7
Show file tree
Hide file tree
Showing 3 changed files with 212 additions and 0 deletions.
95 changes: 95 additions & 0 deletions C++/NumberLife.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <random>
#include <chrono>

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<int> 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;
}
103 changes: 103 additions & 0 deletions C++/StringModify.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cctype> // 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<int> 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;
}
14 changes: 14 additions & 0 deletions C++/StringReverse.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <iostream>
#include <string>
#include <algorithm>

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;
}

0 comments on commit 7ce77a7

Please sign in to comment.