[ACCEPTED]-How to remove \n or \t from a given string in C?-string
This works in my quick and dirty tests. Does 5 it in place:
#include <stdio.h>
void strip(char *s) {
char *p2 = s;
while(*s != '\0') {
if(*s != '\t' && *s != '\n') {
*p2++ = *s++;
} else {
++s;
}
}
*p2 = '\0';
}
int main() {
char buf[] = "this\t is\n a\t test\n test";
strip(buf);
printf("%s\n", buf);
}
And to appease Chris, here is 4 a version which will make a place the result 3 in a newly malloc
ed buffer and return it (thus 2 it'll work on literals). You will need to 1 free
the result.
char *strip_copy(const char *s) {
char *p = malloc(strlen(s) + 1);
if(p) {
char *p2 = p;
while(*s != '\0') {
if(*s != '\t' && *s != '\n') {
*p2++ = *s++;
} else {
++s;
}
}
*p2 = '\0';
}
return p;
}
If you want to replace \n or \t with something else, you can use the function strstr(). It 16 returns a pointer to the first place in 15 a function that has a certain string. For 14 example:
// Find the first "\n".
char new_char = 't';
char* pFirstN = strstr(szMyString, "\n");
*pFirstN = new_char;
You can run that in a loop to find 13 all \n's and \t's.
If you want to "strip" them, i.e. remove them from the string, you'll need to actually 12 use the same method as above, but copy the 11 contents of the string "back" every time 10 you find a \n or \t, so that "this i\ns 9 a test" becomes: "this is a test".
You can 8 do that with memmove (not memcpy, since 7 the src and dst are pointing to overlapping 6 memory), like so:
char* temp = strstr(str, "\t");
// Remove \n.
while ((temp = strstr(str, "\n")) != NULL) {
// Len is the length of the string, from the ampersand \n, including the \n.
int len = strlen(str);
memmove(temp, temp + 1, len);
}
You'll need to repeat this 5 loop again to remove the \t's.
Note: Both of these 4 methods work in-place. This might not be safe! (read Evan Teran's comments for details.. Also, these methods 3 are not very efficient, although they do 2 utilize a library function for some of the 1 code instead of rolling your own.
Basically, you have two ways to do this: you 18 can create a copy of the original string, minus 17 all '\t'
and '\n'
characters, or you can strip the 16 string "in-place." However, I 15 bet money that the first option will be 14 faster, and I promise you it will be safer.
So 13 we'll make a function:
char *strip(const char *str, const char *d);
We want to use strlen()
and 12 malloc()
to allocate a new char *
buffer the same size 11 as our str
buffer. Then we go through str
character 10 by character. If the character is not contained 9 in d
, we copy it into our new buffer. We 8 can use something like strchr()
to see if each character 7 is in the string d
. Once we're done, we have 6 a new buffer, with the contents of our old 5 buffer minus characters in the string d
, so 4 we just return that. I won't give you sample 3 code, because this might be homework, but 2 here's the sample usage to show you how 1 it solves your problem:
char *string = "some\n text\t to strip";
char *stripped = strip(string, "\t\n");
This is a c string function that will find 5 any character in accept
and return a pointer to 4 that position or NULL if it is not found.
#include <string.h>
char *strpbrk(const char *s, const char *accept);
Example:
char search[] = "a string with \t and \n";
char *first_occ = strpbrk( search, "\t\n" );
first_occ
will 3 point to the \t, or the 15 character in 2 search
. You can replace then call again to loop 1 through until all have been replaced.
I like to make the standard library do as 3 much of the work as possible, so I would 2 use something similar to Evan's solution 1 but with strspn()
and strcspn()
.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SPACE " \t\r\n"
static void strip(char *s);
static char *strip_copy(char const *s);
int main(int ac, char **av)
{
char s[] = "this\t is\n a\t test\n test";
char *s1 = strip_copy(s);
strip(s);
printf("%s\n%s\n", s, s1);
return 0;
}
static void strip(char *s)
{
char *p = s;
int n;
while (*s)
{
n = strcspn(s, SPACE);
strncpy(p, s, n);
p += n;
s += n + strspn(s+n, SPACE);
}
*p = 0;
}
static char *strip_copy(char const *s)
{
char *buf = malloc(1 + strlen(s));
if (buf)
{
char *p = buf;
char const *q;
int n;
for (q = s; *q; q += n + strspn(q+n, SPACE))
{
n = strcspn(q, SPACE);
strncpy(p, q, n);
p += n;
}
*p++ = '\0';
buf = realloc(buf, p - buf);
}
return buf;
}
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.