I will try to formulate the condition of the problem: the given string str1: "A roza upala, na lapu + Azora!" divided into individual words using the strcspn and strspn functions, the first one counts the number of elements of the string str1 not included in the string str2: ",.!? + -" (delimiter string) until elements from str2 appear in str1. I used the second function to determine the element on which the pointer stands, the separator or not.

сhar str1[] = "++A roza upala, na lapu Azora.! "; char str2[] = " +-.,!?"; char *word = new char [15]; char **ptrArray = new char*[20]; int k, n, j, i = 0; char *pstr1 = str1 , *pfun; while(pstr1 < str1 + strlen(str1)) { n = strspn(pstr1,str2); if (n!= 0) pstr1++; else { k = strcspn(pstr1, str2); pfun = pstr1 + k; //заполнение указателя j = 0; for (char *pout = pstr1; pout < pfun; pout++) { *(word+j) = *pout; j++; } *(word+j) = ' '; //заполнение массива указателей указателями на слова ptrArray[i] = word; delete[]word; // очистка pstr1 = pfun + 1; i++; // кол-во указателей(слов) + 1 } } //cases for output if (i < 7) { for (int m = i-1; m >= 0; m--) cout << ptrArray[m]; } else { for (int m = 0; m < i; m += 2) cout << ptrArray[m]; } //cleaning all up for (int ii = 0; ii < i; ii++) delete[] ptrArray[ii]; delete[] ptrArray; 

It is necessary to form a line of words separated by spaces in the reverse order, if there are less than seven words, otherwise every second word. And display on the screen. In my solution, I used dynamic selection, first for a separate word (string), and then for an array of strings. The WORD pointer is needed to allocate memory for each new word (string). Perhaps the problem is here:

  for (char *pout = pstr1; pout < pfun; pout++) { *(word+j) = *pout; j++; } *(word+j) = ' '; //заполнение массива указателей указателями на слова ptrArray[i] = word; 

At the end of the program I try to organize the output I need through pointers to pointers to words (lines).

 if (i < 7) { for (int m = i-1; m >= 0; m--) cout << ptrArray[m]; } else { for (int m = 0; m < i; m += 2) cout << ptrArray[m]; } 

Help, who can! (I will be grateful)

  • What's the problem? That you can not do? - Victor Gorban
  • Well, the program does not work)) And do you think that everything is written correctly? I am particularly interested in the conclusion - Okunev Pasha
  • "The program does not work." How not to work? Does not compile, crashes with an error, the wrong data is output to the console, no data is output to the console, damaged data is output to the console, garbage is output to the console? - Victor Gorban
  • I don't like it here: ptrArray [i] = word; delete [] word; // clear I don’t understand why you are deleting a word? In the first line, you copied a pointer to the memory where the word array is stored. And in the second line you say that this area of ​​memory is no longer needed, and you can occupy it with other data. Tried not to delete word in that line? - Victor Gorban
  • Well, how ?? I create a dynamic array of pointers with pointers to each word. Each word needs its own pointer (word, as you understand). It is also dynamic (well, I understand why) And I clean it so that later for the new word there is a new pointer (maybe I understand the wrong dynamics). {PtrArray [i] = word} - I assign a pointer to a word to an element of the array of pointers (everything is just like). On the vyzhl did not give anything, but on the codeblocks incomprehensible characters and compiled with interrupts). When removed delete [] word - on view was executed with interruptions - Okunev Pasha

1 answer 1

Pointers are a topic that requires precise understanding. In such cases, when I do not know exactly where the error is, I do this: 1) I break the program into smaller pieces (in your case by cycles) 2) I write down how, in my opinion, these pieces should be performed (what data should get). 3) compare with what happened.

Yes, and incomprehensible characters just say that you refer to the memory where the garbage is. Do you know how to use a debugger? Add all the variables that interest you to the watches window, and watch how they change with each step. Notice that something is not as expected - look for a mistake.