John
John

Reputation: 3

padding string by adding char to the string

I am trying to create a padding function that adds an underscore to a string. The string length should be 16, and if the string is less than 16 the function should add an underscore until the string length is 16, then return the padding string. If the string is more than 16, the padding function should ignore the characters and returned the first 16 characters of the string.

char* padding(char* plaintext) {
    char ch = '_';
    size_t len = strlen(plaintext);
    size_t lench = 17 - len;
    char *p_text = malloc(len + 1 + 1);
    strcpy(p_text, plaintext);
    
    if (len < 17) {
        int i;
           
        for (i = lench; i < 16; i++) {
            p_text[i] = ch;
        }
    }
        
    // p_text[17] = '\0';
    return p_text;
}
     
int main() {
    char *key = "0123456789ABCDEF"; 
       
    while (1) {
        char plaintext[WIDTH + 1];
            
        printf("Enter a string: ");
        fgets(plaintext, WIDTH + 1, stdin);
           
        if (plaintext[0] == '\n' || plaintext[0] == '\r') 
            break;
            
        char* padded_plaintext = padding(plaintext);
        printf("padded plaintext = %s\n", padded_plaintext);
          
        printf("\n");
            
    }
    
    return 0;
}

this code returns a weird result.

Upvotes: 0

Views: 607

Answers (2)

Chris
Chris

Reputation: 36496

Consider a clean solution to this problem. Hopefully seeing this (and the accompanying explanation) helps.

char *padded_string(char *src, int width, char ch) {
    char *dest = calloc(1, width + 1);

    strncpy(dest, src, width);

    for (int i = 0; i < width; i++) {
        if (!dest[i]) {
            dest[i] = ch;
        }
    }

    return dest;
}

We provide ourselves a clean slate to work on by allocating width + 1 bytes using calloc. Using calloc will ensure all bytes are set to 0. When working with strings in C, they need to be null-terminated, so this is very useful.

We copy the contents of src into dest. Using strncpy ensures we don't get a buffer overflow if the source string is longer than the string we want to end up with.

Next we loop from 0, width times. If the character in the destination string at i is a '\0' we'll insert the padding character at that index.

Now, because we used calloc and we allocated an extra byte beyond the character length we needed, the string is already null-terminated, we can simply return its pointer.

Upvotes: 2

William Pursell
William Pursell

Reputation: 212228

The space that you allocate for p_text should not depend on the input. It should always be 17. If len + 1 + 1 < 16, then accessing p_text[i] will lead to undefined behavior for certain values of i that you are using. You should replace:

char *p_text = malloc(len + 1 + 1);

with

char *p_text = malloc(17);

and check that p_text is not NULL before you write to it.

Also, the commented out //p_text[17] = '\0'; is wrong. That should be

p_text[16] = '\0';

Upvotes: 2

Related Questions