Results 1 to 11 of 11

Thread: Making a string a certain length

  1. #1

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772

    Making a string a certain length

    How can I make a string a certain length by filling the remaining spaces with a certain character? For example, if I wanted each string to have 7 characters, "hello" becomes "__hello", "123" becomes "____123", etc.
    Alcohol & calculus don't mix.
    Never drink & derive.

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    PHP Code:
    #include <iostream>
    using namespace std;

    int main()
    {
        
    char a[6]="hello";
        
    char b[8];
        
    charm=b+sizeof(b)-sizeof(a);
        for (
    chari=b;i<m;*i++='_');
        for (
    charj=a;i<b+sizeof(b);*i++=*j++);
        
    cout << << endl;

                    return 
    0;

    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  3. #3

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    The reason I was asking is because I an trying to write a program that converts text to binary and if i get "101010" i need to add the other zero's on. I am generating the string i want to add to in a loop, so is it possible to do it that way?
    Alcohol & calculus don't mix.
    Never drink & derive.

  4. #4
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Yes but this also puts it in another light, you can do it more efficiently with some bitshifts and bitwise and's
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  5. #5
    jim mcnamara
    Guest
    This is from memory - we have a bunch of these - lpad, rpad,
    zf (zero-fill), etc.

    PHP Code:
    #include <string.h>
    void zf(char *tint i);
    void lpad(char *tint ichar *padchar));

    /* lpad - add spaces on left side of char */
    /* standard ansi c */
    /* you can modify this to pad with any char */

    void lpad(char *tint ichar *padchar) {
          
    char tmp[8192];
          
    int j;
          if (
    strlen(t) > || strlen(t) == i) return;
          while (
    strlen(t) < i){
                   
    strcpy(tmp,padchar);
        
    strcat(tmp,t);
        
    strcpy(t,tmp);
          }
          
    }

    void zf(char *tint i){
          
    lpad(t,i);


  6. #6
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Originally posted by jim mcnamara
    PHP Code:
          char tmp[8192]; 
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  7. #7
    jim mcnamara
    Guest
    Some code sends down special strings that are kinda long.
    We have to make sure it always works.

    I know that allocating a lot of memory is supposed to be harder on the OS, but in Windows, any memory allocation request up to 8192 bytes uses the same amount of os overhead, because of the way P1 space has the memory masthead list.

    It's a complete waste of memory for what Wynd wants. It isn't for us.

  8. #8

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    Did you write those?
    Alcohol & calculus don't mix.
    Never drink & derive.

  9. #9
    jim mcnamara
    Guest
    Unfortunately.

    We have a big lib of functions for strings, barcodes and so on.
    They have to conform to some wierd criteria, like running on several platforms, and supporting some bizrre recordtypes, hence the ansi c compliance.

  10. #10
    Hyperactive Member MPrestonf12's Avatar
    Join Date
    Jun 1999
    Location
    NY
    Posts
    330
    you could use fill().

    Code:
    #include <iostream.h>
    int main()
    {
    using namespace std;
    
    cout.width(10);
    
    
    cout.fill('*');
    
    cout<<"Hi";
    return 0;
    }
    Matt

  11. #11

    Thread Starter
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    Thanks, that function works
    Alcohol & calculus don't mix.
    Never drink & derive.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width