Combine two strings without modifying them

This is a discussion on Combine two strings without modifying them within the C Programming forums, part of the General Programming Boards category; Hey, I've been trying to combine two strings into a third one without modifying the original two. Heres the code ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    11

    Combine two strings without modifying them

    Hey,
    I've been trying to combine two strings into a third one without modifying the original two. Heres the code I've got, but it wont compile:

    char rho_dir[50], vel_dir[50];
    char outputpath[50] = "./path/to/directory/"

    rho_dir = strcpy(rho_dir,strcat(strdup(outputpath),"fields_r ho"));
    vel_dir = strcpy(vel_dir,strcat(strdup(outputpath),"fields_v el"));

    I want to set the variables rho_dir = "./path/to/directory/fields_rho" and vel_dir = "./path/to/directory/fields_vel" without changing the string outputpath. Am I being insanely thick but I can't see an easy way to do it.

    Thanks alot,
    Kieran

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    1. use strcpy + strcat
    2. use sprintf

    Code:
    sprintf(rho_di,"%s%s", outputpath,"fields_rho");
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing strings to functions and modifying them
    By diddy02 in forum C Programming
    Replies: 6
    Last Post: 08-11-2008, 01:07 AM
  2. how do I combine two strings together
    By freeman_peterso in forum C Programming
    Replies: 19
    Last Post: 06-27-2003, 01:35 PM
  3. How to combine 2 strings ?
    By The-Guy in forum C++ Programming
    Replies: 2
    Last Post: 07-03-2002, 09:10 PM
  4. Help on modifying strings! Please. : - )
    By Unregistered in forum Windows Programming
    Replies: 3
    Last Post: 05-15-2002, 02:08 PM
  5. modifying strings
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-13-2001, 12:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21