0

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 print rain.
  • 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 remove p, and print telehone.

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!

Anirban166
  • 3,162
  • 4
  • 11
  • 27
  • Tip: Avoid `using namespace std`, it's a bad habit to get into. The `std::` prefix helps separate your code from the Standard Library. Also pass in as `const` references whenever possible, this avoids pointless copies of data, like `const std::string& s`. You're also needlessly mixing C strings, `std::string`, and have a bunch of global variables in the mix. Use C++. Use `std::string`. Don't use globals. – tadman Nov 05 '20 at 06:53
  • You're never calling your `removeMiddle` function. The function have not named argument. The variable `s` is not a `std::string` object, which means it doesn't have member functions like `erase`. – Some programmer dude Nov 05 '20 at 06:53
  • Tip: If a function returns something, *`return` something*. – tadman Nov 05 '20 at 06:53
  • There are many steps involved, determining whether a string is odd or even, extracting the middle character, extracting the data before and after. Which of these gives you problems? BTW: One mistake is your use of char arrays, use std::string. Also, don't bother with input, hardcode a number of test strings (lengths 0, 1, 2, 3, 5 at least) and process them in turn (like Test Driven Development). As a new user here, also take the [tour] and read [ask]. – Ulrich Eckhardt Nov 05 '20 at 07:03

1 Answers1

0

In your code you never called the removeMiddle function and removeMiddle functions is not returning anything even though its return type is string. Here's the right code:

#include<bits/stdc++.h>
using namespace std;
int main(){
    string str;
    cin>>str;
    if(str.length()%2==0){
        cout<<str;
    }
    else{
        int r = str.length()/2;
        str.erase(r,1);       //string& string ::erase (idx,len);    
                              //Erases at most, len characters starting from index idx;
        cout<<str;
    }
    return 0;
}

We first checked if the length of the string is even, then print the string, else we used the erase function to remove the middle character.

Saniya
  • 113
  • 6
  • [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) Please don't promote bad habits. – Some programmer dude Nov 05 '20 at 07:10
  • Avoid using the [`bits/stdc++`](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) header. Use only the required libraries. – Anirban166 Nov 05 '20 at 07:10
  • Thank you very much. This was easier than I thought. I over thinked it. Cheers! Tho I don't know about this library, i'll look into it. – alexander123 Nov 05 '20 at 07:12
  • @alexander123 Please get [some good C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) to learn from, and perhaps even take a few classes. What you `#include` is *not* a "library". And as mentioned, this specific header file should be avoided. – Some programmer dude Nov 05 '20 at 07:21
  • That's what I learned in school...I don't like c++. I'm reading Automate the boring stuff with python. This was a mandatory thing to do in c++, otherwise i wouldn`t had asked. Oh, python is much easier... – alexander123 Nov 05 '20 at 07:28