Reputation: 4709
I wouldn't mind writing my own function to do this but I was wondering if there existed one in the string.h or if there was a standard way to do this.
char *string = "This is a string";
strcut(string, 4, 7);
printf("%s", string); // 'This a string'
Thanks!
Upvotes: 4
Views: 16409
Reputation: 4483
I already wrote the answer for a similar problem there https://stackoverflow.com/a/42283266/6003870. It suitable for common actions and based mainly the function strncpy() from the C standard library. But this problem could be solved by the function memmove() as mentioned the dead user @sharptooth.
The function str_slice_in_place() use the memmove() as base and has support for positive indexes.
int
str_slice_in_place(char str[], const int index_from, const int index_to)
{
// a support for only positive indexes
if (index_from < 0 || index_to < 0)
return -1;
int len = index_to - index_from;
// "index_from" is more than "index_to"
if (len < 0)
return -1;
memmove(str, str + index_from, len);
str[len] = '\0';
return 0;
}
I also show you a code of a function main() and examples how to use this function.
#include <stdio.h>
#include <string.h>
int
main()
{
char line[] = "--------------------------------------";
char str1[] = "Remember a good";
char str2[] = "Remember a good";
char str3[] = "Remember a good";
char str4[] = "Remember a good";
printf("| Original | Slice | Result \n%s\n", line);
printf("| %s | ", str1);
str_slice_in_place(str1, 0, 8);
printf("(%d, %d) | %s\n", 0, 8, str1);
printf("| %s | ", str2);
str_slice_in_place(str2, 3, 9);
printf("(%d, %d) | %s\n", 3, 9, str2);
printf("| %s | ", str3);
str_slice_in_place(str3, 0, 1);
printf("(%d, %d) | %s\n", 0, 1, str3);
printf("| %s | ", str4);
str_slice_in_place(str4, 1, 1);
printf("(%d, %d) | %s\n", 1, 1, str4);
puts(line);
return 0;
}
| Original | Slice | Result
--------------------------------------
| Remember a good | (0, 8) | Remember
| Remember a good | (3, 9) | ember
| Remember a good | (0, 1) | R
| Remember a good | (1, 1) |
--------------------------------------
$ gcc --version
gcc (Debian 4.9.2-10) 4.9.2
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description: Debian GNU/Linux 8.6 (jessie)
Release: 8.6
Codename: jessie
$ uname -a
Linux localhost 3.16.0-4-amd64 #1 SMP Debian 3.16.36-1+deb8u2 (2016-10-19) x86_64 GNU/Linux
Upvotes: 0
Reputation: 12273
You can just tell printf to cut the interesting parts out for you:
char *string = "This is a string";
printf("%.*s%s", 4, string, &string[7]); // 'This a string'
:-)
Upvotes: 4
Reputation: 6130
You may be able to accomplish what you want and avoid writing a new function with a combination of strncpy and strcat:
#include <stdio.h>
#include <string.h>
int main(void)
{
char newStr[10];
char origStr[] = "This is a string";
strncpy(newStr, origStr, 4);
strcat(newStr, &origStr[7]);
printf("newStr = %s\n", newStr);
return(0);
}
For me, this outputs "This a string" (quotes mine), using Borland's free command line compiler on Windows XP. You can check out string.h at: http://opengroup.org/onlinepubs/007908799/xsh/string.h.html
Upvotes: 0
Reputation: 170549
Use memmove to move the tail, then put '\0' at the new end position. Be careful not to use memcpy - its behaviour is undefined in this situation since source and destination usually overlap.
Upvotes: 7
Reputation: 89839
If you're talking about getting rid of the middle of the string and moving the rest earlier in place, then I don't think there's a standard library function for it.
The best approach would be to find the end of the string, and then do an O(cut_size) cycle of shifting all the characters to the new location. In fact, there's a similar common interview question.
You have to be careful about using things like memory copy since the destination buffer overlaps with the source.
Upvotes: 1
Reputation: 4423
If you are doing this in C and know the offset then it's pretty simple.
char string[] = "my string";
char *substring;
substring = &string[2];
printf("%s", substring);
EDIT: If you wanted to shift the string.
char string[] = "my string";
int i = 0;
int offset = 2;
for( i = 0; string[i+offset] != '\0'; i++ ) {
string[i] = string[i + offset];
}
string[i] = '\0';
Upvotes: -1