473,324 Members | 2,473 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,324 software developers and data experts.

How to insert a string into a char array, (without using pointers) ??URGENT

hello, I'm in a hurry I got an exam monday and I haven't managed to copy a string into and array of characters to treat the letters individually, how do I do that ?thank you, please it's very urgent !!
And I'm a beginner I don't know much about pointers, if anything.
Jun 13 '08 #1
4 10379
oler1s
671 Expert 512MB
You can’t wing your way through programming. If you don’t understand the concepts, you’ll memorize certain actions, which is more or less useless in programming.

I haven't managed to copy a string into and array of characters to treat the letters individually, how do I do that
You don’t mention the language (because C++ has a C++ string versus a C string). Not that it’s relevant. If you have a string, it’s an array of sorts. A C string is an array. A C++ string doesn’t need to be copied into an array. You can index the elements of the string.
Jun 13 '08 #2
Ok, the language is C++, and it's not that I don't understand a thing simply I'm a beginner I haven't got much experience, the thing is I have no trouble imagining the algorithm, but it's writing the program that I find sometimes difficult; the syntax, I'm not trying to wind my way, I really try to improve myself.
What I asked is how to copy a string,i.e. a sentence into a array of individual characters and by searching the forum I've found the c_str thing, and now it's ok, but I'd like to improve and copy strings with spaces, because when I type a string with spaces, I only get the first part in the char array.
Can I use the getline function, if yes how in this context.
Thank you very much.
Jun 13 '08 #3
Oler1s, you told me I can index a C++ string, how if I want to count the number of "a"s in a sentence ?
Thank you.
Jun 13 '08 #4
oler1s
671 Expert 512MB
What I asked is how to copy a string,i.e. a sentence into a array of individual characters and by searching the forum I've found the c_str thing
I hope you realize that c_str is owned by the string. That is, you cannot modify it, delete the array. Furthermore, the string only exists as long as the string exists, and remains unchanged

but I'd like to improve and copy strings with spaces, because when I type a string with spaces, I only get the first part in the char array.
Can I use the getline function, if yes how in this context.
First, aren’t you storing the input in C++ strings (vs. char arrays)? Regardless, yes, you need to use getline. There’s two forms of getline. The first is the global getline from <string>. This operates on C++ strings. The second is the stream getline (e.g. cin.getline) that works with C strings. Please look up examples online, and then ask if you have any trouble understanding them.

you told me I can index a C++ string, how if I want to count the number of "a"s in a sentence ?
There’s about three ways to do this. One might be to index said string like an array (e.g. for string input, input[0], input[1], etc.). Then each element indexed is a char you can compare to ‘a’.

One C++ ish way to do would be to iterate over the string with string::iterator, checking the value to see if it were ‘a’. But what I would do is use count_if. count_if is in <algorithm>.

Actually, there's more ways to do this, but iterating with string::iterator or count_if may be the most straightforward C++ ways to do it.
Jun 14 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

26
by: Kai Jaensch | last post by:
Hello, i am an newbie and i have to to solve this problem as fast as i can. But at this time i don´t have a lot of success. Can anybody help me (and understand my english :-))? I have a...
28
by: Davy | last post by:
Hi all, I found char x={"my"}; can be compiled. But char x; x={"my"}; can not be compiled.
1
by: Chris Fink | last post by:
I am receiving xml documents from a customer without a reference to a doctype. I know what the Doctype DTD should be need to insert the declaration as follows <?xml version="1.0"...
7
by: al | last post by:
char s = "This string literal"; or char *s= "This string literal"; Both define a string literal. Both suppose to be read-only and not to be modified according to Standard. And both have...
22
by: jacob navia | last post by:
A function like strcpy takes now, two unbounded pointers. Unbounded pointers, i.e. pointers where there is no range information, have catastrophic failure modes specially when *writing* to main...
4
by: Simon Schaap | last post by:
Hello, I have encountered a strange problem and I hope you can help me to understand it. What I want to do is to pass an array of chars to a function that will split it up (on every location where...
5
by: cdg | last post by:
Could anyone explain how to write this sample program correctly. I need to convert an integer to a string. However, I would prefer to convert the integer to char array. But I didn`t want to use...
9
by: anachronic_individual | last post by:
Hi all, Is there a standard library function to insert an array of characters at a particular point in a text stream without overwriting the existing content, such that the following data in...
19
by: Adam | last post by:
Hi, I'd like to return an (arbitrary length) string array from a function so that after calling the array I've got a list of strings I can access. After much perusing of the internet I found a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.