简体   繁体   中英

C program prints garbage characters

I have an assignment to write a program in C that functions similarly to the bash sed 's/oldstring/newstring/g' but only using stdio.h and string.h. We cannot use malloc s we have not yet covered it in class. The program has to continue to take user input until the user enters ^D. We're using GCC so I have it set up to use variable length arrays and I've managed to get the program to find and replace a single instance of oldstring in the user input. However, on occasion the program will output some garbage characters and I am not sure why. I assume it is a memory allocation error or the program is reading past where I want it to read. The code is below:

#include <stdio.h>
#include <string.h>
int isMatch(char * os, char * us){
    int i;
    char temp[strlen(os)];
    for(i=0; i<strlen(us); i++){
        int k=0;
        for(int j=i; j<i+strlen(os); j++){
            temp[k]=us[j];
            k++;
        }
        if(strcmp(temp, os)==0){
        return 1;
        }
        else{
            return 0;
        }
    }
}

void replace(char * os, char * us, char * ns, int loc){
    char out[strlen(us) - (strlen(os) - strlen(ns))];
    int i;
    for(i=0; i<loc; i++){
        out[i]=us[i];
    }
    int k=0;
    for(i=loc; i<loc+strlen(ns); i++){
        out[i]=ns[k];
        k++;
    }
    k=0;
    for(i=loc+strlen(ns); i<strlen(us)-(strlen(os)-strlen(ns)); i++){
        out[i]=us[loc+strlen(os)+k];
        k++;
    }
    printf("%s\n", out);
}

int main(int argc, char * argv[]){
    char input[100];
    int i;
    char c;
    int match;
    while(1){
        if(scanf("%c", &c)==EOF){
            break;
        }
        if((input[0]=c) != '\n'){
            for(i=1; i<100; i++){
                scanf("%c", &input[i]);
                if(input[i]=='\n'){
                    break;
                }
            }
        }
        for(i=0; i<100; i++){
            match = isMatch(argv[1], &input[i]);
            if(match == 1){
                replace(argv[1], input, argv[2], i);
            }
            if(input[i]=='\n'){
                break;
            }
        }
    }
}

I call the program with ./a.out aa b for example.

I then enter helaalo and the program spits out helblo which is correct. I then enter libraary and the program outputs librbry followed by some random characters on new lines. I then enter caar and the program outputs cbr followed by even more random letters on new lines. A screenshot of this behavior is included. 在这里你可以看到我尝试了 helaalo 两次,它运行良好,但随后图书馆得到了这个垃圾输出,caar 也是如此,尽管它似乎已经很好地替换了字符。

The problem is coming from your the way you are filling your input buffer. You don't add a \\0 at the end of the string.

There is also an other "problem", when you declare you array out if you want a dynamic size you need to use a malloc. how you are declaring it will not have a dynamic size , the size of the array will be calculated at compilation time. Just to keep it in mind.

Since you can't use malloc, you can force to add terminating \\0 for the end of input string and end of out string,

// input
for(i=1; i<100; i++){
    scanf("%c", &input[i]);
    if(input[i]=='\n'){
        input[i] = '\0'; // add terminating \0 for input
        break;
    }
}
        
  
// out
for(i=loc+strlen(ns); i<strlen(us)-(strlen(os)-strlen(ns)); i++){
    out[i]=us[loc+strlen(os)+k];
    k++;
}
int len = strlen(us)-(strlen(os)-strlen(ns));
out[len] = '\0'; // for out termiante \0
printf("%s\n", out);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM