By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,412 Members | 917 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,412 IT Pros & Developers. It's quick & easy.

Converting Character Array to String

P: n/a
I don't know if this is a stupid quesiton or not.

I would like to know how to convert an array of characters generated from a
previous operation to a string ie how do I append a null character at the
end? I haven't been able to do it so far. Is there a string function I can
use?

Can anyone help?

Charles L
Nov 13 '05 #1
Share this Question
Share on Google+
14 Replies


P: n/a
"Charles L" <tj***@dodo.com.au> wrote in message
news:3f********@news.comindico.com.au...
I don't know if this is a stupid quesiton or not.

I would like to know how to convert an array of characters generated from a previous operation to a string ie how do I append a null character at the
end? I haven't been able to do it so far. Is there a string function I can
use?

Can anyone help?

Charles L


What is a string?
Are you talking about STL's string ?

an array of characters is a string.

If you mean that array of characters doesn't have to be null terminated and
string has to be, then the answer to your question would be:
Try to determine a size out of your character array then do this:
string[length] = 0;

Regards
Elias
Nov 13 '05 #2

P: n/a
"Charles L" <tj***@dodo.com.au> wrote:
I don't know if this is a stupid quesiton or not.

I would like to know how to convert an array of characters generated from a
previous operation to a string ie how do I append a null character at the
end? I haven't been able to do it so far. Is there a string function I can
use?


If you know how long the string is supposed to be, it's simple:
array[length]='\0' will do the trick. If you don't know how long it's
supposed to be, there's no feasible way to find out from the array
alone, since the characters past the required length can be filled with
anything, including seemingly normal text.

Richard
Nov 13 '05 #3

P: n/a
Charles L <tj***@dodo.com.au> wrote:
I would like to know how to convert an array of characters generated from a
previous operation to a string ie how do I append a null character at the
end? I haven't been able to do it so far. Is there a string function I can
use?


Do you track how many characters you have?

If you know this, what you want to do is easy.

long nCharacters; /*should be set to the number of characters you*/
/*have*/

yourString[nCharacters] = '\0';

This does assume 'yourString' has the space to store the null character.
If you do not know that it does, you will need to dynamically allocate a
string that will.

long nCharacters; /*should be set to the number of characters you*/
/*have*/
char *newString = NULL;

newString = malloc( nCharacters + 1 ); /*should check for NULL*/
strncpy( newString, yourString, nCharacters );
newString[nCharacters] = '\0';
If you do not know how many characters you have or cannot determine it,
what you want to do is impossible.
--
== Eric Gorr ========= http://www.ericgorr.net ========= ICQ:9293199 ===
"Therefore the considerations of the intelligent always include both
benefit and harm." - Sun Tzu
== Insults, like violence, are the last refuge of the incompetent... ===
Nov 13 '05 #4

P: n/a
"lallous" <la*****@lgwm.org> wrote:
"Charles L" <tj***@dodo.com.au> wrote in message
news:3f********@news.comindico.com.au...
I don't know if this is a stupid quesiton or not.

I would like to know how to convert an array of characters generated from a
previous operation to a string ie how do I append a null character at the
end? I haven't been able to do it so far. Is there a string function I can
use?


What is a string?


A string is, and I quote the Standard, "a contiguous sequence of
characters terminated by and including the first null character".
Are you talking about STL's string ?
Who is STL?
an array of characters is a string.
Only if terminated by a null character.
If you mean that array of characters doesn't have to be null terminated and
string has to be,
Of course he does.
Try to determine a size out of your character array


How?

Richard
Nov 13 '05 #5

P: n/a
lallous wrote:
"Charles L" <tj***@dodo.com.au> wrote in message
news:3f********@news.comindico.com.au...
I don't know if this is a stupid quesiton or not.

I would like to know how to convert an array of characters generated from
a
previous operation to a string ie how do I append a null character at the
end? I haven't been able to do it so far. Is there a string function I can
use?

Can anyone help?

Charles L

What is a string?


7.1.1 Definitions of terms
1 A string is a contiguous sequence of characters terminated by and
including the first null character. The term multibyte string is sometimes
used instead to emphasize special processing given to multibyte characters
contained in the string or to avoid confusion with a wide string. A pointer
to a string is a pointer to its initial (lowest addressed) character. The
length of a string is the number of bytes preceding the null character and
the value of a string is the sequence of the values of the contained
characters, in order.
Are you talking about STL's string ?
He'd better not be, given that this is comp.lang.c and there's no such thing
as STL here.
an array of characters is a string.
Without the terminating null character, an array of characters is /not/ a
string.
If you mean that array of characters doesn't have to be null terminated and
string has to be, then the answer to your question would be:
Try to determine a size out of your character array then do this:
string[length] = 0;
Correct.
Regards
Elias

--
Lew Pitcher, IT Consultant, Application Architecture
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)

Nov 13 '05 #6

P: n/a
Richard Bos <rl*@hoekstra-uitgeverij.nl> wrote:
"Charles L" <tj***@dodo.com.au> wrote:
I don't know if this is a stupid quesiton or not.

I would like to know how to convert an array of characters generated from a
previous operation to a string ie how do I append a null character at the
end? I haven't been able to do it so far. Is there a string function I can
use?


If you know how long the string is supposed to be, it's simple:
array[length]='\0' will do the trick.


Only if 'array' is big enough the hold the NULL character.

--
== Eric Gorr ========= http://www.ericgorr.net ========= ICQ:9293199 ===
"Therefore the considerations of the intelligent always include both
benefit and harm." - Sun Tzu
== Insults, like violence, are the last refuge of the incompetent... ===
Nov 13 '05 #7

P: n/a
in comp.lang.c i read:
I would like to know how to convert an array of characters generated from a
previous operation to a string ie how do I append a null character at the
end? I haven't been able to do it so far. Is there a string function I can
use?


there is no string function you can use, because until there is a null byte
at the end it's not a string, merely a sequence of characters.

--
a signature
Nov 13 '05 #8

P: n/a
Richard Bos wrote:
"lallous" <la*****@lgwm.org> wrote:

"Charles L" <tj***@dodo.com.au> wrote in message
news:3f********@news.comindico.com.au...
I don't know if this is a stupid quesiton or not.

I would like to know how to convert an array of characters generated from
a
previous operation to a string ie how do I append a null character at the
end? I haven't been able to do it so far. Is there a string function I can
use?

<snip>
Try to determine a size out of your character array

How?


By keeping track in the "previous operation"

Mark

Nov 13 '05 #9

P: n/a
Capstar <sp***@eg.homeip.net> wrote:
Richard Bos wrote:
"lallous" <la*****@lgwm.org> wrote:
Try to determine a size out of your character array


How?


By keeping track in the "previous operation"


Well, quite. But that's not determining it "out of" the array.

Richard
Nov 13 '05 #10

P: n/a
Try using a variation of this code.

It scans the contents of the array and replaces new-line or carriage
returns with a null character.
int count;
for ( count = 0; count < strlen( genre ); count++ )
{
if ( genre[count] == '\n' || genre[count] == '\r' )
{
genre[count] = '\0';
}
}
Nov 13 '05 #11

P: n/a
Andrew Robert wrote:
Try using a variation of this code.

It scans the contents of the array and replaces new-line or carriage
returns with a null character.
int count;
for ( count = 0; count < strlen( genre ); count++ )
{
if ( genre[count] == '\n' || genre[count] == '\r' )
{
genre[count] = '\0';
}
}


Better:

size_t count;
size_t len = strlen(genre);
for(count = 0; count < len; count++)
{
if(genre[count] == '\n' || genre[count] == '\r')
{
genre[count = '\0';
}
}

Better still:

char *p = strpbrk(genre, "\r\n");
if(p != NULL)
{
*p = '\0';
}

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 13 '05 #12

P: n/a
Andrew Robert wrote:

Try using a variation of this code.

It scans the contents of the array and replaces new-line or carriage
returns with a null character.

int count;

for ( count = 0; count < strlen( genre ); count++ )
{
if ( genre[count] == '\n' || genre[count] == '\r' )
{
genre[count] = '\0';
}
}


char *g = genre;
int c;
while ((c = *g++) != '\0')
if (c == '\r' || c == '\n')
*(g - 1) = '\0';
--
Joe Wright http://www.jw-wright.com
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 13 '05 #13

P: n/a
On Sun, 7 Dec 2003 08:37:09 +0000 (UTC), Richard Heathfield
<do******@address.co.uk.invalid> wrote:
Andrew Robert wrote:
Try using a variation of this code.

It scans the contents of the array and replaces new-line or carriage
returns with a null character.
int count;
for ( count = 0; count < strlen( genre ); count++ )
{
if ( genre[count] == '\n' || genre[count] == '\r' )
{
genre[count] = '\0';
}
}


Better:

size_t count;
size_t len = strlen(genre);
for(count = 0; count < len; count++)
{
if(genre[count] == '\n' || genre[count] == '\r')
{
genre[count = '\0';
}
}

Better still:

char *p = strpbrk(genre, "\r\n");
if(p != NULL)
{
*p = '\0';
}


Andrew claimed he replaced all \n and \r but his code only replaces
the first.

"Better" replaces every \n or \r in the original string.

"Better still" replaces only the first \n or \r.

If it was your intent to demonstrate both capabilities, still better
labels would have helped.
<<Remove the del for email>>
Nov 13 '05 #14

P: n/a
On Sun, 07 Dec 2003 08:37:09 +0000, Richard Heathfield wrote:
Andrew Robert wrote:
Try using a variation of this code.

It scans the contents of the array and replaces new-line or carriage
returns with a null character.
int count;
for ( count = 0; count < strlen( genre ); count++ )
{
if ( genre[count] == '\n' || genre[count] == '\r' )
{
genre[count] = '\0';
}
}


Better:

size_t count;
size_t len = strlen(genre);
for(count = 0; count < len; count++)
{
if(genre[count] == '\n' || genre[count] == '\r')
{
genre[count = '\0';
}
}

Better still:

char *p = strpbrk(genre, "\r\n");
if(p != NULL)
{
*p = '\0';
}


Hmm. I haven't seen the original post, but the subject says "character
array to string" - so I'd expect the original data to _not_ be strings,
hence strpbrk, strlen and the like would be bad choices.
Nov 13 '05 #15

This discussion thread is closed

Replies have been disabled for this discussion.