473,371 Members | 1,452 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,371 software developers and data experts.

Converting Character Array to String

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
14 12887
"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
"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
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
"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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Venkat | last post by:
Hi All, Can someone tell me how do we convert a character array to string. I know the other way. For eg:- String strvar = "hello"; printf("%s",strvar.c_str()); regards,
9
by: Christian Kandeler | last post by:
Hi, if I want to store the string "123456" in a variable of type char, I can do it like this: char s = "123456"; Or like this: char s = { '1', '2', '3', '4', '5', '6', '\0' };
8
by: moondaddy | last post by:
I need to convert a byte array to a string and pass it as a parameter in a URL and then convert it back to the original byte array. However, its getting scrambled in the conversion. In short,...
12
by: nikNjegovan | last post by:
So i have a tachometer that I can communicated with via UART which gives me a character array of ascii values in the following form: Standard ascii 7 characters including decimal point such that...
6
by: funmachine | last post by:
Hi there, everyone! I'm a student studying computer animation. But alas, in order to graduate I have to take a programming class and it's kicking my butt. I have two projects left to write (and...
5
by: Chuck Cobb | last post by:
I'm having a problem converting an array to a collection. The code I'm using looks like the following: CustomerInfo cArray; Collection<CustomerInfo> cCollection = new...
3
by: Jim Langston | last post by:
Is it possible to initialize a std::string with a character array, not neccessarily null terminated? I.E. Given something like this: char buffer; buffer = 0x01; buffer = 0x00; buffer = 'A';...
14
by: Shhnwz.a | last post by:
Hi, I am in confusion regarding jargons. When it is technically correct to say.. String or Character Array.in c. just give me your perspectives in this issue. Thanx in Advance.
2
by: quietforever | last post by:
How do you convert a character array into a integer array? For example: If the character array is entered as'12' then how can get an integer array that contains the integers 1 and 2?
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.