• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 355
  • Last Modified:

pad with blanks (x20)

What is an easy way to concatenate blanks to a character string to pad the string to a certain length (length=10)? I can't have any null characters.

The following ends up with a null character in the middle. For example I want "my name" to be stored as "my name   "

Example:

  save five 32 bit words as:

      x796d   y         m
      x6e20   n         space
      x6d61   m        a
      x0065   space     e
      x2020   space  space
      x2020   space  space

  not:

      x796d   y         m
      x6e20   n         space
      x6d61   m        a
      x0065   null      e
      x2020   space  space
      x2020   space  space

void write_10char (unsigned short *start_location, char *characters)
{
   char *mask = "          ";   // 10 blanks

   // printf("\ninput string length: \t%d\n", strlen(characters));
   // printf("input string:\t\t%s\n", (char*)characters);

   strcpy((char*)start_location, mask);
   // printf("words masked:\t\t%s\n", (char*)start_location);

   strcpy((char*)start_location, characters);
   // printf("words final:\t\t%s\n", (char*)start_location);

   return;
0
lhgarrett
Asked:
lhgarrett
  • 2
  • 2
  • 2
1 Solution
 
mcglincheyCommented:

// Will take a string in pcStart, and
// will return the same string, padded
// to 10 chars, or trucated to 10, in
// szOutPaddedString.  szOutPaddedString
// must have at least 10 bytes allocated.
void write_10char(unsigned short * psStart, char * szOutPaddedString) {

 int iIndex;
 int bFoundEnd;

 bFoundEnd = 0;

 for (iIndex=0; iIndex<10; iIndex++) {
   if ((char *)psStart[iIndex] == '\0') {
      bFoundEnd = 1;
   }

   if (bFoundEnd) {
     szOutPaddedString[iIndex] = ' ';
   } else {
     szOutPaddedString[iIndex] = (char *)psStart[iIndex];
   }  
}
0
 
ozoCommented:
printf("input string: %-10s\n", (char*)characters);
0
 
mcglincheyCommented:
Ozo's solution is much better than mine.

Simply

sprintf(characters, "%-10s", (char*)(startLocation))  is much more elegant.

It won't truncate if input is greater than 10, is the only potential problem.
0
Firewall Management 201 with Professor Wool

In this whiteboard video, Professor Wool highlights the challenges, benefits and trade-offs of utilizing zero-touch automation for security policy change management. Watch and Learn!

 
lhgarrettAuthor Commented:
> // Will take a string in pcStart, and

This may may work, but I think it's a bit overkill.
0
 
lhgarrettAuthor Commented:
> printf("input string: %-10s\n", (char*)characters);

This works great! The function I have now is:

void write_10char(unsigned short *word_element, char *characters)
{
   char padded_chars[10];

   sprintf(padded_chars, "%-10s", (char*)characters);
 
   strcpy((char*)word_element, padded_chars);

   return;
}

0
 
ozoCommented:
sprintf(word_element,"%-10.10s",characters);
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now