如何跳过\t和\n以及c中所有其他特殊的空格字符



给定如下字符串: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".

我问如何编写过滤函数(就像我上面写的那样,我知道如何跳过"one_answers",但我如何才能捕获"\n"one_answers"\t"?(

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++ ];

最新更新