Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
4.7 kB
5
Indexable
Never
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_TEXT_LENGTH 1000

void viewText(const char* text) {
    printf("Current Text: %s\n", text);
}

void editText(char* text) {
    int cursorPosition = strlen(text); // Initialize cursor position at the end of the text
    char input[MAX_TEXT_LENGTH];

    while (1) {
        printf("Text: %s\n", text);
        printf("Cursor Position: %d\n", cursorPosition);
        printf("Enter command (I: Insert, D: Delete, M: Move cursor, E: Exit): ");
        scanf(" %s", input);

        // Convert command to uppercase for easier comparison
        char command = toupper(input[0]);

        if (command == 'E') {
            break;
        } else if (command == 'I') {
            char insertText[MAX_TEXT_LENGTH];
            printf("Enter text to insert: ");
            scanf(" %[^\n]s", insertText);

            // Check if there is enough space in the text to insert the new text
            if (strlen(text) + strlen(insertText) < MAX_TEXT_LENGTH) {
                // Shift the existing text to the right to make space for the new text
                memmove(text + cursorPosition + strlen(insertText), text + cursorPosition, strlen(text + cursorPosition) + 1);
                // Insert the new text at the cursor position
                strncpy(text + cursorPosition, insertText, strlen(insertText));
                cursorPosition += strlen(insertText);
            } else {
                printf("Not enough space to insert the text.\n");
            }
        } else if (command == 'D') {
            int numDelete;
            printf("Enter number of characters to delete: ");
            scanf("%d", &numDelete);

            // Check if there are enough characters in the text to delete
            if (strlen(text) >= numDelete) {
                // Shift the remaining text to the left to overwrite the deleted characters
                memmove(text + cursorPosition, text + cursorPosition + numDelete, strlen(text + cursorPosition + numDelete) + 1);
            } else {
                printf("Not enough characters to delete.\n");
            }
        } else if (command == 'M') {
            int newPosition;
            printf("Enter new cursor position: ");
            scanf("%d", &newPosition);

            // Check if the new cursor position is within the valid range
            if (newPosition >= 0 && newPosition <= strlen(text)) {
                cursorPosition = newPosition;
            } else {
                printf("Invalid cursor position.\n");
            }
        } else {
            printf("Invalid command. Try again.\n");
        }
    }
}

void searchText(const char* text) {
    char searchString[MAX_TEXT_LENGTH];
    printf("Enter text to search: ");
    scanf(" %[^\n]s", searchString);

    const char* result = strstr(text, searchString);
    if (result != NULL) {
        printf("'%s' found at position %ld\n", searchString, result - text);
    } else {
        printf("'%s' not found in the text\n", searchString);
    }
}

void replaceText(char* text) {
    char searchString[MAX_TEXT_LENGTH];
    printf("Enter text to replace: ");
    scanf(" %[^\n]s", searchString);

    char replacementString[MAX_TEXT_LENGTH];
    printf("Enter replacement text: ");
    scanf(" %[^\n]s", replacementString);

    char* result = strstr(text, searchString);
    if (result != NULL) {
        int position = result - text;
        memmove(result + strlen(replacementString), result + strlen(searchString), strlen(result + strlen(searchString)) + 1);
        strncpy(result, replacementString, strlen(replacementString));
        printf("'%s' replaced with '%s' at position %d\n", searchString, replacementString, position);
    } else
    {printf("'%s' not found in the text\n", searchString);}
}

int main() {
char text[MAX_TEXT_LENGTH] = "";
int choice;
while (1) {
    printf("\n--- MENU ---\n");
    printf("1) View Text\n");
    printf("2) Edit Text\n");
    printf("3) Search Text\n");
    printf("4) Replace Text\n");
    printf("5) Exit\n");
    printf("Enter your choice: ");
    scanf("%d", &choice);

    switch (choice) {
        case 1:
            viewText(text);
            break;
        case 2:
            editText(text);
            break;
        case 3:
            searchText(text);
            break;
        case 4:
            replaceText(text);
            break;
        case 5:
            printf("Exiting...\n");
            return 0;
        default:
            printf("Invalid choice. Try again.\n");
    }
}

return 0;
}