Converting Character Array to String

C

Charles L

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
 
L

lallous

Charles L said:
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
 
R

Richard Bos

Charles L said:
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
 
E

Eric

Charles L said:
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.
 
R

Richard Bos

lallous said:
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
 
L

Lew Pitcher

lallous said:
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
 
E

Eric

Richard Bos said:
Charles L said:
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.
 
T

those who know me have no need of my name

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

Andrew Robert

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';
}
}
 
R

Richard Heathfield

Andrew said:
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';
}
 
J

Joe Wright

Andrew said:
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';
 
B

Barry Schwarz

Andrew said:
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>>
 
K

Kelsey Bjarnason

Andrew said:
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.
 

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

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,188
Latest member
Crypto TaxSoftware

Latest Threads

Top