I am trying to rewrite this code written in C ++ in C (SI). The code does the following: reads str2 if it finds * reads str1 from the position * to the nearest < then adds ,
Result: Test_Value, 564, Test_Value2,456, Test_Value3,123,
int main() { std::string make_string{}; std::string final_string{}; std::string str1("<tr> <th>Test_Value</th> <th>564</th> </tr><tr> <th>Test_Value2</th> <th>456</th> </tr><tr> <th>Test_Value3</th> <th>123</th> </tr>"); std::string str2("<tr> <th>*</th> <th>*</th> </tr><tr> <th>*</th> <th>*</th> </tr><tr> <th>*</th> <th>*</th> </tr>"); size_t i = 0; i = str2.find("*", i); int position = i; auto count = 0; while (true) { while (str1[position] != '<') //читаем от позиции * до позицици < { make_string += str1[position]; // сохранить в переменную position++; // увеличить позицию на 1 } // повторить цикл i = str2.find('*', i + 1); // найти позицию следующей звезды if (i > str1.length()) // если звездочек больше нет, выйти из цикла break; final_string += make_string + ','; //записать в основную строку и поставить запятую make_string.clear(); // очистить временную строку count += 2; position = i + (final_string.length() - count); // вычисляем позицию для следующей итерации так: позиция следующей звездочки известна (20) прибавим длину записанных символов и вычтем * и , } final_string += make_string; cout << final_string << endl; return 0; } Redid it like this for SI.
#include <stdio.h> #include <string.h> int main(void) { char *make_string = NULL; char *final_string = NULL; char *str1[] = "<tr> <th>Test_Value</th> <th>564</th> </tr><tr> <th>Test_Value2</th> <th>456</th> </tr><tr> <th>Test_Value3</th> <th>123</th> </tr>"; char *str2[] = "<tr> <th>*</th> <th>*</th> </tr><tr> <th>*</th> <th>*</th> </tr><tr> <th>*</th> <th>*</th> </tr>"; size_t i = 0; char *ptr = strchr(str2, '*'); i = ptr - str2; // нашли позицию * int position = i; int count = 0; for (ptr = str2; *ptr != '\0'; ptr++) { // читаем строку посимвольно передвигаем указатель if (*ptr == '*') { // нашли звезду -> установили указатель на позицию * ---> } memset(&make_string, '\0', sizeof(char*)); //make_string = ""; } strcat(final_string, make_string); //final_string += make_string; printf("%s\n",final_string); return 0; } Questions: ---> here you need to read from position * to <in str1.
How to move the pointer to the same position where it is in str2?
strchrreturns a pointer, not a position (i.e. it needs to be recalculated). The star after while can be searched for by him. Naturally, if you accumulate characters inmake_string, thenreallocneeded, but the fact is that you don’t need to accumulate them. It is enough to memorize the positions of the beginning and end of the copied text instr1[], then you can add it to thefinal_string(this is where realloc is needed) (however, if you have an idea of the size of str1, then it may be better to allocate memory for final_string once, in the very beginning). And do not make stupid calls to strcat and strlen inside cycles - avpstrcatin your code, you already know that you cannot concatenate in the C line with+or+=. Why then in the loop do you still have some sort ofmake_string += ...? - AnT