4

I'm trying to copy only a portion of a string (or char *) into another string (or another char *)

char * first_string = "Every morning I"
char * second_string = "go to the library, eat breakfast, swim."
char * final_string;

I would like to copy part of the second_string into the first_string.

For Example:

Every morning I eat breakfast.

What is the function that allows you to copy only a portion of a string, starting from a specific point in the string?

Note: I don't want to use string variables, but char *, or even char arrays, if possible.

7
  • 2
    I don't want to use string variables, but char *, or even char arrays, if possible. Why? Commented Nov 26, 2012 at 18:39
  • 2
    These should all be char const*, btw. If your compiler isn't already warning you about that then you are not using the proper flags. Commented Nov 26, 2012 at 18:40
  • 1
    I have bad memories with strings. I remember not being able to use strings in a win32 C++ file even after including the string header (<string.h>), so I try to avoid using them now. I'm still generally new to C++, so I use what I know how to use. Commented Nov 26, 2012 at 18:42
  • 3
    @JamesDiaz that's because you don't include <string.h>, but <string>. Commented Nov 26, 2012 at 18:43
  • 4
    @JamesDiaz: <string.h> has not been "the string header" since the mid-1990s, i.e. before C++ was even internationally standardised as a language. Strings are vastly and disproportionately easier, and if you were having trouble with them then you should focus on fixing those problems rather than creating myriad new ones for yourself. Commented Nov 26, 2012 at 18:43

5 Answers 5

7

It's std::copy, but with your code it would result in undefined behavior, because you have pointers to string literals, which are illegal to modify.

You'll need something like

char first_string[256] = "Every morning I";
char second_string[256] = "go to the library, eat breakfast, swim.";

std::copy(
    &second_string[23],
    &second_string[36],
    &first_string[strlen(first_string)]
);

Indices might be off.

Sign up to request clarification or add additional context in comments.

2 Comments

there's an analogy here somewhere about worms and being early?!
Your answer is totally right, but the person after you actually gave the me the function/and example I was looking for. If I could I would accept both answers, as both have helped me an equal amount.
4

If you really want to, use strcat (if you don't use strings, you should be ok with the C-functions anyway):

const char * first_string = "Every morning I";
const char * second_string = "go to the library, eat breakfast, swim.";
char final_string[80]; // make sure it's big enough - I haven't counted ;-)

strcpy(final_string, first_string);     // copy to destination
strcat(final_string, second_string+18); // append part of the second string

2 Comments

But that gives you Every morning I eat breakfast, swim. You want strncat, not strcat
Oh yes, I missed the swimming. However you have to add the dot at the end yourself...
1

I'd use 'strncat()' like this:

const char * first_string = "Every morning I";
const char * second_string = "go to the library, eat breakfast, swim.";

char final_string [200];

//Copies first string first
strcpy(final_string, first_string);

//Copies second string
strncat(final_string, second_string[ text_position ], text_length);

Replace text_position with the position of second_string from which you may want to start copying text and replace text_length with the length of the portion of text you want to copy.

This way you may copy separate portions of text and not necessarily from a point of the string to the end.

Comments

1

you could use strtok with "," as a delimiter to separate the parts of the second string and then use strcat to append that part onto the first string

Comments

0

You can use strstr() to search for the begining of the part you want, then strcat() to concatenate.

char * first_string = "Every morning I";
char * second_string = "go to the library, eat breakfast, swim.";
char * final_string;

char* s = "eat";
char* r = strstr(second_string, s);

strcat(final_string, first_string);
strcat(final_string, " ");
strcat(final_string, r);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.