jeudi 6 juillet 2017

Eliminating IF statement from this code

I have this C function which basically takes the input from the user and saves it in a static char[]. Then it checks whether it is empty. If it is empty then it prints an error, else appends a file extension to it. Here's the piece of code I'm concerned about:

const char* text_entry(){
    //create static char[] called input, and accept a value from user
    if(strlen(input)>0){
        strncat(input, ".txt",4);
        return input;
    }
    printf("Error! no text entered!");
    return NULL;
}

My concern is that I want to remove the IF statement from it, making it more DRY. How can I do that?

Aucun commentaire:

Enregistrer un commentaire