Mimic Python strip () function in C

Recently, I started with a small toy project, and I scratched my head over the best way to simulate strip () functionality, which is part of python string objects.

Reading the fscanf or sscanf file indicates that the line is processed until the first space that occurs.

fgets doesn't help since I still have new lines. I tried strchr () to find spaces and set the return pointer to "\ 0" explicitly, but this does not seem to work.

+4
source share
6 answers

There is no standard C implementation for the strip () or trim () function. However, here, which is included in the Linux kernel:

char *strstrip(char *s) { size_t size; char *end; size = strlen(s); if (!size) return s; end = s + size - 1; while (end >= s && isspace(*end)) end--; *(end + 1) = '\0'; while (*s && isspace(*s)) s++; return s; } 
+9
source

The python string strip method removes both trailing and leading spaces. The two halves of the problem are very different when working on C "string" (array char, \ 0 completed).

For a trailing space: set the pointer (or equivalent index) to an existing trailing \ 0. Continue decreasing the pointer until it reaches the beginning of the line or any non-white character; set \ 0 to the right after this end point on the back.

For leading space: set the pointer (or equivalent index) to the beginning of the line; continue to increase the pointer until it reaches a non-white character (possibly the ending \ 0); memmove rest-of-string so that the first non-white goes to the beginning of the line (and similarly for all of the following).

+12
source

It looks like you want something like a finish, a quick google search leads to this forum.

0
source

If you want to remove, in place , the final newline in a line, you can use this snippet:

 size_t s = strlen(buf); if (s && (buf[s-1] == '\n')) buf[--s] = 0; 

To accurately emulate the Python method str.strip([chars]) (as I interpreted its work), you need to allocate space for a new line, fill in a new line and return it. After that, when you no longer need a trimmed line, you need to free the memory in which it was used so that there are no memory leaks.

Or you can use C pointers and change the original string and achieve a similar result.
Suppose your start line is "____forty two____\n" and you want to remove all underscores and '\ n'

 ____forty two___\n ^ ptr 

If you change ptr to "f" and replace the first "_" after two with '\0' , the result will be the same as Python "____forty two____\n".strip("_\n");

 ____forty two\0___\n ^ptr 

Again, this is not the same as Python. The line changes in place, there is no 2nd line, and you cannot undo the changes (the original line will be lost).

0
source

I wrote C code to implement this function. I also wrote some trivial tests to make sure my function does reasonable things.

This function writes to the buffer that you provide, and should never be written to the end of the buffer, so it should not be prone to buffer overflow problems.

Note: only Test () uses stdio.h, so if you just need a function, you need to include ctype.h (for isspace ()) and string.h (for strlen ()).

 // strstrip.c -- implement white space stripping for a string in C // // This code is released into the public domain. // // You may use it for any purpose whatsoever, and you don't need to advertise // where you got it, but you aren't allowed to sue me for giving you free // code; all the risk of using this is yours. #include <ctype.h> #include <stdio.h> #include <string.h> // strstrip() -- strip leading and trailing white space from a string // // Copies from sIn to sOut, writing at most lenOut characters. // // Returns number of characters in returned string, or -1 on an error. // If you get -1 back, then nothing was written to sOut at all. int strstrip(char *sOut, unsigned int lenOut, char const *sIn) { char const *pStart, *pEnd; unsigned int len; char *pOut; // if there is no room for any output, or a null pointer, return error! if (0 == lenOut || !sIn || !sOut) return -1; pStart = sIn; pEnd = sIn + strlen(sIn) - 1; // skip any leading whitespace while (*pStart && isspace(*pStart)) ++pStart; // skip any trailing whitespace while (pEnd >= sIn && isspace(*pEnd)) --pEnd; pOut = sOut; len = 0; // copy into output buffer while (pStart <= pEnd && len < lenOut - 1) { *pOut++ = *pStart++; ++len; } // ensure output buffer is properly terminated *pOut = '\0'; return len; } void Test(const char *s) { int len; char buf[1024]; len = strstrip(buf, sizeof(buf), s); if (!s) s = "**null**"; // don't ask printf to print a null string if (-1 == len) *buf = '\0'; // don't ask printf to print garbage from buf printf("Input: \"%s\" Result: \"%s\" (%d chars)\n", s, buf, len); } main() { Test(NULL); Test(""); Test(" "); Test(" "); Test("x"); Test(" x"); Test(" x "); Test(" xyz "); Test("xyz"); } 
0
source

I asked a very similar question a long time ago. See here ; There are ways to do this both locally and with a new copy.

0
source

All Articles