My idea. I break the string into words before the space, then I check the resulting string for the content of the given letter, if there is a letter in the string, I print the string. The question in the debugger I watched my program work until 1st nullptr and then stops working, what should I do?

#include <iostream> #include <cstdio> using namespace std; char *find_word(char *str, const char ch); int main() { setlocale(LC_ALL, "RUSSIAN"); char str[100] = "skate world sun take"; //набор слов. cout << str << endl; char *word = strtok(str, " "); //получаю новую строку while (*word) { cout << find_word(word, 'k') << ' '; word = strtok(NULL, " "); } cout << endl; return 0; } //проверка на содержание буквы. char *find_word(char *word, const char ch) { char *start = word; while (*start) { if (*start == ch) return word; start++; } return 0; } 
  • All that you wrote above is done simply by 'std :: string sub = "k"; size_t pos1 = str.find (sub, 0); ' - Alex.B
  • I study less than half a year with ++ and did not reach STL libraries - Ilya
  • so what to do))) Bjarne will teach))) - Alex.B

1 answer 1

Your program is full of logical errors. To understand further, you need to eliminate them. Without this, the answer to your question is tantamount to rewriting the code.

  1. The algorithm described by you in words is incorrect. After the last word there is no space, so you will not find it. In addition, if there are 2 spaces in a row and more, the algorithm will in vain check empty words (although this is not an error).
  2. You have created a function that should search for a letter in a word, but it will not look for it in a word, but in the entire phrase. Why? Because strtok returns the INDEX to the place where the word begins, and you look for the letter k through this pointer, that is, you actually run through the str array to the end.
  3. The while loop is generally fake. Why in it to search for your word in empty line (NULL)? In theory, you need to move on to the next lexeme, and this is not done at all. The condition for exiting the loop is also incorrect - you need to exit at the null pointer, and not at the null character.

First you need to correct these errors, then you can think further.

  • Thanks for the info, I'll go figure it out - Ilya
  • As I understand the pointer is part of the string ??? - Ilya
  • Hmm ... if you don't speak the language, you should first learn it. The pointer is (in this case) the actual address of the character position. It only indicates the place in your str, from which the word begins. - Zealint
  • I want to clarify about the 2nd paragraph. Maybe then it will be easier to create a temporary array that will store the word and then check it for the content of the character. - Ilya
  • No no need. Simply, I would go quietly along the str array and depending on the character encountered, I would perform this or that operation. If I see a space or end of a line - the word ends, if I see the letter 'k', then I consider the current word to be appropriate. You can store a pointer to the beginning of the word and its end, then it will be easy to display it. - Zealint