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.
out_strpoints to the last character, isn't it? Try to displaystr1. - pavelcout<<"Полученная новая строка"<<endl; copystring(str,str1); system("pause");cout<<"Полученная новая строка"<<endl; copystring(str,str1); system("pause");and where is the str1 display ? - nick_n_a