-2

A sentence is given in which words are separated by spaces (one or more). Change each word in the line, removing from it all subsequent occurrences of the first letter of this word (do not change the number of spaces between words).

#include <iostream>
#include <cstring>

using namespace std;
void check(char *input){
char* Letter = strtok(input, " ");
while (Letter){
    char _Letter = Letter[0];

    for (int i = 1; i < strlen(Letter); i++)
        if (Letter[ i ] == _Letter)
            Letter[ i ] = ' ';

    cout << Letter << " ";
    Letter = strtok(NULL, " ");
}
cout << endl;
}
int main(){
    setlocale(LC_ALL , "Ukrainian");
    char *input = new char[100];
    gets(input);
    check(input);
    delete[] input;
    return 0;
}

My code removes the occurrence of the first letter from the word, but does not take into account the spaces between words and leaves one space.Using std :: string is not allowed, you must use a character array with functions from the library <string.h> Help fix the code, please

LLLORIS
  • 1
  • 2
  • Which language are you using, `C` or `C++`? – Galik Nov 22 '21 at 20:30
  • We use c ++ with elements of c – LLLORIS Nov 22 '21 at 20:31
  • 1
    *We use c ++ with elements of c* -- The code posted is not C. – PaulMcKenzie Nov 22 '21 at 20:32
  • I deleted the c label – LLLORIS Nov 22 '21 at 20:33
  • Can you help me? – LLLORIS Nov 22 '21 at 20:34
  • Why not use `std::string` and the various functions that come with `std::string`? – PaulMcKenzie Nov 22 '21 at 20:34
  • We were given the task to use only character arrays without a string – LLLORIS Nov 22 '21 at 20:34
  • 3
    This doesn't address the question, but names that begin with an underscore followed by a capital letter (`_Letter`) and names that contain two consecutive underscores are reserved for use by the implementation. Don't use them in your code. – Pete Becker Nov 22 '21 at 20:35
  • Can you help me solve my problem? – LLLORIS Nov 22 '21 at 20:37
  • Suggestion: If allowed by the assignment use `std::string` for holding the input and then take advantage of the [Erase-Remove Idiom](https://en.wikipedia.org/wiki/Erase%E2%80%93remove_idiom). – user4581301 Nov 22 '21 at 20:41
  • Using std :: string is not allowed, you can use a character array with functions from the library – LLLORIS Nov 22 '21 at 20:43
  • I recommend adding that tidbit to the question to keep all of the important information in one place. – user4581301 Nov 22 '21 at 20:46
  • @LLLORIS -- By not using `std::string`, the problem becomes much more difficult than it needs to be. Have you considered that your current solution doesn't work at all? You are not actually removing the character, instead you are replacing it with a space. That space that you are replacing it with increases the number of spaces between words, and that is not what the assignment says you should do. You literally have to "shift" the entire string one character to the left to compensate for that extra space you put in. – PaulMcKenzie Nov 22 '21 at 20:47
  • 1
    A side note: The documentation for `gets` [has stated "Never use gets()."](https://linux.die.net/man/3/gets) for decades. I recommend updating your reading materials. – user4581301 Nov 22 '21 at 20:48
  • Oh, our dear `strtok()`, a function that is deprecated like a T-Rex, especially in C++'s territory. – digito_evo Nov 22 '21 at 20:50
  • How do I solve this problem? – LLLORIS Nov 22 '21 at 20:51
  • 1
    Another note: if you know the size of an array, don't `new` it. Just allocate an automatic: `char input[100];` so you don't have to worry about the mental overhead of making sure the program passes through a `delete[]` and the runtime overhead of asking the system for dynamic memory. In general, [avoid using `new`](https://stackoverflow.com/questions/6500313/why-should-c-programmers-minimize-use-of-new). – user4581301 Nov 22 '21 at 20:51
  • 1
    @LLLORIS I was going to mention the `gets` call. So your instructions say you must use outdated, obsolete, and functions marked as dangerous? Also, I suggest you draw on paper what is to be done, as it seems you didn't really do this (note that you didn't realize that the character sequence has to be shifted to keep the space count correct). – PaulMcKenzie Nov 22 '21 at 20:51
  • Easiest way to do this is to not attempt to edit the given string. Instead make a new string and only copy over the character that are not to be removed. – user4581301 Nov 22 '21 at 20:53
  • I figured out what I had to do, I just didn't know how to do it with code – LLLORIS Nov 22 '21 at 20:53
  • *I figured out what I had to do, I just didn't know how to do it with code* -- Have two indices, one for the original string, one for the new string -- increment each index depending on whatever condition is met. Again, this could have been visualized if you worked this out on paper first. – PaulMcKenzie Nov 22 '21 at 20:55

1 Answers1

0

traverse character by character, printing the characters you want to keep.
This makes it much easier to maintain whitespace and you don't need to mess around with moving characters around.

Lightly tested sketch:

int main()
{
    bool in_word = false;
    char ignore = 0;
    char line[100];
    gets(line);
    char* c = line;
    while (*c)
    {
        if (!std::isspace(*c))
        {
            if (!in_word)
            {
                in_word = true;
                ignore = *c;
                std::cout << *c;
            }
            else if (*c != ignore)
            {
                std::cout << *c;
            }
        }
        else
        {
            in_word = false;
            std::cout << *c;
        }
        c++;
    }
}
molbdnilo
  • 64,751
  • 3
  • 43
  • 82
  • But I need to use a character array – LLLORIS Nov 22 '21 at 21:02
  • You said that you *can* use arrays, not that you *must*. Please post all your teacher's restrictions in the question. (And ask your teacher why they want you to write code in a way that nobody ever does.) – molbdnilo Nov 22 '21 at 21:04
  • Sorry, I did not fully explain the condition, there must be a character array – LLLORIS Nov 22 '21 at 21:06
  • Here is a version with a character array, and the `gets` that you teacher loves so dearly. (it is almost exactly the same as the previous non-array code, since the approach is the same.) – molbdnilo Nov 22 '21 at 21:42