I'm getting a bus error when attempting to run this code. The goal of it is to replace certain letters with numbers, E's with 3's, O's with 0's and so on.
#include <stdio.h>
#include <string.h>
#define MAX_BUF 1024
int main () {
char buf[MAX_BUF];
int length;
// other stuff
do {
// read a line
fgets(buf, MAX_BUF, stdin);
// calculate its length
int len = strlen(buf) - 1;
// modify the line by switching characters
char buf2[MAX_BUF];
strcpy(buf2, buf);
int i;
for(i = 0; i < length; i++){
if (buf2[i] == 'E' || buf2[i] == 'e'){
buf2[i] = '3';
}
if (buf2[i] == 'I' || buf2[i] == 'i'){
buf2[i] = '1';
}
if (buf2[i] == 'O' || buf2[i] == 'o'){
buf2[i] = '0';
}
if (buf2[i] == 'S' || buf2[i] == 's'){
buf2[i] = '5';
}
}
// print the modified line
printf("%s", buf2);
} while (length > 1);
}
Expected output from input of
"The quick brown fox jumps over the lazy dog." is
"Th3 qu1ck br0wn f0x jump5 0v3r th3 lazy d0g."
But again, the program throws "Bus error" which I don't understand and don't know how to spot.