Thread: Reverse a string using single pointer

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    28

    Reverse a string using single pointer

    Hi,I want to reverse a string using single pointer.How to do this.Just give me an idea

  2. #2
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    You could swap the first and last character of your string until you reach the middle. This will only use chars.

    Or do you mean you want to use one additionnal pointer other than the one for the string (in which case, it would be much easier) ?
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    330
    If you can use an index you can swap the items starting at 0 and length-index-1 until half the string is reached

    Code:
    int index = 0;
    int length = strlen(ptr);
    
    // for half the string length
    
    swap(ptr[index], ptr[length - index - 1]);
    index++;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Pointer to array of string and Array of Pointer to String
    By vb.bajpai in forum C Programming
    Replies: 2
    Last Post: 06-15-2007, 06:04 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  5. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM