-3

Hello i have the following sentence which is in a string and in one Line but very long:

l-s-s---s-l---s-s-s-------s-l---l---l-s-s-s-s-l---l-l-s-s---s---s-s---l-s-l-s---s-s-s-s---s---l-s-------l-s-l-l-s---s-l-l-s-l-s---l-s-l-l-s-l-------     

(Its in Morse Code) The --- (3x-) seperates the letters and ------- (7x-) seperates the word. How can i cut the very long code in words.

I've tried the following:

    size_t posWordNext{};
    size_t posWordPre{};

    while (true) {
        posWordNext += code.find("-------");
        if (posWordNext >= code.size()) {
            break;
        }
        cout << code.substr(posWordPre, posWordNext) << endl;
        posWordPre = posWordNext;
    }

This is the output:

l-s-s---s-l---s-s-s
-------s-l---l---l-s-s-s-s-l---l-l-s-s
s-s-s-s-l---l-l-s-s---s---s-s---l-s-l-s---s-s-s-s---s---l
---s---s-s---l-s-l-s---s-s-s-s---s---l-s-------l-s-l-l-s---s-l-l-s-l-s---l-s
Marek R
  • 32,568
  • 6
  • 55
  • 140
Nexxizz
  • 25
  • 5
  • 1
    You tried it and what happened? There are often very good debugging hints in the output. – user4581301 Jul 19 '21 at 18:07
  • You never change `code`. Do you expect `code.find("-------")` to return a different result in each iteration of the loop? – Drew Dormann Jul 19 '21 at 18:08
  • what should be an output? What should happen if input is incorrect (for example 6x`-`). – Marek R Jul 19 '21 at 18:09
  • Have you read the documentation for [`std::string::find`](https://en.cppreference.com/w/cpp/string/basic_string/find)? There are some interesting overloads and defaulted parameters that you can play with. – user4581301 Jul 19 '21 at 18:09
  • This is the output: l-s-s---s-l---s-s-s -------s-l---l---l-s-s-s-s-l---l-l-s-s s-s-s-s-l---l-l-s-s---s---s-s---l-s-l-s---s-s-s-s---s---l ---s---s-s---l-s-l-s---s-s-s-s---s---l-s-------l-s-l-l-s---s-l-l-s-l-s---l-s but in comments it dont shows the newlines sorry – Nexxizz Jul 19 '21 at 18:16
  • Please edit your post with the output from your program. It's not useful in the comment. – Thomas Matthews Jul 19 '21 at 18:17
  • You can start with something like this: https://godbolt.org/z/a1Eodac6x – Marek R Jul 19 '21 at 18:20
  • @MarekR i want first to seperate the words then the letters and then convert them. is there a way with find and substring? – Nexxizz Jul 19 '21 at 18:23
  • @DrewDormann i thought when i find the position of the delimiter (-------) I can cut with substring the word between the posWordPre which is the position of the last find and posWordNext which is in the position of current? – Nexxizz Jul 19 '21 at 18:39
  • @Nexxizz step through your code in a debugger. You will see that `std::string::substr` does not change the underlying string. You will also see that the second parameter to `substr` is not a position, it is a length. – Drew Dormann Jul 19 '21 at 18:48

1 Answers1

1

Every time you call code.find(), you are searching from the beginning of the string again. You are not modifying code on each iteration, so find() will return the same offset each time. You should be passing posWordPre to the 2nd parameter of find() as a starting offset to begin searching from.

Also, when you are calling code.substr(), you are treating the 2nd parameter as an offset to stop at, but that parameter actually expects a character count instead of an offset.

Because of these mistakes, you are chopping up the code string at the wrong offsets.

Try something more like this instead:

size_t posWordPre = 0, posWordNext;

while ((posWordNext = code.find("-------", posWordPre)) != string::npos) {
    cout << code.substr(posWordPre, posWordNext - posWordPre) << endl;
    posWordPre = posWordNext + 7;
}

if (posWordPre < code.size())
    cout << code.substr(posWordPre) << endl;

Online Demo

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770