Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > fill space

Reply
Thread Tools

fill space

 
 
JC
Guest
Posts: n/a
 
      09-27-2003
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


 
Reply With Quote
 
 
 
 
dis
Guest
Posts: n/a
 
      09-27-2003
"JC" <(E-Mail Removed)> wrote in message news:bl3jf4$(E-Mail Removed)-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;
}



 
Reply With Quote
 
 
 
 
Andreas Kahari
Guest
Posts: n/a
 
      09-27-2003
In article <bl3jf4$(E-Mail Removed)-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
 
Reply With Quote
 
Irrwahn Grausewitz
Guest
Posts: n/a
 
      09-27-2003
"JC" <(E-Mail Removed)> 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 !

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.
 
Reply With Quote
 
JC
Guest
Posts: n/a
 
      09-27-2003
thanks.
this is very useful coding.
"dis" <(E-Mail Removed)> wrote in message
news:bl3mjd$lvb$(E-Mail Removed)1.nb.home.nl...
> "JC" <(E-Mail Removed)> wrote in message news:bl3jf4$(E-Mail Removed)-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;
> }
>
>
>



 
Reply With Quote
 
Irrwahn Grausewitz
Guest
Posts: n/a
 
      09-27-2003
"dis" <(E-Mail Removed)> wrote:

>"JC" <(E-Mail Removed)> wrote in message news:bl3jf4$(E-Mail Removed)-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.
 
Reply With Quote
 
Irrwahn Grausewitz
Guest
Posts: n/a
 
      09-27-2003
Andreas Kahari <(E-Mail Removed)> wrote:

>In article <bl3jf4$(E-Mail Removed)-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.
 
Reply With Quote
 
JC
Guest
Posts: n/a
 
      09-27-2003
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" <(E-Mail Removed)> wrote in message
news:bl3mjd$lvb$(E-Mail Removed)1.nb.home.nl...
> "JC" <(E-Mail Removed)> wrote in message news:bl3jf4$(E-Mail Removed)-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;
> }
>
>
>



 
Reply With Quote
 
Steve Zimmerman
Guest
Posts: n/a
 
      09-27-2003
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





 
Reply With Quote
 
Steve Zimmerman
Guest
Posts: n/a
 
      09-27-2003
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;
}

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Make TR/TD fill remaining vertical space? M Hedges HTML 2 02-14-2004 05:40 PM
Why Python style guide (PEP-8) says 4 space indents instead of 8 space??? 8 space indents ever ok?? Christian Seberino Python 21 10-27-2003 04:20 PM
Re: Why Python style guide (PEP-8) says 4 space indents instead of8 space??? 8 space indents ever ok?? Ian Bicking Python 2 10-24-2003 11:15 AM
Re: Why Python style guide (PEP-8) says 4 space indents instead of8 space??? 8 space indents ever ok?? Ian Bicking Python 2 10-23-2003 07:07 AM
Stack space, global space, heap space Shuo Xiang C Programming 10 07-11-2003 07:30 PM



Advertisments