It looks like a simple task, but during the solution there were questions.

Given a string that you want to copy to a new and add points. Using a pointer.

No sooner said than done.

I pass 2 lines as parameters, one filled, the other to which I will copy.

Initializing an empty string.

Using the while go through the line and copy the words into a new line and add symbols using condition operators.

But the result is just an empty string. The compiler persistently sees an empty string. I cycle through all the allocated memory for a line and assign them to a new line.

Perhaps lost links somewhere. Step by step, again, the pointer to the filled line sees everything perfectly, but does not want to copy the second one.

 #include "stdafx.h" #include <iostream> #include <cstdio> #include <string.h> using namespace std; void copystring(char* str, char* str1) { char* in_str = str; char* out_str = str1;//используем указатели *out_str = ' '; ++out_str; while (*in_str == ' ') { if (*in_str == '.') { *out_str = '.'; break; } if (*in_str != ',') *out_str = *in_str; else { *out_str = '\.'; ++out_str; *out_str = ' '; } ++in_str; ++out_str; } cout << out_str << endl; } int _tmain(int argc, _TCHAR* argv[]) { setlocale(LC_ALL, "Russian"); char str[100] = {"one two three four five"}; char str1[100] = " "; cout << "Исходная строка" << endl; cout << str << endl; cout << "Полученная новая строка" << endl; copystring(str, str1); system("pause"); return 0; } 

The most interesting thing is that if everything is done in main , then everything changes correctly.

But it is necessary separately through procedures, therefore and we implement through the procedure.

  • 2
    and what exactly do you want to lead? .. out_str points to the last character, isn't it? Try to display str1 . - pavel
  • The result is the same. Empty line. Changed cycles, for example, wrote while (1) but an exception was already thrown there. In the while loop, I think we’re going right until the end of the line. - beginner
  • @nick_n_a, don't be silly. He allocated memory for the arrays in the stack, and until _tmain execution ends, it will not go anywhere. - yrHeTaTeJlb
  • Memory is ok, almost. ASCIIz can be broken, but the main thing is cout<<"Полученная новая строка"<<endl; copystring(str,str1); system("pause"); cout<<"Полученная новая строка"<<endl; copystring(str,str1); system("pause"); and where is the str1 display ? - nick_n_a
  • @yrHeTaTeJlb is not good at all, it will always be empty, because up to this line a ++ out_str shift has been made . Here is the str1 pointer left, fortunately the author - untouched - you can withdraw it - this will be the right decision. - nick_n_a

1 answer 1

You have a few mistakes.

  1. while(*in_str== ' ') leads to the fact that as soon as the character of the input string is not a space, the function terminates. This is clearly not what you wanted. Here you need to check while(*in_str) .

  2. At the end of the work, you do not write the terminating null character to the output string.

  3. You need to output the string pointed to by str1 , since out_str points beyond the end of this line.

  4. On a trifle: it is not necessary in the same function and to transform a line, and to deduce. A function is a logically complete piece ...

PS I didn’t understand the logic of your replacements, but it seems to me to be somewhat wrong in a casual glance ... But since you didn’t write what should be done there, it is already on your conscience :)