# Thread: Print middle of a given list

1. ## Print middle of a given list

How can i print the middle of given list?
example:
Input: aaa bbb ccc ddd eee
Output: ccc

how can this code to be done?

2. Just get the element and print it

Exactly how to do this depends on the specifics, e.g., a linked list?

3. You mean the middle of a linked list?

4. yup. example i input a sentence: hello my name is alex. so i will get the output: name <-- middle in the list.

5. Indeed. What ideas do you have to solve this?

6. actually this is my tomorrow lab test question. i really can't figure out this coding. how can we calculate the middle of the string?

7. The middle of the string? I thought you want the middle of a linked list? You need to be clear as to the problem that you are trying to solve.

8. whats the different between string and linked list? cause i thought we have input a sentence after that look for middle word of the sentence.
Code:
``` char str[50]
printf("\n\n\t Enter A Sentence: ");
gets(str);```

9. I guess you're supposed to use your experience gained from your previous assignment
http://cboard.cprogramming.com/c-pro...rd-string.html

Once you know what N is, you do the loop again and stop at N/2

10. Oh thanks. Okay i take a try first.

11. Erm. Can someone help me on the coding? so example the loop is ( int i =0; i<=n; i++) how can i stop the loop at middle?

12. Most likely it should be

for (int i=0; i < n; i++)

If n is the number of words there are, and say there are 9 words for example, and i counts how many words you have seen so far, then as soon as i == 4, the "current word" must be the middle word of your text.

To get the result of 4, of course you do integer division: n/2

13. can someone check with my coding?
Code:
```#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <conio.h>

int main()
{

char ip[]="Input string";
int size=strlen(ip);
char *word[size];
int loop;
word[0]=strtok(ip," ");
for(loop=0;loop<size;loop++)
{
word[loop]=strtok(NULL," ");
if(word[loop]==NULL)
break;
}

// to print the middle element

int mid=size/2;
printf("word is %s.\n",word[mid]);

getch();
return 0;
}```

14. See comments in the code:
Code:
```#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <conio.h>

int main()
{

char ip[]="Input string";
int size=strlen(ip);          // size is the number of characters in ip (12)
char *word[size];
int loop;
word[0]=strtok(ip," ");        // here word[0] points to the first word in ip ("Input")
for(loop=0;loop<size;loop++)
{
word[loop]=strtok(NULL," ");   //  loop == 0 at the first iteration,
//  thus you overwrite the value of word[0]
if(word[loop]==NULL)
break;
}

// to print the middle element

int mid=size/2;          // size is still the number of characters (12)
// thus mid == 6
printf("word is %s.\n",word[mid]);     // word[6] is uninitialised and very
// likely pointing to garbage

getch();            // why not the standard getchar()?
return 0;
}```
You should count the number of words in the string and not the characters if you want to find the middle word.

Bye, Andreas