Thread: Simple String Manipulation (Beginner question)

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    1

    Simple String Manipulation (Beginner question)

    I've just started learning C and am having some trouble.

    Lets say you have these two strings.

    char a [5] = "abcd";
    char b [3] = "zz";

    I want to use strcpy() and strcat() to change b into "bd".

    Something like,

    strcpy(b, a[1]);
    strcat(b, a[3]);

    Clearly, this does not work.
    a[1] is not a string. I have no idea how to get a "b" string from the variable a.

    Any help?

  2. #2
    Registered User
    Join Date
    Nov 2008
    Posts
    6
    If the string is actually that short you can manipulate it as a character array.

    strcpy(string,string[location]) wont work b/c you are using a function to copy one string into another to copy one character into a string. If you want to copy one char to a string you should try
    strcpy(string,"c"). I think anything b/w quotes can be considered a string literal. You should google strcpy.

  3. #3
    Registered User
    Join Date
    Dec 2008
    Location
    Black River
    Posts
    128
    In order to get "b" from the string a, you would have to do &a[1]. However, because both strcpy and strcat expect null terminated strings, you can't really use them in your example. On the other hand, you could use something like strncpy and strncat to achieve what you want:

    Code:
    char a[] = "abcd";
    char b[] = "zz";
    
    strncpy(b, &a[1], 1);
    strncat(b, &a[3], 1);
    However, for simple cases like what you've got, you're MUCH better using direct assignment (IE: using strcpy or memcpy with "bd" as the concatenating argument)

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Also this can happen:
    Code:
    b[0]='b';
    b[1]='d';
    's'ingle quotes
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Or, if you really want to do what is in the original post, as closely as possible, with the least overhead:
    Code:
    b[0] = a[1];
    b[1] = a[3];
    --
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. class object manipulation
    By guda in forum C++ Programming
    Replies: 2
    Last Post: 10-09-2004, 10:43 AM
  3. simple string question
    By c_cool in forum C Programming
    Replies: 4
    Last Post: 07-30-2004, 12:27 PM
  4. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM