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 8636811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:18:51+00:00 2026-06-12T10:18:51+00:00

Using printf in C or Bash shell, how can I left- and right-justify two

  • 0

Using printf in C or Bash shell, how can I left- and right-justify two strings (character arrays) to a given length?

E.g. if the strings are “stack” and “overflow”, and the length is 20 characters, I wish to print

stack-------overflow

(for clarity, each space is shown as a dash).

The strings are of unknown length. If the total length + 1 exceeds the length given, is it possible to truncate one or both strings from either given direction and leave a space between them? E.g. if length is 10, can we get any of these?

stack-over
stack-flow
s-overflow
k-overflow

I know that printf(“%10s”, string) justifies one string to the right and printf(“%-10s”, string) justifies one string to the left, but can’t find a way to justify 2 strings, or to truncate them.

  • 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-12T10:18:52+00:00Added an answer on June 12, 2026 at 10:18 am

    This is longer than battery’s, but imho it splits the strings better. Also it uses printf for truncation where possible, falling back to other mechanisms only for left-hand truncation of the second argument.

    Bash:

    truncat () {
      local len=$1 a=$2 b=$3 len_a=${#2} len_b=${#3}
      if ((len <= 0)); then return
      elif ((${len_b} == 0)); then
        printf %-${len}.${len}s "$a"
      elif ((${len_a} == 0)); then
        printf %${len}.${len}s "${b: -$((len<len_b?len:len_b))}"
      elif ((len <= 2)); then
        printf %.${len}s "${a::1}${b: -1}"
      else
        local adj_a=$(((len_a*len+len_b-len_a)/(len_a+len_b)))
        local adj_b=$(((len_b*len+len_a-len_b-1)/(len_a+len_b)))
        printf "%-${adj_a}.${adj_a}s %${adj_b}.${adj_b}s" \
               "$a" \
               "${b: -$((len_b<adj_b?len_b:adj_b))}"
      fi
    }
    

    C:

    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <unistd.h>
    
    void truncat(long len, const char* a, const char* b) {
      if (len <= 0) return;
      unsigned long long len_a = strlen(a);
      unsigned long long len_b = strlen(b);
      if (!len_b)
        printf("%-*.*s", (int)len, (int)len, a);
      else if (!len_a)
        printf("%*s", (int)len, b + (len < len_b ? len_b - len : 0));
      else if (len <= 2)
        printf("%.1s%.*s", a, (int)(len - 1), b + len_b - 1);
      else {
        unsigned long long adj_a = (len_a * len + len_b - len_a) / (len_a + len_b);
        unsigned long long adj_b = (len_b * len + len_a - len_b - 1) / (len_a + len_b);
        printf("%-*.*s %*s",
               (int)adj_a, (int)adj_a, a,
               (int)adj_b, b + (adj_b < len_b ? len_b - adj_b : 0));
      }
    }
    
    int main(int argc, char** argv) {
      truncat(atol(argv[1]), argv[2], argv[3]);
      return 0;
    }
    

    Sample output:

    $ for i in {0..20}; do printf "%2d '%s'\n" $i "$(./truncat $i stack overflow)"; done
     0 ''
     1 's'
     2 'sw'
     3 's w'
     4 's ow'
     5 'st ow'
     6 'st low'
     7 'st flow'
     8 'sta flow'
     9 'sta rflow'
    10 'stac rflow'
    11 'stac erflow'
    12 'stac verflow'
    13 'stack verflow'
    14 'stack overflow'
    15 'stack  overflow'
    16 'stack   overflow'
    17 'stack    overflow'
    18 'stack     overflow'
    19 'stack      overflow'
    20 'stack       overflow'
    

    Disclaimer: The arithmetic can overflow, in which case the output will be wrong (or, if you can arrange for strlen(a)+strlen(b) to be exactly 2^64 bytes, the program will SIG_FPE). I can provide an explanation for the adj_a and adj_b computations if anyone cares.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I make this print properly without using two printf calls? char* second
Wondering if someone could point me in right direction using bash shell scripting and
How can I output colored text using printf on both Mac OS X and
One can remove all calls to printf() using #define printf . What if I
I'm using jps in a bash shell to find all running java processes. I
I'm completely new in the Linux world, i-m using a Bash shell and practicing
I'm using bash shell. Hi,ppl Would be glad if someone could provide some kind
I am using Solaris 5.10 and working on Bash Shell . I have a
Below is my Bash Shell Script from which I am executing my two Hive
When using printf for string, I got : string key = 123; printf(Value is

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.