15,964,188 members
See more:
I need the shortest possible c++ code to solve this problem, with an explanation of each line of code. Here is the task: the word is given. If its length is odd, then delete the middle letter, otherwise - two middle letters.

What I have tried:

I found solutions to this task on the Internet, but the solutions are huge and without explanation, and I need to learn it and it should be as short as possible in c++ code with an explanation of each line of code. So I found solutions:

C++
```#include <iostream>
#include <cstring>
int main(){
char str[1000];;
std::cin.getline(str, 1000);
int n = strlen(str);
int ser = n / 2 - (n%2==0), i = ser, shift = 1 + (n%2==0); // середина и конец
for (i = ser; i != n - shift; i++)
str[i] = str[i + shift];
str[i] = '\0';
std::cout << str << std::endl;
}```
Posted
Updated 24-Aug-23 14:40pm
v2
Mike Hankey 24-Aug-23 23:39pm
When you learn to play the violin, you learn by practicing.
When you learn to be a carpenter, you learn by doing.
When you learn to program you can look at other peoples code and see how they have done it but basically it comes down to practice.

Do it and you learn, have other do it for and you learn nothing!

## Solution 3

Going backwards, I show you the algorithm, leaving the 'shortest code implementation' in up to you.
Consider the separate cases of odd and even string lengths.
Odd:
`"0123456"->"012"+ "456"`

in this case
```length = 7
quotient = 3
remainder = 1```

The result is given by the concatenation of the two substrings:
```[0..(quotient-1)] + [(quotient+1)..(length-1)]
```

Even:
`"01234567"->"012"+ "567"`

in this case
```length = 8
quotient = 4
remainder = 0```

The result is given by the concatenation of the two substrings:
```[0..(quotient-2)] + [(quotient+2)..(length-1)]
```

For the general case (factoring in the reminder), the result is give by the concatenation of the two substrings:
`[0..(quotient-2+remainder)] + [(quotient+2-remainder)..(length-1)]`

I hope it is correct. Good luck coding!

v2

## Solution 1

Nope. That's not how it works. The only code you get is the code YOU write.

We're not here to do your work for you so you can turn it in and get all the credit.

## Solution 2

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]

And do you have any idea how much work explaining code line by line is?
Every single line needs a paragraph of explanation! For example:
```int next = r.Next();
```

Create a new variable called "next" which can hold a integer value. From the previously declared Random instance "r", call the "Next" method to get a new random number, and assign it to the "next" variable.

Can you imagine how long it would take us to explain even a very short code fragment like your example, line by line?

No. It is not going to happen. If you have a specific problem, then ask a question about it. But think first - would you want to sit down for 45 minutes and type up a line-by-line description for no good reason?

## Solution 4

I would do:

1. strlen
2. decide 1 or 2 letters.
3. memcpy of second half of string
4. terminator

tip: write some tests.