fill space

J

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
 
D

dis

JC said:
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;
}
 
A

Andreas Kahari

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 = '?';
}
lengthy[25] = '\0';

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

Irrwahn Grausewitz

JC said:
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
 
J

JC

thanks.
this is very useful coding.
dis said:
JC said:
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;
}
 
I

Irrwahn Grausewitz

dis said:
JC said:
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
 
I

Irrwahn Grausewitz

Andreas Kahari said:
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 = '?';
}
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
 
J

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 said:
JC said:
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;
}
 
S

Steve Zimmerman

JC said:
> 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
 
S

Steve Zimmerman

JC said:
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;
}
 
D

dis

JC said:
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]
 
J

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 said:
JC said:
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]
 
A

Andreas Kahari

[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
 
M

Mike Wahler

JC said:
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
 
M

Mike Wahler

Steve Zimmerman said:
JC said:
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
 
I

Irrwahn Grausewitz

Steve Zimmerman said:
JC said:
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
 
J

JC

i am not a programmer.


Mike Wahler said:
Steve Zimmerman said:
JC said:
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
 
M

Mike Wahler

JC said:
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
 

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,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top