String Padding in C

前端 未结 10 1496
迷失自我
迷失自我 2020-11-28 22:25

I wrote this function that\'s supposed to do StringPadRight(\"Hello\", 10, \"0\") -> \"Hello00000\".

char *StringPadRight(char *string, int padded_len, char          


        
相关标签:
10条回答
  • 2020-11-28 22:45
    #include <iostream>
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    using namespace std;
    
    int main() {
        // your code goes here
        int pi_length=11; //Total length 
        char *str1;
        const char *padding="0000000000000000000000000000000000000000";
        const char *myString="Monkey";
    
        int padLen = pi_length - strlen(myString); //length of padding to apply
    
        if(padLen < 0) padLen = 0;   
    
        str1= (char *)malloc(100*sizeof(char));
    
        sprintf(str1,"%*.*s%s", padLen, padLen, padding, myString);
    
        printf("%s --> %d \n",str1,strlen(str1));
    
        return 0;
    }
    
    0 讨论(0)
  • 2020-11-28 22:45

    One thing that's definitely wrong in the function which forms the original question in this thread, which I haven't seen anyone mention, is that it is concatenating extra characters onto the end of the string literal that has been passed in as a parameter. This will give unpredictable results. In the example call of the function, the string literal "Hello" will be hard-coded into the program, so presumably concatenating onto the end of it will dangerously write over code. If you want to return a string which is bigger than the original then you need to make sure you allocate it dynamically and then delete it in the calling code when you're done.

    0 讨论(0)
  • 2020-11-28 22:52

    Oh okay, makes sense. So I did this:

        char foo[10] = "hello";
        char padded[16];
        strcpy(padded, foo);
        printf("%s", StringPadRight(padded, 15, " "));
    

    Thanks!

    0 讨论(0)
  • 2020-11-28 22:53

    The argument you passed "Hello" is on the constant data area. Unless you've allocated enough memory to char * string, it's overrunning to other variables.

    char buffer[1024];
    memset(buffer, 0, sizeof(buffer));
    strncpy(buffer, "Hello", sizeof(buffer));
    StringPadRight(buffer, 10, "0");
    

    Edit: Corrected from stack to constant data area.

    0 讨论(0)
  • 2020-11-28 22:56
    #include <stdio.h>
    #include <string.h>
    
    int main(void) {
        char buf[BUFSIZ] = { 0 };
        char str[] = "Hello";
        char fill = '#';
        int width = 20; /* or whatever you need but less than BUFSIZ ;) */
    
        printf("%s%s\n", (char*)memset(buf, fill, width - strlen(str)), str);
    
        return 0;
    }
    

    Output:

    $ gcc -Wall -ansi -pedantic padding.c
    $ ./a.out 
    ###############Hello
    
    0 讨论(0)
  • 2020-11-28 22:58

    The function itself looks fine to me. The problem could be that you aren't allocating enough space for your string to pad that many characters onto it. You could avoid this problem in the future by passing a size_of_string argument to the function and make sure you don't pad the string when the length is about to be greater than the size.

    0 讨论(0)
提交回复
热议问题