Thread: Pointers and Strtok Help

  1. #1
    Registered User
    Join Date
    Apr 2016
    Posts
    12

    Pointers and Strtok Help

    Pointers and Strtok Help-screenshot_1-jpg

    You are going explore how to check if asentence is symmetric!

    For example, the following sentences aresymmetric: (read the sentence from left to right and from right toleft they are the same, of course ignore the punctuation marks).

    - “I am, am I?”
    - “Booboo is booboo.”
    - “Life is hard!! Hard is life”

    In your design, you should fully explore the use of pointer and itsarithmetic.

    Using the notation, for example str [k] or *(str + k) toaccess to kth is not allowed; you should however move a pointerrelated to str to kth position, dereferencing it to get its content.

    Steps:
    - Get the 1st and the last words of the array

    - If they are not equal, game over

    - If they are the same, remove the 1st word and the lastword, if any. When we say remove … means we closethe gap of the array. For example, if we wish to removethe first element, we move the 2nd element to theposition of the 1st element; we move the 3rd element tothe position of the 2nd element and etc. In this situation,the size of the original array will be reduced by one.

    - We repeat the above until the size of the array isreduced to one or zero.

    1. How do I do this.....remove the first element, we move the 2nd element to the position of the 1st element; we move the 3rd element to the position of the 2nd element and etc?

    2. How can I move a pointer to a specific location and deference it? I just started to learn pointer, hope any pros here can give me some directions.
    Last edited by Salem; 05-17-2016 at 11:11 PM. Reason: Removed unnecessary code tags

  2. #2
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Using the notation, for example str [k] or *(str + k) toaccess to kth is not allowed; you should however move a pointerrelated to str to kth position, dereferencing it to get its content.
    Am not sure I got the restriction quite right. Am going to give an example with how a simple implementation of strcmp works hope you can apply this to your problem. It should only be a slight modification.

    Code:
    int strcmp(const char *lhs, const char *rhs)
    {
         while(*lhs && (*lhs == *rhs))
              lhs++;
              rhs++;
    
       return *lhs - *rhs;
    }
    Probably not the best example out there but the important thing is the pointer arithmetic. If you increment a pointer it points to the next element in the array.

    In you case you should have a pointer pointing to the last item in the string while another one points to the first then decrement one while incrementing the other and comparing until
    -The pointers are equal, that is, the point to the same location in the string, in which case the string will be symmetrical.
    -Until the characters pointed to by the pointers do not match in which case the string will not be symmetrical.

    Edit: Well, I see you mention strtok in the topic but nowhere in the problem description. Do you have a requirement to use strtok ?
    Last edited by Aslaville; 05-18-2016 at 12:08 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strtok
    By Romyo2 in forum C Programming
    Replies: 7
    Last Post: 06-11-2015, 11:22 PM
  2. strtok
    By Smithy in forum C Programming
    Replies: 6
    Last Post: 04-09-2010, 07:01 PM
  3. strtok
    By jduke44 in forum C Programming
    Replies: 2
    Last Post: 09-28-2005, 08:00 PM
  4. system() , strtok() and pointers
    By Brain Cell in forum C Programming
    Replies: 16
    Last Post: 12-18-2004, 01:58 PM
  5. strtok
    By (TNT) in forum Windows Programming
    Replies: 4
    Last Post: 02-21-2002, 09:54 AM

Tags for this Thread