Thread: Using a pointer to a string to insert characters (spaces) and shift the remainder?

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    93

    Arrow Using a pointer to a string to insert characters (spaces) and shift the remainder?

    If I have a string, char str
    and a pointer to a string char *ptr
    and using curses.h with direct terminal input/output #DEFINE TAB_KEY 9 ASCII
    and the cursor position on the string int *curpos

    How can I do the following,

    Code:
    ABCDEF<--cursor here & hit tab (defined 4 spaces-->GHIJKL
    or
    Code:
    <--cursor here & hit tab (4 spaces inserted)-->ABCDEFGHIJKL
    I know that I am going to be using a for loop, I just am struggling with with the conditions.

    Appreciate any help as always.
    Last edited by INFERNO2K; 10-17-2005 at 04:19 PM.

  2. #2
    Registered User TactX's Avatar
    Join Date
    Oct 2005
    Location
    Germany.Stuttgart
    Posts
    65
    I may get you wrong, but I think memmove() would please you.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Well, if you have a string, and you want to insert 4 spaces into it, you'd do it something like:
    Code:
    {
      char *p;
    
      // Move everything after curpos 4 spaces to the right
      for(p = curpos + strlen(curpos) ; p >= curpos ; p--);
        *(p + 4) = *p;
    
      // Insert 4 spaces starting at curpos
      for(p = curpos ; p - curpos < 4 ; p++)
        *p = ' ';
    }
    If you understand what you're doing, you're not learning anything.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Like TactX said, you could also do it with a memmove() and a memset() function call. Assuming the same stuff as in my last post:
    Code:
    memmove(curpos + 4, curpos, strlen(curpos) + 1);
    memset(curpos, ' ', 4);
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Nov 2004
    Posts
    93
    How can I do strlen(curpos), if is not of type string?
    Last edited by INFERNO2K; 10-17-2005 at 04:52 PM.

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    To be honest, I can't tell what the hell curpos is. From the name I figured you probably either a) Typo'd the type as int instead of char, or b) accidentally typo'd it as a pointer instead of a plain int like an index usually is.

    Is it a pointer to an index into the string then? If so, use strlen(str + *curpos) instead of strlen(curpos).

    You didn't explain what it was very well, and why it's a pointer is still a mystery to me. Anyway, you have the concept of what you need to do so you should be able to do it now. Move everything from curpos to the end 4 spaces to the right, then stick 4 spaces in starting at curpos.
    Last edited by itsme86; 10-17-2005 at 04:57 PM.
    If you understand what you're doing, you're not learning anything.

  7. #7
    Registered User
    Join Date
    Nov 2004
    Posts
    93
    Oops that's a mistake I made in the begining, here, I'll try my best to exaplain. The pointer "curpos" points to an integer holding the position of the cursor in the field (0 is the first position).

  8. #8
    Registered User
    Join Date
    Sep 2005
    Location
    Sydney
    Posts
    60
    One thing: char str is not a string, it's a single character, so I hope you aren't using it as a string...

  9. #9
    Registered User
    Join Date
    Nov 2004
    Posts
    93
    nope

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. still problems with ceasar shift
    By trevordunstan in forum C Programming
    Replies: 2
    Last Post: 09-14-2008, 01:49 AM
  2. Replies: 4
    Last Post: 10-14-2005, 12:53 PM