• C

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;
lhgarrettAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Powerful Yet Easy-to-Use Network Monitoring

Identify excessive bandwidth utilization or unexpected application traffic with SolarWinds Bandwidth Analyzer Pack.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.