answersLogoWhite

0


Best Answer

This kind of exercise requires a minimal amount of mathematics and logic.

In C, strings are character arrays, and the variables used to contain strings actually contain the location (or "address") of the first character. The last character in the string is ASCII 0 (NUL), which signals the end of the string.

This problem requires you first know how long the string is, and for this purpose you would use "strlen". Assuming you have an integer variable called "length" and a string called "str", this assignment would store the length of "str" into "length": length=strlen(str);

From there, the middle character is located at the halfway point in the array. So, if you had an integer variable called "halfpos", then this assignment would place the middle position of the string in "halfpos": halfpos=length/2;

Note that strings with an even number of character (2, 4, 6, 8, 10, etc.) actually have two middle characters. So "halfpos" would actually contain one character less than what's considered the "middle" of the string. Examples of this are:

- "Grape" contains five characters, ranged 0 to 4, so "halfpos" is 2, or "a" - although 5/2 is 2.5, because "length" and "halfpos" are integers that .5 is stripped off.

- "Tomato" contains six characters, ranged 0 to 5, so "halfpos" would be the point between the "m" and the "a".

To write the code for this exercise, you will need to know more about arrays. See the related links below for more help.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write a program in c to find out the middle character of a word?
Write your answer...
Submit
Still have questions?
magnify glass
imp