Ok this is what I got and the results determine if the string is odd or even but I need to DISPLAY the middle character if even and the middle two if odd
how do I get that?
int main()
{
char line [80];
char* ptr1 = line;
int count = 0;
cout << "Enter a string of charcters. Max length allowed is 80 " << endl;
cin.getline(line, 80);
cout << endl << endl;
cout << "The string you enter was " << endl << line << endl;
while ( *ptr1 != '\0' )
{
++count;
++ptr1;
}
cout << "Your string consisted of " << strlen(line) << " characters" << endl;
if (count % 2 == 0)
{
cout << "The string had an even number of characters" << endl;
}
else cout << "The String had an odd number of characters" << endl;
1. Divide count by 2 and save this number.
2. If you detect an odd number of characters, get the character at the result - 1 of the first step.
3. Else get the two characters at the result - 1 of the first step.
In case you haven't already finished the coding, here's what I came up with.
Thanks for asking about this, as I was then able to learn how to do it as well.
I can see an issue with your code, Nelli. line is uninitialized. Each character of line contains unused memory. When you ask for input, the given characters are stored into the corresponding slot within line. However, when the user enters less than 80 characters, the remaining characters still remain uninitialized. In such an event, you would have to scan each character within the array to see if it's valid.
[I see many users who create uninitialized variables. It's like they've never heard of a constructor.]