473,396 Members | 1,970 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 3606
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*********@rain.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(&(lengthy[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.com> 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.com> 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*******@freeshell.org> wrote:
In article <bl*********@rain.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(&(lengthy[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.com> 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] = "12345678901234567890123456";

strcat(bigger_string, little_string);

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

return 0;
}

--Steve

Nov 13 '05 #9
JC wrote:
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"


#include <stdio.h>
#include <string.h>

int main()
{
char a[26] = "abcdefg";
char b[26];

strcpy(b, strcat(a, " "));

printf("%s :Before this colon is string b\n", b);

return 0;
}

Nov 13 '05 #10
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 #11
JC
the problem i met is i need to put a short string in to a file which provide
a field for that string is 26 char. but i am not sure.. how many char did
the user input. therefore i need to know how to check the size of data that
input by user and fill space to the rest of that string.. than i can put the
string to the file.. i need to keep the file tiny.

Thanks

JC

"dis" <di*@hotmail.com> wrote in message
news:bl**********@news3.tilbu1.nb.home.nl...
"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 #12
In article <jg********************************@4ax.com>, Irrwahn Grausewitz wrote:
Andreas Kahari <ak*******@freeshell.org> wrote:

[cut]
memset(&(lengthy[4]), '?', 21);


No offense intended, but:

Magic number 4.
Magic number 21.

[other magic numbers that could easily be computed from the
length of the strings involved snipped]

You're absolutely right. I didn't think about that when I was
writing the code and I didn't catch it when reading it through.
Thanks for noticing it.

Andreas

--
Andreas Kähäri
Nov 13 '05 #13

"JC" <JC@JC.com> wrote in message news:bl*********@rain.i-cable.com...
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!


You asked this same question in your thread
"strcpy and strcat problem", and I answered
that post with an example today.

-Mike
Nov 13 '05 #14
"Steve Zimmerman" <st******@sonic.net> wrote in message
news:3F************@sonic.net...
JC wrote:
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"


#include <stdio.h>
#include <string.h>

int main()
{
char a[26] = "abcdefg";
char b[26];

strcpy(b, strcat(a, " "));


You're not a real programmer, are you? I surely hope not.

-Mike
Nov 13 '05 #15
Steve Zimmerman <st******@sonic.net> wrote:
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] = "12345678901234567890123456";

strcat(bigger_string, little_string);


How does this relate to the OP's problem in any way?

<SNIP>

Irrwahn
--
Great minds run in great circles.
Nov 13 '05 #16
JC
i am not a programmer.
"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:8E*****************@newsread3.news.pas.earthl ink.net...
"Steve Zimmerman" <st******@sonic.net> wrote in message
news:3F************@sonic.net...
JC wrote:
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"


#include <stdio.h>
#include <string.h>

int main()
{
char a[26] = "abcdefg";
char b[26];

strcpy(b, strcat(a, " "));


You're not a real programmer, are you? I surely hope not.

-Mike

Nov 13 '05 #17
"JC" <JC@JC.com> wrote in message news:bl*********@rain.i-cable.com...
i am not a programmer.


That was not addressed to you, but to Steve Zimmerman.
Did not your news reader show the thread properly?

From what I've seen from him so far, I recommend you
regard his code with suspicion.

BTW please don't top post.

-Mike
Nov 13 '05 #18

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

Similar topics

3
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
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...
8
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...
3
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...
4
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...
2
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,...
2
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...
7
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...
3
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
0
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...
0
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,...

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.