如何在C语言中删除给定字符串中的所有空格和制表符?
什么样的C函数(如果有的话)从字符串中删除所有前面的空格和制表符?在C中,字符串由指针标识,例如如何在C语言中删除给定字符串中的所有空格和制表符?,c,string,tabs,spaces,C,String,Tabs,Spaces,什么样的C函数(如果有的话)从字符串中删除所有前面的空格和制表符?在C中,字符串由指针标识,例如char*str,或者可能是数组。无论哪种方式,我们都可以声明自己的指针,该指针将指向字符串的开头: char *c = str; 然后,我们可以使指针移动过任何类似空格的字符: while (isspace(*c)) ++c; 这将向前移动指针,直到它不指向空格,即在任何前导空格或制表符之后。这使原始字符串保持不变-我们刚刚更改了指针c指向的位置 要获取isspace,您将需要此包含:
char*str
,或者可能是数组。无论哪种方式,我们都可以声明自己的指针,该指针将指向字符串的开头:
char *c = str;
然后,我们可以使指针移动过任何类似空格的字符:
while (isspace(*c))
++c;
这将向前移动指针,直到它不指向空格,即在任何前导空格或制表符之后。这使原始字符串保持不变-我们刚刚更改了指针c
指向的位置
要获取isspace
,您将需要此包含:
#include <ctype.h>
void trim(常量char*src、char*buff、常量unsigned int sizeBuff)
{
如果(sizeBuff<1)
返回;
常量字符*当前=src;
无符号整数i=0;
而(当前!='\0'&&i
你只需要给buff足够的空间。一个更简单的函数来修剪空白
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char * trim(char * buff);
int main()
{
char buff[29];
strcpy(buff, " \r\n\t abcde \r\t\n ");
char* out = trim(buff);
printf(">>>>%s<<<<\n",out);
}
char * trim(char * buff)
{
//PRECEDING CHARACTERS
int x = 0;
while(1==1)
{
if((*buff == ' ') || (*buff == '\t') || (*buff == '\r') || (*buff == '\n'))
{
x++;
++buff;
}
else
break;
}
printf("PRECEDING spaces : %d\n",x);
//TRAILING CHARACTERS
int y = strlen(buff)-1;
while(1==1)
{
if(buff[y] == ' ' || (buff[y] == '\t') || (buff[y] == '\r') || (buff[y] == '\n'))
{
y--;
}
else
break;
}
y = strlen(buff)-y;
printf("TRAILING spaces : %d\n",y);
buff[strlen(buff)-y+1]='\0';
return buff;
}
#包括
#包括
#包括
字符*修剪(字符*浅黄色);
int main()
{
字符buff[29];
strcpy(buff,“\r\n\t abcde\r\t\n”);
char*out=修剪(浅黄色);
printf(“>>>>>%s您可以设置一个计数器来计算相应的空格数,并相应地将字符移位那么多空格。其复杂性最终为O(n)
void removeSpaces(char *str) {
// To keep track of non-space character count
int count = 0;
// Traverse the given string. If current character
// is not space, then place it at index 'count++'
for (int i = 0; str[i]; i++)
if (str[i] != ' ')
str[count++] = str[i]; // here count is
// incremented
str[count] = '\0';
}
从技术上讲,这将移动指针“c”"对于第一个非空格字符,假设它从字符串的开头开始。它根本不会更改字符串。我指出这一点是因为如果您有另一个指向字符串开头的指针,它仍然会指向字符串的原始开头,其中可能有空格。很明显,这种技术是非常首选的,因为你没有在字符串中移动位,但你必须意识到你在移动指针,而不是消除空格。你必须很好地理解指针才能理解差异。好的一点,我希望改进解释来涵盖这一点。哎呀,我以为你想删除给定字符串中的“所有”空格和制表符。我错过了“在前”。希望它能帮助其他人。:)您可以就地压缩,因为输出小于输入。问题标题要求“删除所有空格”;问题正文要求“删除所有前面的空格”。您的意思是“删除前导空格和制表符”还是“删除所有空格和制表符”?您只需知道第一个非空白字符的位置,还是需要移动将字符强制添加到字符串中的起始位置。所有这些都是有效的要求-不完全清楚您要查找的是哪一个。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char * trim(char * buff);
int main()
{
char buff[29];
strcpy(buff, " \r\n\t abcde \r\t\n ");
char* out = trim(buff);
printf(">>>>%s<<<<\n",out);
}
char * trim(char * buff)
{
//PRECEDING CHARACTERS
int x = 0;
while(1==1)
{
if((*buff == ' ') || (*buff == '\t') || (*buff == '\r') || (*buff == '\n'))
{
x++;
++buff;
}
else
break;
}
printf("PRECEDING spaces : %d\n",x);
//TRAILING CHARACTERS
int y = strlen(buff)-1;
while(1==1)
{
if(buff[y] == ' ' || (buff[y] == '\t') || (buff[y] == '\r') || (buff[y] == '\n'))
{
y--;
}
else
break;
}
y = strlen(buff)-y;
printf("TRAILING spaces : %d\n",y);
buff[strlen(buff)-y+1]='\0';
return buff;
}
void removeSpaces(char *str) {
// To keep track of non-space character count
int count = 0;
// Traverse the given string. If current character
// is not space, then place it at index 'count++'
for (int i = 0; str[i]; i++)
if (str[i] != ' ')
str[count++] = str[i]; // here count is
// incremented
str[count] = '\0';
}