Just pass the address of the first letter you want to copy: strcpy(dest, &c[13]).
If you want to end before the string ends, use strncpy or, better, memcpy (don't forget to 0-terminate the copied string).
Just pass the address of the first letter you want to copy: strcpy(dest, &c[13]).
If you want to end before the string ends, use strncpy or, better, memcpy (don't forget to 0-terminate the copied string).
strncpy is (essentially always) the wrong choice. In this specific case, it'll work, but only pretty much by accident -- since you're copying the end of a string, it'll work just like strcpy. That of course, leads to the fact that strcpy will work too, but more easily.
If you want to handle a more general case, there's a rather surprising (but highly effective) choice: sprintf can do exactly what you want:
sprintf(d, "%s", c+20);
sprintf (unlike strncpy or strcpy) also works for the most general case -- let's assume you wanted to copy just do I to d, but have d come out as a properly NUL-terminated string:
sprintf(d, "%*s", 4, c+13);
Here, the 4 is the length of the section to copy, and c+13 is a pointer to the beginning of the part to copy.
c - copy specific characters from a string to another string - Stack Overflow
url - How to Compare Last n Characters of A String to Another String in C - Stack Overflow
c++ get the last (n) char in a string - Stack Overflow
C - copy the last characters from an unknown length string - Stack Overflow
char *lastN(const char *str, size_t n)
{
size_t len = strlen(str);
return (char *)str + len - n;
}
int main(void)
{
printf("`%s`\n", lastN("1234567890", 4));
}
The header <string.h> contains function strlen that returns the length of a passed string. The function is declared like
size_t strlen(const char *s);
Strictly speaking the last character of a string is its terminating zero character '\0'. But it seems by the last n characters of a string you mean n characters before the terminating zero.
To be able to get a pointer to the last n characters of a string the string should have at least n characters.
You could write a function the following way. If the passed string contains less than n characters then the function returns a pointer to the string itself.
char * last_n( const char *s, size_t n )
{
size_t length = strlen( s );
return ( char * )( length < n ? s : s + length - n );
}
Here is a demonstrative program.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
char * last_n( const char *s, size_t n )
{
size_t length = strlen( s );
return ( char * )( length < n ? s : s + length - n );
}
int main(void)
{
char s[] = "Hello world!";
char *p = last_n( s, 6 );
puts( p );
for ( char *current = p; *current != '\0'; ++current )
{
*current = toupper( ( unsigned char )*current );
putchar( *current );
}
putchar( '\n' );
return 0;
}
The program output is
world!
WORLD!
If you need to obtain the index where the last n characters of a string start you can write for example
char *p = last_n( s, n );
size_t pos = p - s;
Or just
size_t pos = last_n( s, n ) - s;
If you have a pointer-to-char array, str, then this:
int len = strlen(str);
const char *last_four = &str[len-4];
will give you a pointer to the last four characters of the string. You can then use strcmp(). Note that you'll need to cope with the case where (len < 4), in which case the above won't be valid.
Just perform if ( strcmp(str1+strlen(str1)-4, str2+strlen(str2)-4) == 0 ) {}.
Make sure both strings are at least 4 characters long.
string lastN(string input)
{
return input.substr(input.size() - n);
}
Is find_last_of what you need?
size_type find_last_of( const basic_string& str, size_type pos = npos ) const;
Finds the last character equal to one of characters in the given character sequence. Search finishes at pos, i.e. only the substring [0, pos] is considered in the search. If npos is passed as pos whole string will be searched.
char s[5] = {0};
size_t len = 0;
char dest[] = "abcdef.ghi";
char *p = strchr(dest, '.');
if(!p)
{
// error no '.'
return;
}
len = strlen(p);
if(len != sizeof(s)-1)
{
// More or less than 3 characters plus '.'
return;
}
strcpy(s, p);
When the string is null-terminated you can use strlen to get the lenght of the string:
char s[5];
size_t len, offset;
char dest[] = "abcdef.ghi";
len = strlen(dest);
memset( s, '\0', sizeof(char)*5);
offset = len-4;
strncpy( s, &dest[offset], 4 );
If this is not the case you can loop over the string as an array and look for your dot.
Afterwards you can use this index to calculate your correct offset. But be careful for that solution. If one string does not hate a dt and last free character you can cause an access violation.
First, you need add +1 in size for the \0.
char color1[3];
char color2[5];
and then:
strncpy(color1, string, 2);
color1[3] = '\0';
strncpy(color2, string + 2, 4);
color2[4] = '\0';
Assuming that
char *string = "AAbbCC";
printf("color1 => %s\ncolor2 => %s\n", color1, color2);
The output is:
color1 => AA
color2 => bbCC
I hope this help you.
UPDATE
You can write a substr() function to get part of string(from x to y) and then copy to your string.
char * substr(char * s, int x, int y)
{
char * ret = malloc(strlen(s) + 1);
char * p = ret;
char * q = &s[x];
assert(ret != NULL);
while(x < y)
{
*p++ = *q++;
x ++;
}
*p++ = '\0';
return ret;
}
Then:
char *string = "AAbbCC";
char color1[3];
char color2[4];
char color3[5];
char *c1 = substr(string,0,2);
char *c2 = substr(string,2,4);
char *c3 = substr(string,4,6);
strcpy(color1, c1);
strcpy(color2, c2);
strcpy(color3, c3);
printf("color1 => %s, color2 => %s, color3 => %s\n", color1, color2, color3);
The output:
color1 => AA, color2 => bb, color3 => CC
And Don't forget:
free(c1);
free(c2);
free(c3);
Well, color1 and color2 are two bytes long - you have no room for the \0 terminator. When you look at one of them as a string, you get more characters that you wished for. If you look at them as two characters, you'll get the right result.
You should define them as 3 characters long and put the \0 at the end.
I just fell foul of the fact that strncpy does not add an old terminator if the destination buffer is shorter than the source string. Is there a single function standard library replacement that I could drop in to the various places strncpy is used that would copy a null terminated string up to the length of the destination buffer, guaranteeing early (but correct) termination of the destination string, if the destination buffer is too short?
Edit:
-
Yes, I do need C-null terminated strings. This C API is called by something else that provides a buffer for me to copy into, with the expectation that it’s null terminated
Edit 2:
-
I know I can write a helper function that’s shared across relevant parts of the code, but I don’t want to do that because then each of those modules that need the function becomes coupled to a shared helper header file, which is fine in isolation but “oh I want to use this code in another project, better make sure I take all the misc dependencies” is best avoided. Necessary if necessary, but if possible using a standard function, even better.
If i have understood your question correctly, what you need is strncpy() or memcpy(), which will copy exactly n bytes.
Here, you need to be careful while giving the value of n. If n is equal to the size of the supplied target buffer and there is no null byte among the first n bytes of the source, there will be no space left for the terminating null.
So, IMO, if you want to copy exactly n bytes then define the array to be of size n+1, copy exactly n bytes and and put a '\0' at nth variable [Considering index starts from 0].
Yes you can. But in that case must not have to use the character array as a string, i.e, you must not apply any string operation on it (as it is not \0 terminated and will invoke undefined behavior).
To copy that array, you can use a loop or standard library function memcpy or memmove.