473,811 Members | 3,424 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

4 New Member
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 10425
oler1s
671 Recognized Expert Contributor
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
Voevoda
4 New Member
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
Voevoda
4 New Member
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 Recognized Expert Contributor
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::iterato r, 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::iterato r 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
808
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 .txt-file in which the data is structured in that way: Project-Nr. ID name lastname 33 9 Lars Lundel 33 12 Emil Korla
28
2133
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
4111
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" encoding="UTF-8"?> <!-- start of add validation tag --> <!DOCTYPE D1XML SYSTEM "http://url/myDTD.DTD"> <!-- end of add validation tag --> <XML.....>
7
4368
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 type of "const char *". Right? But why does the compiler I am using allow s to be modified, instead of generating compile error?
22
2091
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 memory. A better string library would accept *bounded* pointers. We would have then: char *strcpyN(char *destination, size_t bound1, char *src,size_t bound2);
4
8830
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 a * occurs in the string). This split function should allocate a 2D array of chars and put the split results in different rows. The listing below shows how I started to work on this. To keep the program simple and help focus the program the...
5
2698
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 "itoa". And I am not that familar with using "stringstream". And is there better approach to converting integers to char arrays. #include <iostream> #include <sstream> #include <string> using namespace std;
9
3705
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 appropriately moved further down? From a cursory search of the libc documentation I can't find such a function. Will I have to write it myself? Thanks.
19
1844
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 related answer here (by Eric Sosman) which involved creating an array of pointers and using that, so it looks something like:
0
9605
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
10651
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...
1
10403
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7671
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
6893
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5555
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
5693
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4341
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3868
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.