I have to make a console program that will print:
- If the string is even, all the characters - let's say I have
rain
, it will printrain
. - If the string is odd, it will remove the middle character, and then print the remaining ones - let's say I have
telephone
, it will removep
, and printtelehone
.
I've searched the StackOverflow forum for 3 hours but I couldn't solve this. This is my try:
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
char s[21], x[21];
int j;
int main()
{
cin.get(s, 21);
if(strlen(s) % 2 == 0)
strcpy(x, s);
cout << x;
return 0;
}
string removeMiddle(const string)
{
int str = strlen(s);
bool isOdd;
if(str % 2 != 0)
isOdd == true;
if(isOdd == true)
{
j = str / 2;
}
s.erase (j+1, 1);
cout << s;
}
I've tried a lot of snippets and this is the best I can do. Thanks guys!