![]() |
|
[C++][Question] Copying a string to another string without libraries like string.h - Printable Version +- Sinisterly (https://sinister.li) +-- Forum: Coding (https://sinister.li/Forum-Coding) +--- Forum: C, C++, & Obj-C (https://sinister.li/Forum-C-C-Obj-C) +--- Thread: [C++][Question] Copying a string to another string without libraries like string.h (/Thread-C-Question-Copying-a-string-to-another-string-without-libraries-like-string-h) Pages:
1
2
|
[C++][Question] Copying a string to another string without libraries like string.h - OversouL - 09-12-2015 So I wrote this code, but after copying string 1 to string 2 it outputs extra characters depending on the size of my array and how many characters I put in my string. My question is why? Code: #include <iostream>
using namespace std;
int main(){
char str_1[1000], str_2[1000];
cout << "Input String 1: "; cin >> str_1;
cout << "\nCopying string 1 to string 2....\n";
for(int i = 0; str_1[i] != '\0'; i++){
str_2[i] = str_1[i];
}
cout << "Value of string 2 is " << str_2 << endl;
return 0;
}With the current code, if I put a string of 8 characters to string 1 it outputs the string of string 2 (after string 1 is copied to string 2), with a 1 at the end, but everything copies fine with 9 characters or less except 8. Haven't tested it thoroughly, but if I minimize the array to 128 it just doesn't work right. Someone please explain.
RE: [C++][Question] Copying a string to another string without libraries like string.h - OversouL - 09-12-2015 Bumping this, just incase I still don't get replies when I wake up. Really interested in knowing what's causing the extra characters. RE: [C++][Question] Copying a string to another string without libraries like string.h - Coca. - 09-12-2015 It's most likely your boolean expression in your for loop. Try changing the != to ==. Also, why not use the string library? Honestly, it's a lot better than having to deal with character arrays. RE: [C++][Question] Copying a string to another string without libraries like string.h - dotcppfile - 09-13-2015 Oh well, you got things wrong mate. I won't just give you the answer, I'll go for a simple question and I hope you'll answer it seriously: What does cout do exactly? And when does it stop reading what's in an array? If you know how to answer this question then you know how to solve this stupid mistake in your code, and if not then I'll give you the answer and hope that you can go back and read your c++ ebook (or whatever) before you move on with this. As for your code then it's bad anyways but you're a beginner and you'll learn how to make it better in the near future. As for all of you l337 c++ coders out there then don't just go ahead and answer my question, I asked him and he should think about it so calm your titties lol. Also, @Coca. everything you said is wrong, no offense but you fucked up too lol. RE: [C++][Question] Copying a string to another string without libraries like string.h - 0xDEAD10CC - 09-13-2015 Main issue is here: Code: str_1[i] != '\0'Null-terminator is not being copied to the destination string, and you don't zero-initialize the buffer beforehand either so after the valid data, all that remains in the buffer after the actual string data is garbage. (09-12-2015, 10:57 PM)Coca. Wrote: It's most likely your boolean expression in your for loop. Try changing the != to ==. Also, why not use the string library? Honestly, it's a lot better than having to deal with character arrays. You realize that the for loop will probably just end up being *skipped* by doing this unless the very first character begins with a null-terminator? You'd be copying null-terminators and null-terminators only to a destination buffer... :\ What he wants is most likely a do-while loop. Code: #include <iostream>
#define MAX_BUF 1000
int main()
{
char str1[MAX_BUF], str2[MAX_BUF];
std::cin >> str1; // whitespace delimited
int i = 0;
do
{
str2[i] = str1[i];
if (!str1[i]) break;
} while (++i < MAX_BUF);
std::cout << "str2: " << str2 << '\n';
}As for functions within the <string.h> header though, they already exist to simplify these kinds of operations. You can also use memory copying/moving functions if you're smart enough with them. RE: [C++][Question] Copying a string to another string without libraries like string.h - OversouL - 09-13-2015 (09-13-2015, 01:43 AM)dotcppfile Wrote: Oh well, you got things wrong mate. cout outputs str_2 and it stops reading what's in an array if str_1[i] is the null terminator. (09-13-2015, 06:28 AM)0xDEAD10CC Wrote: Main issue is here: Thanks, but I still don't quite get some of what you said, except for the null terminator not being copied part. Can you please explain what (!str[i]) does exactly? Also what do you mean that I don't zero-initialize the buffer? EDIT: Oh I get the !str[i] part now. The null terminator is kind of like a false value right? And putting a not operator makes the value true and therefore executing the if statement. Am I right? RE: [C++][Question] Copying a string to another string without libraries like string.h - dotcppfile - 09-13-2015 Just keep answering my questions mate: how does this work: for (int i = 0; i < 5; i++) cout << i Where does it start and when does it end? RE: [C++][Question] Copying a string to another string without libraries like string.h - OversouL - 09-13-2015 (09-13-2015, 12:03 PM)dotcppfile Wrote: Just keep answering my questions mate: The loop will continue and stops when i is equal to 5. Starts at 0 ends at 4. I get it, the null terminator isn't copied to the 2nd string. But why does it output extra random chars at the end of the string if there is no null terminator? [C++][Question] Copying a string to another string without libraries like string.h - dotcppfile - 09-13-2015 True, the null terminator isnt copied. Now just like you said, cout will stop when it finds a null terminator but it isn't finding any instantly, it just keeps outputting random garbage found in Memory. RE: [C++][Question] Copying a string to another string without libraries like string.h - OversouL - 09-13-2015 (09-13-2015, 12:27 PM)dotcppfile Wrote: True, the null terminator isnt copied. Oh so that's what's happening. Although it's not going infinitely, does that mean, cout has found a null terminator even though it wasn't copied over? |