Connecting Tech Pros Worldwide Forums | Help | Site Map

Converting Character Array to String

Charles L
Guest
 
Posts: n/a
#1: Nov 13 '05
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



lallous
Guest
 
Posts: n/a
#2: Nov 13 '05

re: Converting Character Array to String


"Charles L" <tjf00@dodo.com.au> wrote in message
news:3fcdfe8e$1@news.comindico.com.au...[color=blue]
> 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[/color]
a[color=blue]
> 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
>[/color]

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


Richard Bos
Guest
 
Posts: n/a
#3: Nov 13 '05

re: Converting Character Array to String


"Charles L" <tjf00@dodo.com.au> wrote:
[color=blue]
> 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?[/color]

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
Eric
Guest
 
Posts: n/a
#4: Nov 13 '05

re: Converting Character Array to String


Charles L <tjf00@dodo.com.au> wrote:
[color=blue]
> 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?[/color]

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... ===
Richard Bos
Guest
 
Posts: n/a
#5: Nov 13 '05

re: Converting Character Array to String


"lallous" <lallous@lgwm.org> wrote:
[color=blue]
> "Charles L" <tjf00@dodo.com.au> wrote in message
> news:3fcdfe8e$1@news.comindico.com.au...[color=green]
> > 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[/color]
> a[color=green]
> > 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?[/color]
>
> What is a string?[/color]

A string is, and I quote the Standard, "a contiguous sequence of
characters terminated by and including the first null character".
[color=blue]
> Are you talking about STL's string ?[/color]

Who is STL?
[color=blue]
> an array of characters is a string.[/color]

Only if terminated by a null character.
[color=blue]
> If you mean that array of characters doesn't have to be null terminated and
> string has to be,[/color]

Of course he does.
[color=blue]
> Try to determine a size out of your character array[/color]

How?

Richard
Lew Pitcher
Guest
 
Posts: n/a
#6: Nov 13 '05

re: Converting Character Array to String


lallous wrote:[color=blue]
> "Charles L" <tjf00@dodo.com.au> wrote in message
> news:3fcdfe8e$1@news.comindico.com.au...
>[color=green]
>>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[/color]
>
> a
>[color=green]
>>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
>>[/color]
>
>
> What is a string?[/color]

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.
[color=blue]
> Are you talking about STL's string ?[/color]

He'd better not be, given that this is comp.lang.c and there's no such thing
as STL here.
[color=blue]
> an array of characters is a string.[/color]

Without the terminating null character, an array of characters is /not/ a
string.
[color=blue]
> 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;[/color]

Correct.
[color=blue]
> Regards
> Elias
>
>[/color]


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

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

Eric
Guest
 
Posts: n/a
#7: Nov 13 '05

re: Converting Character Array to String


Richard Bos <rlb@hoekstra-uitgeverij.nl> wrote:
[color=blue]
> "Charles L" <tjf00@dodo.com.au> wrote:
>[color=green]
> > 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?[/color]
>
> If you know how long the string is supposed to be, it's simple:
> array[length]='\0' will do the trick.[/color]

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... ===
those who know me have no need of my name
Guest
 
Posts: n/a
#8: Nov 13 '05

re: Converting Character Array to String


in comp.lang.c i read:
[color=blue]
>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?[/color]

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
Capstar
Guest
 
Posts: n/a
#9: Nov 13 '05

re: Converting Character Array to String


Richard Bos wrote:[color=blue]
> "lallous" <lallous@lgwm.org> wrote:
>
>[color=green]
>>"Charles L" <tjf00@dodo.com.au> wrote in message
>>news:3fcdfe8e$1@news.comindico.com.au...
>>[color=darkred]
>>>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[/color]
>>
>>a
>>[color=darkred]
>>>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?[/color]
>>[/color][/color]
<snip>[color=blue]
>
>[color=green]
>>Try to determine a size out of your character array[/color]
>
>
> How?[/color]

By keeping track in the "previous operation"

Mark

Richard Bos
Guest
 
Posts: n/a
#10: Nov 13 '05

re: Converting Character Array to String


Capstar <spam0@eg.homeip.net> wrote:
[color=blue]
> Richard Bos wrote:[color=green]
> > "lallous" <lallous@lgwm.org> wrote:
> >[color=darkred]
> >>Try to determine a size out of your character array[/color]
> >
> > How?[/color]
>
> By keeping track in the "previous operation"[/color]

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

Richard
Andrew Robert
Guest
 
Posts: n/a
#11: Nov 13 '05

re: Converting Character Array to String


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';
}
}
Richard Heathfield
Guest
 
Posts: n/a
#12: Nov 13 '05

re: Converting Character Array to String


Andrew Robert wrote:
[color=blue]
> 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';
> }
> }[/color]

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 : binary@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
Joe Wright
Guest
 
Posts: n/a
#13: Nov 13 '05

re: Converting Character Array to String


Andrew Robert wrote:[color=blue]
>
> 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';
> }
> }[/color]

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 ---
Barry Schwarz
Guest
 
Posts: n/a
#14: Nov 13 '05

re: Converting Character Array to String


On Sun, 7 Dec 2003 08:37:09 +0000 (UTC), Richard Heathfield
<dontmail@address.co.uk.invalid> wrote:
[color=blue]
>Andrew Robert wrote:
>[color=green]
>> 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';
>> }
>> }[/color]
>
>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';
>}[/color]

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>>
Kelsey Bjarnason
Guest
 
Posts: n/a
#15: Nov 13 '05

re: Converting Character Array to String


On Sun, 07 Dec 2003 08:37:09 +0000, Richard Heathfield wrote:
[color=blue]
> Andrew Robert wrote:
>[color=green]
>> 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';
>> }
>> }[/color]
>
> 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';
> }[/color]

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.


Closed Thread