character stuffing
Mon Nov 04 2024 20:28:16 GMT+0000 (Coordinated Universal Time)
Saved by
@sem
#include <stdio.h>
#include <string.h>
void main() {
int i = 0, j = 0, n, pos;
char a[20], b[50], ch;
printf("Enter string\n");
scanf("%s", a);
n = strlen(a);
printf("Enter position\n");
scanf("%d", &pos);
while (pos > n) {
printf("Invalid position, Enter again: ");
scanf("%d", &pos);
}
printf("Enter the character\n");
getchar();
ch = getchar();
// Initialize the stuffed string with "dlestx"
b[0] = 'd';
b[1] = 'l';
b[2] = 'e';
b[3] = 's';
b[4] = 't';
b[5] = 'x';
j = 6;
while (i < n) {
// Insert "dle" + character + "dle" at the specified position
if (i == pos - 1) {
b[j] = 'd';
b[j + 1] = 'l';
b[j + 2] = 'e';
b[j + 3] = ch;
b[j + 4] = 'd';
b[j + 5] = 'l';
b[j + 6] = 'e';
j = j + 7;
}
if (a[i] == 'd' && a[i + 1] == 'l' && a[i + 2] == 'e') {
b[j] = 'd';
b[j + 1] = 'l';
b[j + 2] = 'e';
b[j + 3] = 'd';
b[j + 4] = 'l';
b[j + 5] = 'e';
j = j + 6;
i = i + 3;
} else {
b[j] = a[i];
j++;
i++;
}
}
b[j] = 'd';
b[j + 1] = 'l';
b[j + 2] = 'e';
b[j + 3] = 'e';
b[j + 4] = 't';
b[j + 5] = 'x';
b[j + 6] = '\0';
printf("Stuffed string is:\n%s\n", b);
}
content_copyCOPY
Comments