← Back to Home

Homeworks

Stack Implementation

Data Structures

A stack data structure for integers using a fixed-size array (no dynamic allocation). Supports push, pop, top, is_empty, and is_full operations.

stack.c
#include <stdio.h>

#define MAX_SIZE 100

int stack[MAX_SIZE];
int top_index = -1;

int is_empty() {
    return top_index == -1;
}

int is_full() {
    return top_index == MAX_SIZE - 1;
}

void push(int value) {
    if (is_full()) {
        printf("Stack overflow!\n");
        return;
    }
    stack[++top_index] = value;
}

void pop() {
    if (is_empty()) {
        printf("Stack underflow!\n");
        return;
    }
    top_index--;
}

int top() {
    if (is_empty()) {
        printf("Stack is empty!\n");
        return -1;
    }
    return stack[top_index];
}

int main() {
    push(10);
    push(20);
    push(30);

    printf("Top element: %d\n", top());
    pop();
    printf("After pop, top: %d\n", top());
    printf("Is empty: %d\n", is_empty());

    return 0;
}
Download .c
Output:
Top element: 30
After pop, top: 20
Is empty: 0

Caesar Cipher Encryption

Encryption

A basic Caesar cipher encryption system. Each uppercase letter is shifted by a key value, wrapping around from Z to A. Other characters remain unchanged.

caesar.c
#include <stdio.h>

void caesar_encrypt(char* message, int key) {
    for (int i = 0; message[i] != '\0'; i++) {
        char c = message[i];

        if (c >= 'A' && c <= 'Z') {
            c = (c - 'A' + key) % 26 + 'A';
            message[i] = c;
        }
    }
}

int main() {
    char message[] = "BAREV ASHXARH";
    int key = 3;

    printf("Original: %s\n", message);
    caesar_encrypt(message, key);
    printf("Encrypted: %s\n", message);

    return 0;
}
Download .c
Output:
Original: BAREV ASHXARH
Encrypted: EDUHV DVKADUK

Integer Number Parser (FSM)

Finite State Machine

A Finite State Machine that validates whether a string represents a valid integer. Accepts formats like "123", "-456", "+78". Rejects invalid inputs like "--12", "12a", "+", or empty strings.

int_parser.c
#include <stdio.h>

typedef enum {
    START,
    SIGN,
    DIGIT,
    INVALID
} State;

int is_valid_integer(const char* str) {
    State state = START;

    if (str == NULL || str[0] == '\0') {
        return 0;
    }

    for (int i = 0; str[i] != '\0'; i++) {
        char c = str[i];

        switch (state) {
            case START:
                if (c == '+' || c == '-') {
                    state = SIGN;
                } else if (c >= '0' && c <= '9') {
                    state = DIGIT;
                } else {
                    state = INVALID;
                }
                break;

            case SIGN:
                if (c >= '0' && c <= '9') {
                    state = DIGIT;
                } else {
                    state = INVALID;
                }
                break;

            case DIGIT:
                if (c >= '0' && c <= '9') {
                    state = DIGIT;
                } else {
                    state = INVALID;
                }
                break;

            case INVALID:
                return 0;
        }
    }

    return state == DIGIT;
}

int main() {
    const char* tests[] = {"123", "-456", "+78", "--12", "12a", "+", ""};
    int n = 7;

    for (int i = 0; i < n; i++) {
        printf("\"%s\" -> %s\n", tests[i],
               is_valid_integer(tests[i]) ? "VALID" : "INVALID");
    }

    return 0;
}
Download .c
Output:
"123" -> VALID
"-456" -> VALID
"+78" -> VALID
"--12" -> INVALID
"12a" -> INVALID
"+" -> INVALID
"" -> INVALID