Untitled
user_4805151
plain_text
3 years ago
3.5 kB
18
Indexable
include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX_TEXT_LENGTH 1000
void viewText(const char* text)
{
printf("Current Text: %s\n", text);
}
int main()
{
char text[MAX_TEXT_LENGTH] = "";
int cursorPosition = 0;
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, P: Replace, L: Move cursor left, R: Move cursor right, 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 == 'P')
{
char replaceText[MAX_TEXT_LENGTH];
printf("Enter text to replace: ");
scanf(" %[^\n]s", replaceText);
// Check if there is enough space in the text to replace the old text with the new text
if (strlen(text) - cursorPosition >= strlen(replaceText))
{
// Replace the old text with the new text
strncpy(text + cursorPosition, replaceText, strlen(replaceText));
cursorPosition += strlen(replaceText);
}
else
{
printf("Not enough space to replace the text.\n");
}
}
else if (command == 'L')
{
if (cursorPosition > 0)
{
cursorPosition--;
}
}
else if (command == 'R')
{
if (cursorPosition < strlen(text))
{
cursorPosition++;
}
}
else
{
printf("Invalid command. Try again.\n");
}
}
return 0;
}Editor is loading...