473,785 Members | 2,308 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to left pad a string with spaces?

If I have a string say:

myvar[128] = "This is a string";

How do I use sprintf to convert the string so it has 4 spaces padded on
the left like:

" This is a string";
Thanks!
Jun 27 '08
41 12467
CBFalconer said:

<snip>
I guess I am getting sloppy.
It's been happening for some time.
Well, I did
mark it 'untested', and say 'something like this'. :-)
In future, please mark it "tested", and say "exactly like this", and try to
ensure that those claims will stand up to expert scrutiny.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #41
Ben Bacarisse <be********@bsb .me.ukwrites:
Barry Schwarz <sc******@dqel. comwrites:
>On Tue, 10 Jun 2008 19:27:31 +0100, Ben Bacarisse
<be********@bs b.me.ukwrote:
>>>Barry Schwarz <sc******@dqel. comwrites:
<snip>
>>>As long as your array is at least twice as long as (your string plus
the number of spaces to insert):
sprintf(myvar+6 4, "%s", myvar);
sprintf(myvar, " %s", myvar+64);

I don't think that is permitted:

7.19.6.6 The sprintf function

... If copying takes place between objects that overlap, the
behavior is undefined.

It does not say "between /regions/ of objects that overlap". It seems
any overlap is enough to render it undefined.

It seems that the term "object" is not used with great precision
here.
I have to think that may be deliberate. See below...
>What would happen if myvar were defined as
char myvar[2][64] = {"This ..."};
and in the calls to sprintf each use of myvar was cast to char*?
myvar[0] and [1] are objects that do not overlap but both are part of
the object myvar. Is this overlap? Would it be overlap if the
arguments were changed to myvar[0] and myvar[1]?
<snip>
>I will assert that the intent of the word object in this section is
meant to refer to the source and destination strings and therefore
under the conditions I postulated there is no overlap.
And I now agree.
These are good points, but I think the wording is clear that the
behaviour is undefined. The intent may not be this wide, but the word
"object" must mean the whole object, surely?
The definition of "object" is very loose: "region of data storage in
the execution environment, the contents of which can represent
values". Thus within an object declared: 'char obj[128];' there
are many other objects. Each char is an object as well as the whole
thing, but the region from obj[3] too obj[30] (inclusive) is also an
object. In deciding if "objects overlap" there are therefore two ways
to go: define "the objects" as the smallest ones involved in the copy,
or define them as the largest. Anything else is rather arbitrary.

It seems obvious to me, now, that the intent is that the smallest
objects be the ones that need to be overlap-free. So, in short,
forget my objection!

--
Ben.
Jun 27 '08 #42

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2061
by: Uttam | last post by:
Hello, Using ADO I have created a table and have also created fields. To create fields, I have used the following: ..Columns.Append "Field_Name", adWChar, 6 I load records into this created table including into this field.
4
2738
by: mhw | last post by:
I don't found function! Thanks£¡
1
4086
by: Anonieko Ramos | last post by:
> > > How to display multiple spaces in a dropdownlist webform1.aspx <asp:DropDownList id="DropDownList1" runat="server"></asp:DropDownList>
3
6100
by: Sam | last post by:
I want to divide character into 2 section. Example, 200412 divided into 2004 in A block and 12 in B block. Please advise how to use left(string, length) or right(string, length) for above request. Many thanks in advance.
3
15200
by: NathanC | last post by:
Left('ironman',4) Result - 'iron' Is there anyway to excute this task in VB.NET? Currently, I am determing the value of the 4th character, splitting on that, then grabbing the value of the first part of the split. It works, but is really cumbersome and doesn't account for a string with two consecutive values that are the same, but I might want to include both of them Example: ('neccesarry') Result: 'necce'
2
7242
by: Reny | last post by:
Hi, I came across a doubt on the String.PadLeft Method.The doubt is this -- What's difference it make if the argument to this function carries an integer whose value is less than the length of the string object For example in the sample code below(VB.NET) I could not see any difference
2
1947
by: akoymakoy | last post by:
is there a function to remove leading and trailing spaces on strings? example: word = " THE QUICK BROWN FOX " output: word = "THE QUICK BROWN FOX"
3
4010
by: AWW | last post by:
RichTextBox.Text = string & vbCR works but if I extract a substring with microsoft.visualbasic.left then vbCR stops working. I can do F5 executes with/without the Left and the vbCr does/doesNot work. Comment?
5
3772
by: =?Utf-8?B?Qm9iQWNoZ2lsbA==?= | last post by:
I need a function (or code) that will physically change the words in a text string to make the first word be last, second word next to last, etc. but maintain the same position of the letters in each word. e.g. Before: The dog ran After: ran dog the Is there such a thing?
3
15384
by: Elohim | last post by:
Hi to everybody, I'm a learner of C++. I'll really appreciate if you can help me or teach me something. Thank you in advance. #include<iostream> #include<string> int main() { std::cout << "Introduce your first name:";
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10095
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9954
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8979
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7502
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6741
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5513
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4054
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.