给定字符串(如:ab \t \nc ),我如何忽略空格并在c中获取abc?
我知道如何跳过真正的选项卡和空白:
if(str[i] == ' ' || str[i] = '\t')但是如果我严格地用\t传递字符串,那么我将得到str[i]=\和str[i+1]=t。我怎么才能抓到这些案子?
例如:
char* str = "abcd \n \t ef ";
char* str_clear = filter(str); // need to be "abcdef".我询问如何编写过滤器函数(就像我上面写的那样,我知道如何跳过'‘和’‘,但是如何捕获"\n“和”t“呢?)
发布于 2022-09-15 21:32:18
OP中条件的第二部分使用=,其中==显然是有意使用的。
给你..。
if(str[i] == ' ' || str[i] == '\t' || ( str[i] == '\\' && str[i+1] == 't' ) )更好的是:
#include <ctype.h> // use this
if( isspace( str[i] )
|| ( str[i] == '\\' && ( str[i+1] == 't' || str[i+1] == 'n' ) ) )不能真正看到这个的用途,但是OP清楚地说这是想要的。
这是一个字符串,所以允许“嗅探”下一个字符。最坏的情况下,下一个字符是'\0‘。
如果源字符串正在压缩到另一个缓冲区中;
if( isspace( str[i] ) )
i++; // ignore one character
else if( str[i] == '\\' && ( str[i+1] == 't' || str[i+1] == 'n' ) )
i += 2; // ignore two characters
else
dst[ j++ ] = str[ i++ ];https://stackoverflow.com/questions/73737373
复制相似问题