Thread: Padding string with string

  1. #1
    Registered User
    Join Date
    Feb 2014
    Location
    NY
    Posts
    56

    Padding string with string

    I'm trying to fill a string with the contents of the string for the size of another string. Problem is it will only copy the first element, how can I make it copy the whole string?

    Code:
    char a = "string A. Hello. World!";
    char b = "help";
    1.
    Code:
    memset(b, *b, strlen(a));
    puts(b);
    2.
    Code:
    for(i=0;i<strlen(a)-1;i++){
                    memset(b, b[i], strlen(a));
                    if(i==strlen(b)-1 && strlen(a)!=strlen(b)) i=0;
                    } 
    
    puts(b);
    3. Also tried this.
    Code:
    char bNew[256]=" ";          
    int j=0;
    for(i=0;i<strlen(a);i++){
          bNew[i]=b[j];
          if(j==strlen(b)-1) j=0;
          if(i==strlen(a))bNew[i]='\0';
          }
    puts(b);
    }
    Output 1&2:
    Code:
    hhhhhhhhhhhhhhhhhh
    Output 3:
    Code:
    help
    Desired output is:
    Code:
    helphelphelphelphelph

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Have you considered using strcat() or (if you remember the need to add a character with value zero as terminator) memcpy() in a loop?
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Here is the strcat function from K&R, for reference. Or you can use strcat from the string.h header. It's problem is that it assumes the copied to string (s in the code below) is big enough to contain itself and the thing it's copying. You might be able to fix that with strlen though. Here:

    Code:
    void strcat(char s[], char t[])   
    {       
    
        int i, j;
        i = j = 0;      
    
        while (s[i] != '\0')           
            i++;       
    
        while ((s[i++] = t[j++]) != '\0');  
    }
    Edit: Also if you want the strings to truly look concatenated, don't forget about newlines. It's not practical to replace the newlines with '\0' on the string being copied into, you might try spaces though. Good luck.
    Last edited by Alpo; 04-20-2014 at 08:16 AM.

  4. #4
    Registered User
    Join Date
    Feb 2014
    Location
    NY
    Posts
    56
    I tried strcat but it doesn't work. It had the same output as 3.

    Can someone explain why this does not work?
    Code:
    j=0;
    for(i=0;i<strlen(a);i++){
            bNew[i]=b[j];
            j++;
            if(j==strlen(decode)-1) j=0;    
    }
    Values of i and j during for loop show that it should work.
    Code:
    i= 0    j= 0
    i= 1    j= 1
    i= 2    j= 2
    i= 3    j= 3
    i= 4    j= 4
    i= 5    j= 5
    i= 6    j= 6
    i= 7    j= 7
    i= 8    j= 8
    i= 9    j= 9
    i= 10   j= 10
    i= 11   j= 11
    i= 12   j= 12
    i= 13   j= 13
    i= 14   j= 14
    i= 15   j= 15
    i= 16   j= 16
    i= 17   j= 0
    i= 18   j= 1
    i= 19   j= 2
    i= 20   j= 3
    i= 21   j= 4
    i= 22   j= 5
    i= 23   j= 6
    i= 24   j= 7
    i= 25   j= 8
    i= 26   j= 9
    i= 27   j= 10
    i= 28   j= 11
    i= 29   j= 12
    i= 30   j= 13
    i= 31   j= 14
    i= 32   j= 15
    i= 33   j= 16
    i= 34   j= 0
    i= 35   j= 1
    i= 36   j= 2
    i= 37   j= 3
    Last edited by 3DT; 04-20-2014 at 01:11 PM.

  5. #5
    Registered User
    Join Date
    Feb 2014
    Location
    NY
    Posts
    56
    Restarted my system and its working with the last code I posted!!

  6. #6
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    It doesn't look like you are copying string a.

    Edit: Nevermind, just looked at the desired output again. I thought you were trying to copy both strings into bnew[].

    Out of curiosity, what was the maximum value of I?
    Last edited by Alpo; 04-20-2014 at 01:43 PM.

  7. #7
    Registered User
    Join Date
    Feb 2014
    Location
    NY
    Posts
    56
    In my actual program I have:

    Code:
    char encode[256]="\0";
    char decode[256]="\0";
    char encrypt[256]="\0";
    char encryptPad[256]="\0";
    So the max value of i could be less than or equal to 255.


    The output I posted was for a random phrase. Heres output of i,j for provided test data.

    Code:
    i= 0    j= 0
    i= 1    j= 1
    i= 2    j= 2
    i= 3    j= 0
    i= 4    j= 1
    i= 5    j= 2
    i= 6    j= 0
    i= 7    j= 1
    i= 8    j= 2
    i= 9    j= 0
    i= 10   j= 1
    i= 11   j= 2
    i= 12   j= 0
    i= 13   j= 1
    i= 14   j= 2
    i= 15   j= 0
    i= 16   j= 1
    i= 17   j= 2
    i= 18   j= 0
    i= 19   j= 1
    i= 20   j= 2
    i= 21   j= 0
    Last edited by 3DT; 04-20-2014 at 03:47 PM.

  8. #8
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Thanks for replying, I wasn't sure if <strlen of an array of 255 would be 255, or 254.

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Alpo View Post
    Thanks for replying, I wasn't sure if <strlen of an array of 255 would be 255, or 254.
    It's neither. strlen() computes the number of non-zero characters that precedes the first character with value zero. The zero is referred to as a sentinel (a marker indicating something - in this case "end of string").
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  10. #10
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    That makes sense, I guess it wouldn't be much use as a function if it was just another way to write the array size. I remembered reading that it doesn't count the null bit, if it gets that far. That's why I was curious what the variable could go up to written like that, but with a full array. I'll check it out on my compiler. Thanks.

  11. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    No, strlen() does not count the zero character (but it always gets that far, since it counts until it finds a zero character).

    If the array has no elements with value zero, then strlen() exhibits undefined behaviour (anything can happen). In practice, it'll often keep searching through a random area of memory (unrelated to your array, but happens to be located in memory after the array) until it happens to find a character with value zero. That means it can give a much larger value than the length of your array. If the host system detects your program reading memory it shouldn't, then your program will be terminated with prejudice. In worst case, if there is no character with value zero in memory and the host system doesn't detect your program running rampant, eventually a size_t will overflow and be reset to zero (since size_t is an unsigned integral type), so counting will resume - that means an infinite loop.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  12. #12
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    I is 253 with the array all full (254 counting 0). I've gotten lucky to have my program crash whenever I've gone off the deep end of an array before. I read it can screw the memory of nearby programs if your computer doesn't detect it, and you are writing to it.

    Code:
     main()
     #include <stdio.h>
     #include<string.h>
    
     #define ARRSIZE 255
    
     main()
     {
         int i;
         char arr[ARRSIZE];
         memset(arr,'A',ARRSIZE-1);
    
         for(i=0; i < strlen(arr); ++i){
            printf("%d\n", i);
         }
         printf("%s", arr);
     }
    Last edited by Alpo; 04-20-2014 at 08:36 PM.

  13. #13
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Your compiler happens to have initialised arr with zero bytes, so your strlen() call (and the printf() call) work. That is not guaranteed. You just got lucky .... with your compiler.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  14. #14
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    I thought memset initialized the array? Do I need to do it differently?

    I got how to use it from here: C 'memset' Examples | Lainoox . There was another site that looked better, but it was giving a certificate warning.
    Last edited by Alpo; 04-20-2014 at 10:36 PM.

  15. #15
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Alpo View Post
    I thought memset initialized the array? Do I need to do it differently?
    memset() only changes what it is told to change. In your example, the array was of length ARRSIZE, and memset() was told to set the values of ARRSIZE-1 characters. The last character remains uninitialised.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 22
    Last Post: 07-28-2011, 01:26 PM
  2. string padding
    By TIMBERings in forum C++ Programming
    Replies: 1
    Last Post: 04-22-2010, 03:42 PM
  3. string padding and replacement functions
    By George2 in forum Tech Board
    Replies: 4
    Last Post: 11-19-2006, 01:40 AM
  4. Replies: 1
    Last Post: 10-31-2005, 11:36 AM