473,583 Members | 3,155 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

fill space

JC
sorry . i got one more problem
i got a string with 4 char. i want to put that in a string with 26 char. how
can i fill space on the remain char.. ??
is that i need to do a while loop do fill the space for the string?

please help!

thanks
Jack
Nov 13 '05 #1
17 3626
dis
"JC" <JC@JC.com> wrote in message news:bl******** *@rain.i-cable.com...
i got a string with 4 char. i want to put that in a string with 26 char. how can i fill space on the remain char.. ??
is that i need to do a while loop do fill the space for the string?


Not sure what you want exactly, but the following program might be a good
start.
#include <string.h>

int main(void)
{
char small[] = "abc"; /* string consisting of 4 characters */
char large[26] = ""; /* string consisting of 26 characters */

/* copy the string small into large, the terminating '\0' excluded */
memcpy(large, small, sizeof small - 1);
/* set the remaining chararacters in large to '-' */
memset(large + sizeof small - 1, '-', sizeof large - sizeof small);
/* terminate large with a '\0' to make it a string */
large[sizeof large - 1] = 0;

/* large contains now the string "abc----------------------" */

return 0;
}

Nov 13 '05 #2
In article <bl*********@ra in.i-cable.com>, JC wrote:
sorry . i got one more problem
i got a string with 4 char. i want to put that in a string with 26 char. how
can i fill space on the remain char.. ??
is that i need to do a while loop do fill the space for the string?


#include <string.h>

/* ... */

char shorty[] = "foo"; /* four chars, including termination */
char lengthy[26] = { 0 };

/* ... */

strcpy(lengthy, shorty);

The string terminator at shorty[3] will be copied over to the
lenthy string, terminating it at lengthy[3]. You don't need to
"fill out" the string with anything.

If you *do* want to fill the remainder of the string (elements
with index 4 through to 25), then memset() will do this for you:

memset(&(length y[4]), '?', 21);
lengthy[25] = '\0';

A for-loop will do the same thing:

int i;
for (i = 4; i < 25; ++i) {
lengthy[i] = '?';
}
lengthy[25] = '\0';

The string will still be terminated at index 3 though, unless
you overwrite the '\0' at that position with something else.

--
Andreas Kähäri
Nov 13 '05 #3
JC
thanks.
this is very useful coding.
"dis" <di*@hotmail.co m> wrote in message
news:bl******** **@news3.tilbu1 .nb.home.nl...
"JC" <JC@JC.com> wrote in message news:bl******** *@rain.i-cable.com...
i got a string with 4 char. i want to put that in a string with 26 char.

how
can i fill space on the remain char.. ??
is that i need to do a while loop do fill the space for the string?


Not sure what you want exactly, but the following program might be a good
start.
#include <string.h>

int main(void)
{
char small[] = "abc"; /* string consisting of 4 characters */
char large[26] = ""; /* string consisting of 26 characters */

/* copy the string small into large, the terminating '\0' excluded */
memcpy(large, small, sizeof small - 1);
/* set the remaining chararacters in large to '-' */
memset(large + sizeof small - 1, '-', sizeof large - sizeof small);
/* terminate large with a '\0' to make it a string */
large[sizeof large - 1] = 0;

/* large contains now the string "abc----------------------" */

return 0;
}

Nov 13 '05 #4
"JC" <JC@JC.com> wrote:
sorry . i got one more problem
i got a string with 4 char. i want to put that in a string with 26 char. how
can i fill space on the remain char.. ??
is that i need to do a while loop do fill the space for the string?


No need to double-post, I was already at it :P!

ANTI-SPOIL-DISCLAIMER: If this is homework, stop reading RIGHT NOW! ;-)

..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..

The following should work (any corrections/suggestions are welcome).
[Note: for pre-C99 drop the restrict keyword]

/*------------------8<----------------*/
#include <stdio.h>
#include <stdlib.h>

/*
** Prototype:
*/
char *Strncpypad( char * restrict s1, const char * restrict s2,
size_t n, int pad_char );

/*
** Simple test:
*/

#define BUFLEN 27

int main( void )
{
char s[ BUFLEN ] = "This is a garbage string!!";
char t[] = "abcd";

printf( "s before: '%s'\n", s );
printf( "t before: '%s'\n", t );
Strncpypad( s, t, BUFLEN, ' ' );
printf( "s after : '%s'\n", s );

return EXIT_SUCCESS;
}
/*
** Strncpypad
**
** Copy up to n-1 characters from the string pointed to by s2 to the
** array pointed to by s1. Characters that follow a null character
** are not copied. The result will be padded with pad_char, if
** applicable, and will always be null-terminated.
*/

char *Strncpypad( char * restrict s1, const char * restrict s2,
size_t buflen, int pad_char )
{
size_t i;

for ( i = 0; i < buflen-1; i++ )
{
if ( *s2 )
s1[ i ] = *s2++;
else
s1[ i ] = pad_char;
}
s1[ i ] = '\0';

return s1;
}
/*------------------8<----------------*/
Regards

Irrwahn
--
Computer: a million morons working at the speed of light.
Nov 13 '05 #5
"dis" <di*@hotmail.co m> wrote:
"JC" <JC@JC.com> wrote in message news:bl******** *@rain.i-cable.com...
i got a string with 4 char. i want to put that in a string with 26 char.how
can i fill space on the remain char.. ??
is that i need to do a while loop do fill the space for the string?


Not sure what you want exactly, but the following program might be a good
start.
#include <string.h>

int main(void)
{
char small[] = "abc"; /* string consisting of 4 characters */
char large[26] = ""; /* string consisting of 26 characters */

/* copy the string small into large, the terminating '\0' excluded */
memcpy(large, small, sizeof small - 1);


Fails if small was dynamically allocated.
No protection against buffer overrun.
/* set the remaining chararacters in large to '-' */
memset(large + sizeof small - 1, '-', sizeof large - sizeof small);
Fails if small and/or large were dynamically allocated.
Again, no protection against buffer overrun.
/* terminate large with a '\0' to make it a string */
large[sizeof large - 1] = 0;

/* large contains now the string "abc----------------------" */

return 0;
}


Regards

Irrwahn
--
Computer: a million morons working at the speed of light.
Nov 13 '05 #6
Andreas Kahari <ak*******@free shell.org> wrote:
In article <bl*********@ra in.i-cable.com>, JC wrote:
sorry . i got one more problem
i got a string with 4 char. i want to put that in a string with 26 char. how
can i fill space on the remain char.. ??
is that i need to do a while loop do fill the space for the string?
#include <string.h>

/* ... */

char shorty[] = "foo"; /* four chars, including termination */
char lengthy[26] = { 0 };

/* ... */

strcpy(lengthy , shorty);

The string terminator at shorty[3] will be copied over to the
lenthy string, terminating it at lengthy[3]. You don't need to
"fill out" the string with anything.

If you *do* want to fill the remainder of the string (elements
with index 4 through to 25), then memset() will do this for you:

memset(&(length y[4]), '?', 21);


No offense intended, but:

Magic number 4.
Magic number 21.
lengthy[25] = '\0';
Magic number 25.

A for-loop will do the same thing:

int i;
for (i = 4; i < 25; ++i) {
Magic number 25.
lengthy[i] = '?';
}
lengthy[25] = '\0';
Magic number 21.

The string will still be terminated at index 3 though, unless
you overwrite the '\0' at that position with something else.


Regards

Irrwahn
--
Computer: a million morons working at the speed of light.
Nov 13 '05 #7
JC
sorry. my problem is
char a[26];
char b[26];

now a is "abcdefg" i want to put that into b with space after "g"

how can i fill the space in that b? or just keep in "a" with space ?
"dis" <di*@hotmail.co m> wrote in message
news:bl******** **@news3.tilbu1 .nb.home.nl...
"JC" <JC@JC.com> wrote in message news:bl******** *@rain.i-cable.com...
i got a string with 4 char. i want to put that in a string with 26 char.

how
can i fill space on the remain char.. ??
is that i need to do a while loop do fill the space for the string?


Not sure what you want exactly, but the following program might be a good
start.
#include <string.h>

int main(void)
{
char small[] = "abc"; /* string consisting of 4 characters */
char large[26] = ""; /* string consisting of 26 characters */

/* copy the string small into large, the terminating '\0' excluded */
memcpy(large, small, sizeof small - 1);
/* set the remaining chararacters in large to '-' */
memset(large + sizeof small - 1, '-', sizeof large - sizeof small);
/* terminate large with a '\0' to make it a string */
large[sizeof large - 1] = 0;

/* large contains now the string "abc----------------------" */

return 0;
}

Nov 13 '05 #8
JC wrote:
sorry . i got one more problem
i got a string with 4 char. i want to put that in a string with 26 char. how
can i fill space on the remain char.. ??
is that i need to do a while loop do fill the space for the string?

please help!

thanks
Jack


You're welcome, Jack. Thanks for the question.

Does this program do what you want?
#include <stdio.h>
#include <string.h>

int main()
{
char little_string[] = "abcd";
char bigger_string[31] = "12345678901234 567890123456";

strcat(bigger_s tring, little_string);

printf("%s\n", bigger_string);

return 0;
}

--Steve

Nov 13 '05 #9
dis
"JC" <JC@JC.com> wrote in message news:bl******** *@rain.i-cable.com...
sorry. my problem is
char a[26];
char b[26];

now a is "abcdefg" i want to put that into b with space after "g"

how can i fill the space in that b? or just keep in "a" with space ?


The program provided in my previous post to this thread should suffice to
answer your question the way I interpret it. Maybe you could clarify your
question by posting some actual code illustrating your problem, and clearly
specifying what you expect the resulting string to be.

[snip]

Nov 13 '05 #10

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

Similar topics

3
11025
by: Chris John Jordan | last post by:
How can one get a text control's size to fill the available space e.g. the width of the cell? I find not input type=text size=100% .. Thanks. -- Chris
0
11057
by: Ian McCall | last post by:
Hello. How can I make an absolutely-positions div element expand and fill the remaining horizontal space available on-screen? The background is that I've used CSS to create the normal 'three frames' look using div elements rather than frames. One of these DIV elements needs to contain an iframe, and that iframe must expand to use all...
8
2225
by: netsurfer | last post by:
Hi: Have a question on making the date automatically filled in by what the user enters in by the date at the top. The date entered at the top would most likely be on a Wednesday then I need to have all the prior dates pop in. Example: User enters 2/9/05 in the date field at the top being a Wednesday...I need the dates at the bottom to...
3
3842
by: Wiggy | last post by:
Hi, It's probably easiest if I describe what I'm trying to do: I have several tables I want to base a query on. In addition I have some dynamic data that I want to join against that consists of several records of information. I could just create a temp table of the dynamic data and join it against my tables, but I thought there was a...
4
3482
by: TS | last post by:
Hi, I'm using the ConvertCurrencyToEnglish module readily available on the internet to convert some currency figures to text. However, I want the code do one additional thing and I'm not sure how to go about it. I want the data field to fill a particular size of space on my form, and I want the area that does not include text from the...
2
6619
by: Mike Gage | last post by:
I am trying to populate DataSets with results from SQL queries. Usually, to that end, the DataSet.Fill method works fine. If however the table includes a character field populated with a space, the DataSet throws an exception. I assume that the restriction is because of the DataSet's use of XML. I tried setting the XMLDataDocument's...
2
2430
by: salmobytes | last post by:
In Java, using the gridbaglayout manager and its complementary gridbagconstraints you can tell a cell to horizontally fill 'any remaining space.' But I can't seem to do that with divs. There must be a way. THE PROBLEM This would be a two-column left-floated layout. I want a left div to contain a variable number of vertical thumbnail-...
7
2197
by: globalrev | last post by:
if i do something like while 1: print "x" will the program ever stop because it runs out of memory? or is the print x never stored somewhere? if u do soemhting like add element to a list it has an upper nbr of elements right? if though huge... does windows or any other OS have some proces that would stop something like this after a...
3
1859
by: DuncanIdaho | last post by:
Good Morning Firefox/2.0.0.14 IE/7.0.5730.11 Opera/9.27 I thought I'd test out my new free server with a couple of URLs (see below) I want an area to 'fill right' regardless of it's contents. If it contains nothing I don't want anything to appear. If something does
0
7827
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8328
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7936
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...
0
8195
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...
0
6581
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...
1
5701
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...
0
5375
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...
0
3845
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1158
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.