about a function that pads string

This is a discussion on about a function that pads string within the C Programming forums, part of the General Programming Boards category; Hi everyone, I am working on a function that pads atring from the back, it works well but it doesn't ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    28

    about a function that pads string

    Hi everyone,

    I am working on a function that pads atring from the back,
    it works well but it doesn't produce the right result. that means
    I designed the function so that when given a string like "Hello" which is 5 character long, the program should produce "Helloxxx" which should be 8 character long. I am somehow getting wrong something ? can anyone help me figure out why itdoes so ?




    Code:
    code
    
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    #include <stdio.h> 
    
    
    
    char *padstr(char *s, char c, int field, int pos);
    
    int main( )
    {
        char str[] = "Hello" ;
        //char c = 'x';
        printf("the paded string is %s\n", padstr(str, 'x', 7, 1));
        
    } 
    
    
    /* field = length of resulting string
     *  pos   = 1 - left padded-- means desierd result is "Helloxxx " 
     *  s     = string to be formatted 
      * c     = padding character 
    */
    char *padstr(char *s, char c, int field, int pos) 
    {
    
        int i;
        char tmpstr[9] = " ";
       
        /* we won't accept wrong entry */ 
        if (strlen(s) > field) 
        { 
            return 0;
        }
    
        /* clear the variable content first */
        strcpy(tmpstr,"\0");    		
    
        /* we want it right-padded */
        if (pos == 1)
        { 
          for (i =0; i < (field - strlen(s)); i++)
          {          /* start padding the string at the back */
             strcat(tmpstr, &c);
          }          
            
            strcat(s,tmpstr);
        }
       return s;
    
    }

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Your program produces "Helloxx" which is good because you want the string to be 7 characters long (third argument of the padstr function). If you change this argument to 8 you should get "Helloxxx".

    You still have some things to fix:

    - Your variabele str has only space for 6 characters (5 plus null char). So you can't just add some characters to it. Make sure you have enough space in your buffer:
    Code:
    char str[20];
    strcpy(str, "Hello");
    - In stead of using strcpy(tmpstr,"\0"); you can also use: tmpstr[0] = '\0';

    - Your main function should return a value.

  3. #3
    *
    Guest
    Abdi,

    Monster makes a good point-- You need to be very careful about poking data into RAM in places you don't own.

    When you declared and allocated the variable 'str[]', you told the compiler to determine how much RAM needed to be allocated, based on the value you were initializing the variable's RAM to ("Hello"). The compiler will allocate exactly 5 bytes to hold this value.

    Now, when you go and poke a new letter, like 'x', into the 6th and the 7th locations, you are actually writing into RAM that you don't own, or that another part of your program may be relying on. In which case, you just trashed other data.

    Don't do this.

    Most developers today have a real hard time understanding RAM, and that's why most software out there is unstable, leaky, buggy, and slow.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    2
    Or you can also do this:

    char *padstr(char *s, char c, int field, int pos)
    {
    int i;
    char *tmpstr=s;

    /* we won't accept wrong entry */
    if (strlen(s) > field)
    {
    return 0;
    } /* clear the variable content first */

    if( pos==1)//left padding
    {
    for(i=strlen(tmpstr);i<field;i++)
    *(tmpstr+i)=c;
    }

    *(tmpstr+i)='\0';

    return tmpstr;

    }

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by hobbes
    Or you can also do this:

    char *padstr(char *s, char c, int field, int pos)
    {
    int i;
    char *tmpstr=s;

    /* we won't accept wrong entry */
    if (strlen(s) > field)
    {
    return 0;
    } /* clear the variable content first */

    if( pos==1)//left padding
    {
    for(i=strlen(tmpstr);i<field;i++)
    *(tmpstr+i)=c;
    }

    *(tmpstr+i)='\0';

    return tmpstr;

    }
    This code has a bug which causes a buffer overflow. The line
    >*(tmpstr + i) = '\0';
    puts the \0 one past the end of the array, not as the last element. You need to do this:
    >*(tmpstr + i - 1) = '\0';

    Also, what is this about:
    Code:
    if (strlen(s) > field)
    {
    	return(0);
    }
    Why are you returning zero, when you should be returning a pointer to the string?

    You can write it a little more efficiently using pointers:
    Code:
    char *padstr(char *s, char c, int field, int pos)
    {
    	char	*tmpstr = s + strlen(s); /* Pointer to NULL terminator */
    	char	*endptr = s + field - 1; /* Pointer to Last byte of the array */
    
    	if (pos == 1)	/* left padding ?? */
    	{
    		for ( ; tmpstr < endptr; tmpstr++)
    		{
    			*tmpstr = c;
    		}
    	}
    
    	*tmpstr = '\0';
    
    	return(s);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 03:45 PM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 10:19 AM
  3. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 04:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21