Thread: Help with String Operations

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    12

    Help with String Operations

    Hey all,

    I've searched far and wide for an answer to this but have yet to find a case which is the same as mine...

    Basically I have a string (indata), eg "1234" and I want to be able to copy the "23" from this and store it in a seperate string (oldadd). I have tried strcpy but it only seems to want to copy the entire string, not just the single element which I want.

    Originally I had the following (left out declarations since I'm being lazy here):
    Code:
    indata = "1234";
    strncpy(oldadd, indata[1], 1);
    strncat(oldadd, indata[2], 1);
    With the intention of copying the 2nd char of the string indata to oldadd and then sticking the 3rd char onto the end of oldadd later. This is the only way I can think of doing it, I can't use a strstr or strchr because the little bit I want will always be different however I know that it will always be the 2nd and 3rd character of the indata string.

    Thanks for any help, I hope this hasn't been answered somewhere else which I've missed :S

    pgcrooks

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Have you tried to use strncpy in a proper way (call once to copy two characters)?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    12
    Quote Originally Posted by anon View Post
    Have you tried to use strncpy in a proper way (call once to copy two characters)?
    Hey anon, thanks for the quick reply.

    The problem with strcpy or strncpy is that it copies the string from the start, I want to ignore the 1st character (ie. start at indata[1] and not indata[0]).

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It doesn't necessarily copy the string from the start. You give it a pointer and it starts copying from there. The pointer may well point to the middle of a string.

    Now you'll have to figure out how to obtain a pointer to the second character in your string.

    It also looks that you'll need to set the null terminator yourself after using strncpy.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    to copy one character only just use
    Code:
    dest[x] = src[y];
    to add nul-char after that
    Code:
    dets[x+1] = '\0';
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Jul 2008
    Posts
    133
    Maybe:
    Code:
    snprintf(old, 3, "%s", &(new[1]));
    ?

  7. #7
    Registered User
    Join Date
    Aug 2008
    Posts
    12
    Humm I shall have to learn more about pointers, never really got my head around the snytax for them. Thanks anyway!

    Code:
    addptr = *indata[2];
    strncpy = (oldadd, addptr, 2);
    Right, tried that and no luck. Thought it should have set a pointer to the start of what I want in the string but it says incorrect type. Back to work I go...

  8. #8
    Registered User
    Join Date
    Jul 2008
    Posts
    133
    Quote Originally Posted by pgcrooks View Post
    Humm I shall have to learn more about pointers, never really got my head around the snytax for them. Thanks anyway!

    Code:
    addptr = *indata[2];
    strncpy = (oldadd, addptr, 2);
    Right, tried that and no luck. Thought it should have set a pointer to the start of what I want in the string but it says incorrect type. Back to work I go...
    Try:
    Code:
    snprintf(oldadd, 3, "%s", &(indata[1]));
    EDIT: docs says it auto-puts \0 on end !
    Last edited by rasta_freak; 08-07-2008 at 08:07 AM.

  9. #9
    The larch
    Join Date
    May 2006
    Posts
    3,573
    As other people are showing you, you don't obtain the pointer by trying to dereference what is already a char (*str[i]), but by taking the address (&str[i]). Alternatively you can use pointer arithmetic (str + 1).

    Also note that the second character is at index 1, not 2.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  10. #10
    Registered User
    Join Date
    Aug 2008
    Posts
    12
    Quote Originally Posted by rasta_freak View Post
    Try:
    Code:
    strncpy(oldadd, 3, "%s", &(indata[1]));
    Ok, thats seems to have compiled ok! Just had to modify the order

    Code:
    strncpy(oldadd, &(indata[1]), 2);
    Have about a million other syntax errors to fix before I can actually run the program, lets hope that works!

    Can you tell it's the first time I've tried string manipulation, integer work is so much easier lol.

    Thanks all.

  11. #11
    Registered User
    Join Date
    Jul 2008
    Posts
    133
    Quote Originally Posted by pgcrooks View Post
    Ok, thats seems to have compiled ok! Just had to modify the order

    Code:
    strncpy(oldadd, &(indata[1]), 2);
    Have about a million other syntax errors to fix before I can actually run the program, lets hope that works!

    Can you tell it's the first time I've tried string manipulation, integer work is so much easier lol.

    Thanks all.
    No, you got my error-post. Use "snprintf()" !!! (look at post again, and sorry about mistake)

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by rasta_freak View Post
    No, you got my error-post. Use "snprintf()" !!! (look at post again, and sorry about mistake)
    s(n)printf probably takes 30-100 times longer than strncpy()/strncat(), as the process of parsing the input string and such is quite inefficient.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User
    Join Date
    Jul 2008
    Posts
    133
    Quote Originally Posted by matsp View Post
    s(n)printf probably takes 30-100 times longer than strncpy()/strncat(), as the process of parsing the input string and such is quite inefficient.

    --
    Mats
    But, then again, fastest method would be:
    Code:
    oldadd[0] = indata[1];
    oldadd[1] = indata[2];
    oldadd[2] = 0;
    EDIT: maybe using 'short' for 2-byte operation.

  14. #14
    Registered User
    Join Date
    Aug 2008
    Posts
    12
    Thanks guys, got it working in the end. Just used Rasta's assign technique, much easier! I forgot you could do that with single chars, just not with full strings (think that's right).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. standard String operations
    By kolucoms6 in forum C++ Programming
    Replies: 3
    Last Post: 03-09-2008, 12:47 PM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM