Copying a string literal in a char array to a different char array

Started by
37 comments, last by Atrix256 13 years, 3 months ago
Finally decided to pick up programming for real, so I've been doing it for two weeks now (although last week I didn't get much done due to Christmas and stuff). The language is C++ and I'm using Xcode on my MacBook.

I have to "write a program that reads words from the keyboard to a char array. The program should then copy the text string in that char array to another char array". I'm not allowed to use strcpy.

const int max = 100;char myArray[max];string myString;	getline(cin, myString);int size = myString.size();	for (int i = 0; i < size; i++)   myArray = myString;


I'm not sure what code I should be using to copy the string to another array. In fact, I'm not entirely sure I'm doing this right, since my string is stored in a string object, and then I put each char in that string in a char array.

for (int i = 0; cin.get(c); i++)   myArray = c;


Above code would be reading each char and store it directly in an array, but I don't know how to break this for-loop. If I wanted to break it once it reads a space character then I'd declare a variable char space = ' '; and then make something like: if (cin >> space) { break; } but that doesn't work either. Most likely because it reads one char at a time and pressing ENTER to execute the program would count as a space.


So, in short: How do I directly store what I type in in an array?

Thanks for any and all help!

[Edited by - Metallon on December 29, 2010 2:40:53 AM]
Advertisement
Before trying to answer your other questions, I have a question about this:
Quote:"write a program that reads words from the keyboard to a char array. The program should then copy the string literal in that char array to another char array".
How can words 'read from the keyboard' (presumably using std::cin) be in the form of a string literal? As far as I know, in C++, the term 'string literal' refers to a string that appears in double quotation marks in your code (there's more to it than that, of course, but that's the basic idea). So it's not immediately clear to me why text read from an input device at run time would be referred to as a string literal.

Where is this assignment from?
Well you should be using StringBuilder not String to grab the input from the keyboard.

Beginner in Game Development?  Read here. And read here.

 

Ugh, translation error, and it's 1 AM.

Let me rephrase. I'm supposed to type a word or many words. Whatever I type in should be stored in a char array. Then I need to copy whatever I typed in from that char array to a different char array.

The "problem" is from a book called C++ Direkt, it's a Swedish book.
Quote:Original post by Alpha_ProgDes
Well you should be using StringBuilder not String to grab the input from the keyboard.
I believe the language in question is C++.
My bad. It's StringStream in C++, ain't it.

Beginner in Game Development?  Read here. And read here.

 

It should be doable without stringstream though, right?
A lot of people will (correctly!) tell you that using char arrays to store strings is a bad idea.

This is because its really easy to make mistakes that cause major problems in your application like memory corruption, hard to debug intermittent crashes, and problems in unrelated areas of code.

But, since the book is giving you an assignment to use char arrays, that's sort of different than you going out of your way to use them (you should learn them eventually anyhow!).

Anyways...

Yes, its totally doable without a string builder and without strcpy.

how char * strings work (and how String strings work behind the scenes) is that they have 0 or more characters for the string and then a null character to mark that you've reached the end of the string. The null character is just literally a 0. It isn't '0' (the character 0), it's actually just 0 like this:

char cNull = 0; //this is the null character

So to copy a string from one array to another, you first make sure you have memory enough to store a copy of the string (it should be strlen(sourceString)+1 in size since strlen returns the length without the null character).

Next up, you loop through the source string starting at index 0 until you reach the null character in the source string.

For each iteration of the loop, copy the current positions character from the source string to the destination string.

Lastly, after the loop, make sure and put a null character at the end of your copy if you haven't already handled that.

Here's a little example to help you out!

const char *srcString = "This is a string";char destString[100];int nIndex = 0;do{  destString[nIndex] = srcString[nIndex];  nIndex++;}while(srcString[nIndex]);


There's plenty of stuff you'd wand to do in "the real world", like making sure srcString wasn't null before copying, and making sure destString was large enough to hold the source string, but hopefully this should give you an idea.

Also note that the loop above copies the null character so you don't need to explicitly add a null to the end of destString.

HTH!
Quote:Original post by Metallon
It should be doable without stringstream though, right?
By my understanding of the question, you aren't supposed to be using std::string either.

The exercise is to read characters from stdin (hint: use std::cin.get() to read a character), and store them in a char array (not a string). Once you have read in a newline character (indicating that you are done reading), you need to copy the contents of the first char array into a second char array (one character at a time).

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

I agree swiftcoder, it sounds like the assignment wants him to work only with char arrays, not using any of the helper string objects or functions at all so that he knows the basics of how C strings work

This topic is closed to new replies.

Advertisement