One word to the right extreme and another to the left extreme

K

karthikbalaguru

Hi,
I have a query based on the format
specifier in printf.

Consider the below code -

#include<stdio.h>
int main(void)
{
char *s = "Hello World";
printf("%15.11s\n",s);
printf("%-15.11s\n",s);
return 0;
}

I got the below output :-
Hello World
Hello World

Is ther any specifier to print the Hello World
such that Hello goes to the left extreme and
World to the Right extreme with spaces inbetweeen
them ?

That is, i am expecting the below output using
any of the format specifier in printf :-
Hello World

Thx in advans,
Karthik Balaguru
 
B

Ben Pfaff

karthikbalaguru said:
Is ther any specifier to print the Hello World
such that Hello goes to the left extreme and
World to the Right extreme with spaces inbetweeen
them ?

That is, i am expecting the below output using
any of the format specifier in printf :-
Hello World

No, there is no printf specifier to do that.
 
K

karthikbalaguru

No, there is no printf specifier to do that.

As there is lot of tricks with format specifiers,
a trick for the above output can be added into C.
I think, there should be some support as below in
the future releases of C to do that -
printf("%-15.-5.5s\n",s);
where the -5 is for having the Hello on
the extreme left and the 5 is for having
the World on the extreme right.

Karthik Balaguru
 
B

Ben Pfaff

karthikbalaguru said:
I think, there should be some support as below in
the future releases of C to do that -
printf("%-15.-5.5s\n",s);
where the -5 is for having the Hello on
the extreme left and the 5 is for having
the World on the extreme right.

In the 20 years or so that I have been writing C code, I do not
recall ever needing such a feature.

Why don't you just write a function to do what you want?
 
M

Martin Ambuhl

karthikbalaguru said:
Is ther any specifier to print the Hello World
such that Hello goes to the left extreme and
World to the Right extreme with spaces inbetweeen
them ?

#include <stdio.h>

int main(void)
{
const int leftchars = 7, spacing = 3, rightchars = 7;
printf("With explicit padding space:\n"
"%-*s%*s%*s\n\n", leftchars, "Hello", spacing, "",
rightchars, "World");

printf("Without explicit padding space:\n"
"%-*s%*s\n", leftchars, "Hello", rightchars, "World");
return 0;
}

[Output]

With explicit padding space:
Hello World

Without explicit padding space:
Hello World
 
K

Keith Thompson

karthikbalaguru said:
As there is lot of tricks with format specifiers,
a trick for the above output can be added into C.
I think, there should be some support as below in
the future releases of C to do that -
printf("%-15.-5.5s\n",s);
where the -5 is for having the Hello on
the extreme left and the 5 is for having
the World on the extreme right.

And what should this do if the string doesn't contain any spaces? Or
if it contains more than two words? Or if it has leading and trailing
spaces? What about white space other than space characters?

A printf format string is, in effect, written in a very small language
of its own. It's not a general-purpose language; it's designed for
one specific narrow purpose. Don't expect that language to become so
sophisticated that it will do your programming for you, when the C
language itself is more than powerful enough to do what you want.
 
C

CBFalconer

karthikbalaguru said:
.... snip ...

That is, i am expecting the below output using
any of the format specifier in printf :-

If you want to know about printf formats and you are using Windoze
I suggest you mount 4dos, n869.txt, Buergs list, and the following
two alias'. Then the command 'printf' will tell you all you want
to know. Instantaneously. Without cluttering the C newsgroup.

[1] c:\>alias printf
cstd 7.19.6.1

[1] c:\>alias cstd
list c:\stds\n869.txt /f%1 %2&
 
K

karthikbalaguru

karthikbalaguru said:
Hi,
I have a query based on the format
specifier in printf.
Consider the below code -
#include<stdio.h>
int main(void)
{
char *s = "Hello World";
printf("%15.11s\n",s);
printf("%-15.11s\n",s);
return 0;
}
I got the below output :-
Hello World
Hello World
Is ther any specifier to print the Hello World
such that Hello goes to the left extreme and
World to the Right extreme with spaces inbetweeen
them ?

No. The string[*] printed by "%s" and its variants
is one single thing, not a sequence of separable things.

[*] With a precision specifier -- the ".11" in your
example -- the thing printed need only be a sequence of
characters, not necessarily a string.
That is, i am expecting the below output using
any of the format specifier in printf :-
Hello World

You'll need to make your own decision about where
to divide the string and how much space to use. For
example, if you want to put the first five characters
on the left and the rest on the right, using fifteen
positions in all, you could write

printf ("%.5s%10s\n", s, s+5);

More generally, if you want to put the first m on the
left and the rest on the right, using n positions in
all (m and n both ints),

printf ("%.*s%*s\n", m, s, n-m, s+m);

Nice method for printing the One word to the right
extreme and another to the left extreme .

Karthik Balaguru
 
C

CBFalconer

Richard said:
CBFalconer said:

He has asked a topical question, and you've introduced plenty of
non-topical distractions in your failure to answer his question.


Cluttering the C newsgroup with a question about C?

You may disagree with my suggestion. Fine. It is quite evident
that you have not noticed balagurus continuous posing of questions
that are answered in almost any C text. Not once. Apparently
daily.
 
J

JosephKK

As there is lot of tricks with format specifiers,
a trick for the above output can be added into C.
I think, there should be some support as below in
the future releases of C to do that -
printf("%-15.-5.5s\n",s);
where the -5 is for having the Hello on
the extreme left and the 5 is for having
the World on the extreme right.

Karthik Balaguru

Confrakulations, you have made it to the very rare "entertaining
nutcase" catagory. Membership has historically been proven to be
transitory.
.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top