Print a middle character of a string

I'm working a problem that returns a string containing the character in str if the length of str is odd, or the two middle char if the length is even.


here is what i got, and i just dont no how to call for the function. help!

#include <iostream>

#include <string>

using namespace std;

string middle(string str)

{



int i = str.length();


if (((i = str.length() / 2) % 2) == 0)

{

cout << str[i - 1];

}

if (((i = str.length() / 2) % 2) >= 1)

{
cout << str[i];
}

return str;

}

int main()


{
string input;

cout << "Enter a word to print the middle character: ";

cin >> input;

int i = input.length();

string result = middle(i);

cout << "The middle char is: " << i << endl;

system ("pause");

return 0;

}
You actually are already calling the function. However, you are giving it i which is an int. You should give it a string. Also, you then output i when you should output result:
1
2
3
4
5
6
7
8
9
10
11
12
int main()
{	
	string input;
	cout << "Enter a word to print the middle character: ";
	cin >> input;

	string result = middle(input);

	cout << "The middle char is: " << result << endl;

	return 0;
}


There are also a few issues with your function. If you need help once you get it integrated let us know.
Thanks for your help stewbond, i have change the function a little bit but it still would not work. Please let me know where i did wrong.

if ( i % 2 == 1)

{

cout << str.substr((i/2), 2);

}

if (i % 2 == 0)

{
cout << str.substr((i/2), 1);
}

return str;

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
using namespace std;

string middle(string str) {
	int i = str.length();

	if ((i % 2) == 0) {
		return str.substr(i/2 - 1, 2);
	}
	return str.substr(i/2, 1);
}

int main() {
	string input;

	cout << "Enter a word to print the middle character: ";
	cin >> input;
	string result = middle(input);

	cout << "The middle char is: " << result << endl;
	main();
	return 0;
}
Do not call main() in C++ code.

It is forbidden by the C++ standard, though some compilers will allow you to do it.

In addition, it can lead to dangerous recursion/memory leaks.

Use a while loop or something similar if you want to be able to enter additional words.
Really? OK, now I will know.
How about this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <string>
using namespace std;

string middle(string str) {
	int i = str.length();

	if ((i % 2) == 0) {
		return str.substr(i/2 - 1, 2);
	}
	return str.substr(i/2, 1);
}

void second_main() {
	string input;

	cout << "Enter a word to print the middle character: ";
	cin >> input;
	string result = middle(input);

	cout << "The middle char is: " << result << endl;
}

int main() {
	while (true)
		second_main();
	return 0;
}
I would suggest:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <string>
using namespace std;

string middle(string str) {
    int i = str.length();

    if ((i % 2) == 0) {
        return str.substr(i/2 - 1, 2);
    }
    return str.substr(i/2, 1);
}

int main() {
    //will loop indefinitely, ctrl+c or equivalent to escape
    while(true)
    {
        string input;

        cout << "Enter a word to print the middle character: ";
        cin >> input;
        string result = middle(input);

        cout << "The middle char is: " << result << endl;
    }

    return 0;
}


Of course, then you have the problem that there's no way to stop the program, except for Ctrl+C, but your previous program had that issue too.

If you want to correct that, consider using a do-while loop and exiting if input is "exit" or something like that.

The original poster didn't mention needing a loop, so your original code with the call to main() removed should work fine.
I added main() in because I did not wonted to run program again if I wonted to test another string. And left it in.
You can write your function middle without using if-statement.:)

1
2
3
4
5
6
std::string middle( const std::string &s )
{
	std::string::size_type j, i = ( j = s.size() / 2 ) % 2 == 0;
	
	return ( s.substr( j - i, i + 1 ) );
}


Sorry, I am wrong. It is invalid code. The correct code will look

1
2
3
4
5
6
7
8
9
10
std::string middle( const std::string &s )
{
	std::string::size_type i, j;

	j = s.length();
	i = j % 2 == 0;
	j /= 2;
	
	return ( s.substr( j - i, i + 1 ) );
}

Last edited on
Thanks everyone for the help, i got it!!
Topic archived. No new replies allowed.