473,882 Members | 1,593 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
14 12934
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.pow ernet.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******@addre ss.co.uk.invali d> 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
10679
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
3373
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
4212
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, here's the code: ====================================== Dim textConverter As New ASCIIEncoding Dim sParam As String = "This is my cool param" Dim bytParam() As Byte 'load the byte array here...
12
8558
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 the array when printed would equal the rpm value ie 2031.00 rpm = I've been rummaging everywhere and can't find anything to tackle this. I would be extreemly greatful to anyone that can point me in the right
6
2124
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 believe me, to some of the veterans out there, it'll be a piece of cake) one of which takes a character array and capitalizes where necessary - eg. begining of the string, after a period. Names and such aren't taken into account. Like I said,...
5
11324
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 Collection<CustomerInfo>(cArray); The above code creates a collection from the array but the collection winds up as a read-only collection. Is there an easy way to convert an array to a collection and have it not be read-only?
3
20302
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'; buffer = 'B'; buffer = 'C';
14
4106
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
2159
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?
0
9931
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9777
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11108
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10725
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9557
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7956
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5781
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5978
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3226
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.