Hi there,
I am currently converting my code from using strings to char arrays as strings take up too much memory and are not good practice. I am currently using functions like .replace() and .remove() to format my strings. Is there an equivalent method to do what these functions do with char arrays?
The replace() function allows you to replace all instances of a given character with another character. You can also use replace to replace substrings of a string with a different substring.
The remove() function allows you to remove chars from the provided index to the end of the string or from the provided index to index plus count.
Thanks!
Start by having a look at the functions that are available in the standard c-string library:
http://www.cplusplus.com/reference/cstring/
Thanks!
I had already looked at that documentation and did not find what I wanted.
I know how to mimic these functions but was wondering if there are already any c functions to remove and replace character.
I wanted to do this as I believe the functions likely use the most memory efficient way to do things.
Thanks
Here's a freebie - (I don't usually post answers in one shot, but here you go!)
Not really the most elegant - but it works and handles mixed length 'strings'.
Uses no extra RAM outside the function.
Note the do-while() always executes at least once unlike while()
#define this up top MAX_STRING_LENGTH nnnn
void str_replace(char *src, char *oldchars, char *newchars) { // utility string function
char *p = strstr(src, oldchars);
char buf[MAX_STRING_LENGTH];
do {
if (p) {
memset(buf, '\0', strlen(buf));
if (src == p) {
strcpy(buf, newchars);
strcat(buf, p + strlen(oldchars));
} else {
strncpy(buf, src, strlen(src) - strlen(p));
strcat(buf, newchars);
strcat(buf, p + strlen(oldchars));
}
memset(src, '\0', strlen(src));
strcpy(src, buf);
}
} while (p && (p = strstr(src, oldchars)));
}
Thanks so much lastchancename!!
I'll definitely take the time to understand that code and implement that function into my own.
I am not new to programming but am new to embedded programming so memory is something I have never been concerned with when programming.
Is there something lying around for a remove function?
And to confirm: your function would work like this:
char test[] = "12-12-12";
str_replace(test, "-", "");
then test would now be "121212"?
Thanks again!
char test[] = "12-12-12";
str_replace(test, "-", "");
then test would now be "121212"?
Yes... and to remove, simply replace with an empty string "" as you have shown.
Great!
In terms of removing, I need a function which removes a certain number of characters starting at an index. Your function would not work for this as I do not know what the characters are.
Would memmove work then?
Daniel
If ypu can find the first character to remove - strtok() / strchr(), and the last with strtok() / strchr() or a known number of chars...
Then there are ways - either with the replace function above, or copy the required bits into a new target string to be returned by the function.
Thanks for the response but I do not know what the characters will be. I only know which index they are at and how many characters I would like to remove. Therefore, I do not think those functions work.
For context, I have the current time in a char array received from my phone. I would like to alter this time to not include seconds.
10:34:20 PM --> 10:34 PM
Thanks!
In that case, create a temporary char xxx[] target string...
Then strncpy() from the source string to the end of the first part target... (n chars)
Finally, strcpy() from the 'next following character' to the end of the source. (the trailing \0 null will automagically be added).
memset(buf, '\0', strlen(buf));
Ouch! Not cool.
Hi lastchancename,
This is what I have done for my remove function but am confused on what you are talking about. Please elaborate?
void str_remove(char *src, int index, int count)
{
char buf[20];
strncpy(buf, src, index-1);
}
Codingbadly, what is the problem?
ddesousa:
Codingbadly, what is the problem?
What's the fun in me just telling you? Spend some time thinking about it.
Small hint: There are actually two problems. One rather minor and one extremely serious. Which is especially compelling because it is just a single line of code.
I must admit I didn't look at that at the time!
... simply tacking the NULL on the end, or wait until finished then tack[] the NULL on the end would have been sufficient!
Problem: HINT - it can run past the end of the array[] !
I found the original function somewhere else, and tweaked it 'at the time' - so I have to take credit for propagating the untidy memset(), but it's not entirely bad code...!
Sorry everyone,
I still do not see the problem with that line.
What should it be instead?
And lastchancename, can you elaborate what I need to do for the remove function to work?
Thanks all!
ddesousa:
I still do not see the problem with that line.
Describe strlen. What does it do?
void str_remove(char *src, int index, int count)
{
char buf[20];
strncpy(buf, src, count); // copy count chars from start of src to buf
strcat(buf, src[index]); // append RH end of string from src[index]
}
memset(buf, '\0', strlen(buf));
Simply nulls out (some of) buf[]
EDIT: Correction
It nulls the populated part of buf[] - which may not be the full length of the buf array.
For that (which is a couple of uS slower) would use sizeof(buf)
Point 13 in the forum sticky post:
- Standard libraries (libc)
The Arduino IDE includes ("links") libc - a standard library of common building-blocks that C programs often need. This includes things such as type conversions and string manipulations.
The documentation is here: avr-libc: Modules
C/C++ programmers should make themselves familiar with what kinds of things are already provided in this standard library. In particular, browse through the functions in stdlib.h and string.h and the types in stdint.h .
Visit avr-libc: Modules and especially the string,h page avr-libc: <string.h>: Strings