Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove \n or \t from a given string in C?

Tags:

c

string

How can I strip a string with all \n and \t in C?

like image 652
goe Avatar asked Oct 03 '09 23:10

goe


People also ask

Is \n considered a character in C?

"\n" is a character. char[] to represent a string needs "0" to mark the end of the string. In order to do that, char array requires one more element to store "0" but it is not counted as a string size.

What does Strcspn function do?

The C library function strcspn() calculates the length of the number of characters before the 1st occurrence of character present in both the string.

Does Fgets read newline?

The fgets function reads characters from the stream stream up to and including a newline character and stores them in the string s , adding a null character to mark the end of the string. You must supply count characters worth of space in s , but the number of characters read is at most count - 1.


2 Answers

This works in my quick and dirty tests. Does 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 a version which will make a place the result in a newly malloced buffer and return it (thus it'll work on literals). You will need to 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;
}
like image 103
Evan Teran Avatar answered Sep 19 '22 14:09

Evan Teran


If you want to replace \n or \t with something else, you can use the function strstr(). It returns a pointer to the first place in a function that has a certain string. For 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 all \n's and \t's.

If you want to "strip" them, i.e. remove them from the string, you'll need to actually use the same method as above, but copy the contents of the string "back" every time you find a \n or \t, so that "this i\ns a test" becomes: "this is a test".

You can do that with memmove (not memcpy, since the src and dst are pointing to overlapping 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 loop again to remove the \t's.

Note: Both of these methods work in-place. This might not be safe! (read Evan Teran's comments for details.. Also, these methods are not very efficient, although they do utilize a library function for some of the code instead of rolling your own.

like image 45
Edan Maor Avatar answered Sep 22 '22 14:09

Edan Maor