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?![]()
This is a discussion on Print middle of a given list within the C Programming forums, part of the General Programming Boards category; How can i print the middle of given list? example: Input: aaa bbb ccc ddd eee Output: ccc how can ...
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?![]()
Just get the element and print it
Exactly how to do this depends on the specifics, e.g., a linked list?
C + C++ Compiler: MinGW port of GCC
Version Control System: Bazaar
Look up a C++ Reference and learn How To Ask Questions The Smart Way
You mean the middle of a linked list?
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
I support http://www.ukip.org/ as the first necessary step to a free Europe.
yup. example i input a sentence: hello my name is alex. so i will get the output: name <-- middle in the list.
Indeed. What ideas do you have to solve this?
C + C++ Compiler: MinGW port of GCC
Version Control System: Bazaar
Look up a C++ Reference and learn How To Ask Questions The Smart Way
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?
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.
C + C++ Compiler: MinGW port of GCC
Version Control System: Bazaar
Look up a C++ Reference and learn How To Ask Questions The Smart Way
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);
I guess you're supposed to use your experience gained from your previous assignment
Count word in a string.
Once you know what N is, you do the loop again and stop at N/2
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
I support http://www.ukip.org/ as the first necessary step to a free Europe.
Oh thanks. Okay i take a try first.![]()
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?
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
You must first be able to detect where words are... you asked about this in the other thread.
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; }
See comments in the code:
You should count the number of words in the string and not the characters if you want to find the middle word.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; }
Bye, Andreas