-2

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.

Zach Goodman
  • 45
  • 1
  • 7

3 Answers3

3

The variable length is used to limit iterations of both your loops but is never assigned a value. This is undefined behaviour and a bus error is definitely a possible outcome of it.

Did you intend to use len for this purpose?

EDIT: strlen already does not include the null terminator in its return value. strlen(buf) - 1 is probably not what you want.

Govind Parmar
  • 20,656
  • 7
  • 53
  • 85
1

You declare length without initializing it.

Then you declare len, with initialization.

Then you use the uninitialized length, which leads to undefined behavior.

Turn up your warning flags when compiling (consult your compiler docs for those) and don't declare variables so far from the point you actually need them.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
  • Thanks for the tip about declaring variables closer to when they're needed, hadn't ever thought of that. Thanks for your help! – Zach Goodman Feb 14 '19 at 04:27
0

The scope of len should be through 'main()'.

So, remove declaration of length and instead declare len there. Here is the corrected code.

#include <stdio.h>
#include <string.h>
#define MAX_BUF 1024

int main ()
{
        char buf[MAX_BUF];
        int len;

        do {
                fgets(buf, MAX_BUF, stdin);
                len = strlen(buf) - 1;
                printf("%d",len);
                char buf2[MAX_BUF];
                strcpy(buf2, buf);
                int i;
                for(i = 0; i < len; 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';
                        }
                }
                printf("%s", buf2);
        } while (len > 1);
}
M. Knight
  • 89
  • 5