Toto said:
Hello,
my problem is quite simple to explain.
I have the following string:
"table+camera"
and I want to remove the + sign:
"tablecamera".
How do i do that ?
Thanks for your help.
/* BEGIN new.c */
#include <stdio.h>
#define STRING "table+camera"
#define WHITE "+"
char *str_squeeze(char *, const char *);
char *str_tok_r(char *, const char *, char **);
size_t str_spn(const char *, const char *);
size_t str_cspn(const char *, const char *);
char *str_chr(const char *, int);
char *str_cpy(char *, const char *);
int main(void)
{
const char *const original = STRING;
char s1[sizeof STRING];
puts(str_squeeze(str_cpy(s1, original), WHITE));
return 0;
}
char *str_squeeze(char *s1, const char *s2)
{
char *p3;
char const *const p2 = s2;
char *const p1 = s1;
s2 = str_tok_r(p1, p2, &p3);
while (s2 != NULL) {
while (*s2 != '\0') {
*s1++ = *s2++;
}
s2 = str_tok_r(NULL, p2, &p3);
}
*s1 = '\0';
return p1;
}
char *str_tok_r(char *s1, const char *s2, char **p1)
{
if (s1 != NULL) {
*p1 = s1;
}
s1 = *p1 + str_spn(*p1, s2);
if (*s1 == '\0') {
return NULL;
}
*p1 = s1 + str_cspn(s1, s2);
if (**p1 != '\0') {
*(*p1)++ = '\0';
}
return s1;
}
size_t str_spn(const char *s1, const char *s2)
{
size_t n;
for (n = 0; *s1 != '\0' && str_chr(s2, *s1) != NULL; ++s1) {
++n;
}
return n;
}
size_t str_cspn(const char *s1, const char *s2)
{
size_t n;
for (n = 0; str_chr(s2, *s1) == NULL; ++s1) {
++n;
}
return n;
}
char *str_chr(const char *s, int c)
{
while (*s != (char)c) {
if (*s == '\0') {
return NULL;
}
++s;
}
return (char *)s;
}
char *str_cpy(char *s1, const char *s2)
{
char *const p1 = s1;
do {
*s1 = *s2++;
} while (*s1++ != '\0');
return p1;
}
/* END new.c */