0

char MiddleCharacter( ); //Returns the "middle" character of the
//string. If the string length is even
//then the right-most character of the
//"left" half is returned.

how to find the middle character in a string

Thank you

2
Contributors
3
Replies
4
Views
10 Years
Discussion Span
Last Post by mauro21pl
0

Is Cstring a class? If it lets you get an individual character and the size, you can do this easy.

char MiddleCharacter( Cstring string ) {
  return string[string.length() / 2];
}

A char array string is the same way but you have to use strlen.

char MiddleCharacter( const char *string ) {
  return string[strlen( string ) / 2];
}

No biggie, right? :)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.