Thread: Print middle of a given list

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    28

    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. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Just get the element and print it

    Exactly how to do this depends on the specifics, e.g., a linked list?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

  4. #4
    Registered User
    Join Date
    Jan 2013
    Posts
    28
    yup. example i input a sentence: hello my name is alex. so i will get the output: name <-- middle in the list.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Indeed. What ideas do you have to solve this?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jan 2013
    Posts
    28
    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. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Jan 2013
    Posts
    28
    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. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

  10. #10
    Registered User
    Join Date
    Jan 2013
    Posts
    28
    Oh thanks. Okay i take a try first.

  11. #11
    Registered User
    Join Date
    Jan 2013
    Posts
    28
    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. #12
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    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.

  13. #13
    Registered User
    Join Date
    Jan 2013
    Posts
    28
    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. #14
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem inserting node in middle of list
    By c_weed in forum C++ Programming
    Replies: 3
    Last Post: 12-22-2010, 09:14 PM
  2. in the middle delete list
    By bazzano in forum C Programming
    Replies: 7
    Last Post: 05-05-2007, 01:03 AM
  3. Help with deleting node in the middle of a linked list
    By allplay in forum C Programming
    Replies: 2
    Last Post: 11-11-2006, 11:22 AM
  4. priority list won't work, i give up
    By teamster in forum C Programming
    Replies: 8
    Last Post: 10-19-2006, 12:26 PM
  5. Adding To The Middle Of A Linked List
    By LostNotFound in forum C++ Programming
    Replies: 1
    Last Post: 02-23-2003, 06:02 PM