如何在C中删除字符串中的空格?
我正在尝试下面的代码,但得到了错误的输出。例如,我键入“abc”,希望结果是“abc”,但结果是一个汉字如何在C中删除字符串中的空格?,c,string,space,C,String,Space,我正在尝试下面的代码,但得到了错误的输出。例如,我键入“abc”,希望结果是“abc”,但结果是一个汉字 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <string.h> /* function prototype */ char *sweepSpace(char *sentence); int main() { char str[80]; printf("Enter a stri
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
/* function prototype */
char *sweepSpace(char *sentence);
int main()
{
char str[80];
printf("Enter a string: ");//enter a string for example "a b c'
gets(str);
printf("Result: %s ", sweepSpace(str));//should print "abc"here
return 0;
}
char *sweepSpace(char *setence)
{
char b[80];
int i = 0;
while (*setence != NULL)
{
//if not reach the end
if (!isspace(*setence))
{
//if not a space
b[i] = *setence;//assign setence to b
i++;//increment
}
setence++;//pointer increment
}
b[i]= "\0";
return b;//return b to print
}
\define\u CRT\u SECURE\u NO\u警告
#包括
#包括
/*功能原型*/
字符*扫描空间(字符*句子);
int main()
{
char-str[80];
printf(“输入字符串:”;//输入字符串,例如“a b c”
获取(str);
printf(“结果:%s”,sweepSpace(str));//应在此处打印“abc”
返回0;
}
字符*扫描空间(字符*设置)
{
charb[80];
int i=0;
while(*setence!=NULL)
{
//如果没有达到终点
如果(!isspace(*setence))
{
//如果不是空间
b[i]=*设置;//将设置分配给b
i++;//增量
}
setence++;//指针增量
}
b[i]=“0”;
返回b;//返回b进行打印
}
b
的作用域在函数中。请将结果复制回原始指针
比如:
strcpy(setence,b);
b
的作用域在函数中。将结果复制回原始指针
比如:
strcpy(setence,b);
b
的作用域在函数中。将结果复制回原始指针
比如:
strcpy(setence,b);
b
的作用域在函数中。将结果复制回原始指针
比如:
strcpy(setence,b);
您正在返回一个本地数组变量(b
),当在main()中访问该变量时,该变量将调用未定义的行为
不要这样做
在函数结束前将新字符串复制回:
strcpy(setence, b);
还有一些注意事项:
它的拼写是句子
检查'\0'
,而不是NULL
使用isspace()
时强制转换为unsigned int
终止符是'\0'
,而不是“\0”
您正在返回一个本地数组变量(b
),当在main()
中访问该变量时,该变量将调用未定义的行为
不要这样做
在函数结束前将新字符串复制回:
strcpy(setence, b);
还有一些注意事项:
它的拼写是句子
检查'\0'
,而不是NULL
使用isspace()
时强制转换为unsigned int
终止符是'\0'
,而不是“\0”
您正在返回一个本地数组变量(b
),当在main()
中访问该变量时,该变量将调用未定义的行为
不要这样做
在函数结束前将新字符串复制回:
strcpy(setence, b);
还有一些注意事项:
它的拼写是句子
检查'\0'
,而不是NULL
使用isspace()
时强制转换为unsigned int
终止符是'\0'
,而不是“\0”
您正在返回一个本地数组变量(b
),当在main()
中访问该变量时,该变量将调用未定义的行为
不要这样做
在函数结束前将新字符串复制回:
strcpy(setence, b);
还有一些注意事项:
它的拼写是句子
检查'\0'
,而不是NULL
使用isspace()
时强制转换为unsigned int
终止符是'\0'
,而不是“\0”
使用此代码,您试图返回b
,它是在stack
上定义的。一旦超出范围,它将不会反映在main
中
char b[80];
..
..
return b;//return b to print
而是返回new\u b
char* new_b = (char*)malloc(strlen(b)+1);
strncpy(new_b,b,strlen(b)+1);
return new_b;//return b to print
使用此代码,您试图返回b
,它是在stack
上定义的。一旦超出范围,它将不会反映在main
中
char b[80];
..
..
return b;//return b to print
而是返回new\u b
char* new_b = (char*)malloc(strlen(b)+1);
strncpy(new_b,b,strlen(b)+1);
return new_b;//return b to print
使用此代码,您试图返回b
,它是在stack
上定义的。一旦超出范围,它将不会反映在main
中
char b[80];
..
..
return b;//return b to print
而是返回new\u b
char* new_b = (char*)malloc(strlen(b)+1);
strncpy(new_b,b,strlen(b)+1);
return new_b;//return b to print
使用此代码,您试图返回b
,它是在stack
上定义的。一旦超出范围,它将不会反映在main
中
char b[80];
..
..
return b;//return b to print
而是返回new\u b
char* new_b = (char*)malloc(strlen(b)+1);
strncpy(new_b,b,strlen(b)+1);
return new_b;//return b to print
无法在C
中返回自动数组。当函数sweepSpace
返回时,数组b
超出范围(在堆栈上分配)然后将内存位置的地址返回到不再可用的main
。这将导致未定义的行为,并可能导致segfault。此外,切勿使用gets
。它不会检查写入的缓冲区的边界,如果输入字符串太大,可能会使缓冲区溢出。请使用fgets
inste这将再次导致错误。以下是我的建议
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <ctype.h> // for prototype of isspace
#include <stdlib.h> // for prototype of malloc
char *sweepSpace(char *sentence);
int main(void) // parameter list should contain void explicitly
{
char str[80];
printf("Enter a string: "); //enter a string for example "a b c'
fgets(str, 80, stdin); // read at most 79 chars. add null byte at the end
char *new_sentence = sweepSpace(str);
if(new_sentence) {
printf("Result: %s ", new_sentence); //should print "abc" here
free(new_sentence);
new_sentence = NULL;
}
return 0;
}
char *sweepSpace(char *sentence)
{
char *b = malloc(1 + strlen(sentence)); // +1 for the null byte
if(b == NULL) {
printf("Not enough memory");
return NULL;
}
int i = 0;
while(*sentence != '\0') // compare with null byte, not NULL pointer
{
//if not reach the end
if (!isspace(*sentence))
{
// if not a space
b[i] = *sentence; //assign sentence to b
i++; //increment
}
sentence++; //pointer increment
}
b[i]= '\0'; // "\0" is a string literal, not a character.
return b; //return b to print
}
\define\u CRT\u SECURE\u NO\u警告
#包括
#包括
#包括//用于isspace原型
#包括//用于malloc原型
字符*扫描空间(字符*句子);
int main(void)//参数列表应显式包含void
{
char-str[80];
printf(“输入字符串:”;//输入字符串,例如“a b c”
fgets(str,80,stdin);//最多读取79个字符。在末尾添加空字节
char*new_句子=sweepSpace(str);
如果(新句子){
printf(“结果:%s,新句子);//应在此处打印“abc”
免费(新句子);
新句子=空;
}
返回0;
}
字符*扫描空间(字符*句子)
{
char*b=malloc(1+strlen(句子));//+1表示空字节
如果(b==NULL){
printf(“内存不足”);
返回NULL;
}
int i=0;
while(*句子!='\0')//与空字节比较,而不是空指针
{
//如果没有达到终点
如果(!isspace(*句))
{