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.
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.
char *p = string;
int ofs = 0;
while(*(p+ofs)) {
while(*(p+ofs) == '+') ++ofs;
*p = *(p+ofs); ++p;
} *p = 0;
fixing the many errors i have made in this code might be a good exercise
.sect signature
thanks.
Isn't there another way to do that using a combination of the String functions provided by the C ANSI ?
like strstr(), strtok() ...
thanks
if you have a single +, then you can use strstr(string, "+") to find a point in the string w/ a + and then use strcpy to write all the characters past this point back one, but if you have multiple characters to replace rolling your own replacement function to do it in a single pass might be worth it
.sect signature
can you write the code in details ?
I do not have a compiler at the moment, so I cannot make tests for now.
Thanks
Since + is a CHARACTER and not a string you should use strchr not strstr.
Note: since I'm using strncat you have to put the null character when you declare the destantion.Code:#include <string.h> #include <stdio.h> int main (void) { char str[30]="H+e+l+l+o+W+o+r+l+d"; char dest[30]="\0"; char *front, *end; front = str; while ( (end = strchr(front, '+')) != NULL) { strncat(dest, front, end-front); front = end + 1; } /* Take care of the rest of the string */ end = front + strlen(front); strncat(dest, front, end-front); puts(dest); return 0; }
Hopefully this will give you enough information to modify it to suit your needs.
thanks, that is exactly what i needed
Also here...
//hi try this one
#include <string.h>
#include <stdlib.h>
#define NULL 0
main()
{
char string[]="table+camera";
char seps[]= "+";
char* token1;//found by strtok
char* token2;
token1 = strtok(string,seps);
token2 = strtok(NULL,seps);
strcat(token1,token2);
//look in token1
}
by the way while i was trying to create string as char* i got some breaks, whats the problem
> #define NULL 0
Include the correct header file, don't define NULL in your own code
> main()
Whilst its much better than saying void main, saying int main() is preferred. Especially since C99 (the latest C standard) makes implicit types illegal, it's best to get into the habit of being specific.
> strcat(token1,token2);
Bad news - strcat() had undefined behaviour if the source and destination strings overlap (as they do in this case).
> by the way while i was trying to create string as char* i got some breaks
Most modern compilers on most modern operating systems make string "constants", really constant, by placing them in read-only memory. Since strtok modifies the string by writing \0 into it, your OS will step in and kill your program as soon as you try it.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Code:void remove_character(char * dst, char * src, char ch) { char chCur; size_t iSrc = 0, iDst = 0; do { if ((chCur = src[iSrc++]) != ch) dst[iDst++] = chCur; } while (chCur != '\0'); } int main(void) { char tst[] = "++++ta+ble+cam+era++++"; char tst2[sizeof(tst)]; /* To another buffer... */ puts(tst); remove_character(tst2, tst, '+'); puts(tst2); /* and in place... */ puts(tst); remove_character(tst, tst, '+'); puts(tst); getchar(); return 0; }