0% found this document useful (0 votes)
1 views

Reverse_String_Extended_Project.AD

This document presents a C++ program that reverses a string using a stack data structure and includes additional features such as palindrome checking, character counting, and saving the reversed string to a file. It defines a Stack class, functions for string manipulation, and implements a main function to interact with the user. The program also includes a typing effect for displaying the reversed string and measures the time taken for the reversal process.

Uploaded by

amriandhillon
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Reverse_String_Extended_Project.AD

This document presents a C++ program that reverses a string using a stack data structure and includes additional features such as palindrome checking, character counting, and saving the reversed string to a file. It defines a Stack class, functions for string manipulation, and implements a main function to interact with the user. The program also includes a typing effect for displaying the reversed string and measures the time taken for the reversal process.

Uploaded by

amriandhillon
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Reverse a String using Stack (Extended Version)

// Project: Reverse a String using Stack (Extended Version)


// Author: Amritpal Dhillon (Updated by Assistant)
// Description: Extended with additional features.

#include <iostream>
#include <string>
#include <chrono>
#include <thread>
#include <fstream>
#include <cctype>

using namespace std;


using namespace chrono;

// ------------------------------
// Stack Class Definition
// ------------------------------
class Stack {
private:
char* arr;
int top;
int capacity;

public:
Stack(int size) {
arr = new char[size];
capacity = size;
top = -1;
}
~Stack() {
delete[] arr;
}
void push(char ch) {
if (top == capacity - 1) {
cout << "Stack Overflow\n";
return;
}
arr[++top] = ch;
}
char pop() {
if (isEmpty()) {
cout << "Stack Underflow\n";
return '\0';
}
return arr[top--];
}
bool isEmpty() {
return top == -1;
}
};
// ------------------------------
// Function Definitions
// ------------------------------
string reverseString(const string& str) {
Stack s(str.length());
for (char ch : str) {
s.push(ch);
}
string reversed;
while (!s.isEmpty()) {
reversed += s.pop();
}
return reversed;
}

bool isPalindrome(const string& str) {


int left = 0, right = str.length() - 1;
while (left < right) {
if (str[left] != str[right])
return false;
left++;
right--;
}
return true;
}

void countCharacters(const string& str) {


int vowels = 0, consonants = 0, spaces = 0, specials = 0;
for (char ch : str) {
if (isalpha(ch)) {
char lower = tolower(ch);
if (lower == 'a' || lower == 'e' || lower == 'i' || lower == 'o' || lower ==
'u')
vowels++;
else
consonants++;
}
else if (isspace(ch))
spaces++;
else if (ispunct(ch))
specials++;
}
cout << "\nCharacter Analysis:\n";
cout << "Vowels: " << vowels << endl;
cout << "Consonants: " << consonants << endl;
cout << "Spaces: " << spaces << endl;
cout << "Special Characters: " << specials << endl;
}

string toUpperCase(const string& str) {


string upper = str;
for (char& ch : upper) {
ch = toupper(ch);
}
return upper;
}

void saveToFile(const string& str) {


ofstream outfile("output.txt");
if (outfile.is_open()) {
outfile << str;
outfile.close();
cout << "\nReversed string saved to output.txt successfully!\n";
} else {
cout << "\nError opening file for writing!\n";
}
}

void displayASCII() {
cout << "-------------------------------" << endl;
cout << "| Reverse String App |" << endl;
cout << "-------------------------------" << endl;
}

void typingEffect(const string& text, int delay = 100) {


for (char ch : text) {
cout << ch << flush;
this_thread::sleep_for(milliseconds(delay));
}
cout << endl;
}

// ------------------------------
// Main Function
// ------------------------------
int main() {
string input;
displayASCII();
cout << "\nEnter a string to reverse: ";
getline(cin, input);

auto start = high_resolution_clock::now();


string reversed = reverseString(input);
auto stop = high_resolution_clock::now();
auto duration = duration_cast<milliseconds>(stop - start);

int delay = (input.length() > 10) ? 50 : 100;

cout << "\nReversed string is: ";


typingEffect(reversed, delay);

cout << "\nTime taken to reverse: " << duration.count() << " milliseconds\n";
cout << "\nLength of the entered string: " << input.length() << endl;

if (isPalindrome(input))
cout << "The entered string is a Palindrome!\n";
else
cout << "The entered string is NOT a Palindrome.\n";
if (isPalindrome(reversed))
cout << "The reversed string is also a Palindrome!\n";
else
cout << "The reversed string is NOT a Palindrome.\n";

cout << "\nReversed string in UPPERCASE: " << toUpperCase(reversed) << endl;

countCharacters(input);
saveToFile(reversed);

return 0;
}

You might also like