Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8681287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:28:08+00:00 2026-06-12T21:28:08+00:00

Let’s say I have a struct called test : struct test { char name[16];

  • 0

Let’s say I have a struct called test:

struct  test
{
    char name[16];

} test;

I want to read the user’s input and put it in the name field. Let’s say the user has typed in "hello" as the input. My code is like this:

struct test test1;

strcpy(test1.name, user_input);

Now the name has 5 characters in it ("hello"), but I want it to have its 16 characters: 5 for the actual input, and the rest white spaces. How can I achieve that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T21:28:09+00:00Added an answer on June 12, 2026 at 9:28 pm

    sprintf() can do it:

    sprintf(test1.name,"%-15s","John Doe");
    printf("[%s] length of test1.name: %ld\n",test1.name,strlen(test1.name));
    sprintf(test1.name,"%-*s",(int) sizeof(test1.name) - 1,"Jane Jones");
    printf("[%s] length of test1.name: %ld\n",test1.name,strlen(test1.name))
    

    output:

    [John Doe       ] length of test1.name: 15
    [Jane Jones     ] length of test1.name: 15
    

    or

    #include <stdio.h>
    #include <string.h>
    
    int copy_with_pad(char *destination,const char *source, int dest_size, char pad_char)
    {
       int pad_ctr = 0;
       if (dest_size < 1 ) return -1;
       int source_length = strlen(source);
       int data_size = dest_size - 1;
       destination[data_size] = '\0';
       int i = 0;
       while (i < data_size)
       {
          if ( i >= source_length )
          {
             destination[i] = pad_char;
             pad_ctr++;
          }
          else
             destination[i] = source[i];
          i++;
       }
       return pad_ctr;
    }
    
    
    int main(void)
    {
       struct test {
          char name[16];
       };
       struct test test1;
       int chars_padded = copy_with_pad(test1.name,"Hollywood Dan",
                                        sizeof(test1.name),' ');
       printf("%d padding chars added: [%s]\n",chars_padded,test1.name);
       chars_padded = copy_with_pad(test1.name,"The Honorable Hollywood Dan Jr.",
                                     sizeof(test1.name),' ');
       printf("%d padding chars added: [%s]\n",chars_padded,test1.name);
       chars_padded = copy_with_pad(test1.name,"",16,' ');
       printf("%d padding chars added: [%s]\n",chars_padded,test1.name);
    }
    

    output

    2 padding chars added: [Hollywood Dan  ]
    0 padding chars added: [The Honorable H]
    15 padding chars added: [               ]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I don't have photoshop, but I want to make pattern files (.pat)
Let's say I have a method in java, which looks up a user in
Let's say I have thousands of users and I want to make the passwords
Let's say I have window.open (without name parameter), scattered in my project and I
Let's say I have an facebook application running using the JS SDK. First user
let's say I have a select list as follows: <select name='languages'> <option value='german'>German</option> <option
let's say i want to build a smartphone app that tells a user when/where
Let's say I have many items with click event (all have same element name
Let's say that I have a rails plugin called wipy on GitHub. It has
Let's say I have a user enter the URL of an image. After URL

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.