[英]how to remove space in a string in C?
I'm trying the code below but get a wrong output. 我正在尝试下面的代码,但输出错误。 For example I type "abc" and I want the result to be "abc", but the result is a chinese character. 例如,我键入“ 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 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
}
You are returning a local array variable ( b
), which invokes undefined behavior when it's accessed in main()
. 您将返回一个局部数组变量( b
),该变量在main()
访问时会调用未定义的行为。
Don't do this. 不要这样
Copy the new string back before the function ends: 函数结束之前,将新字符串复制回去:
strcpy(setence, b);
Some more notes: 更多注意事项:
sentence
. 这是拼写的sentence
。 '\\0'
, not NULL
. 检查'\\0'
,而不是NULL
。 unsigned int
when using isspace()
. 使用isspace()
时强制转换为unsigned int
。 '\\0'
, not "\\0"
. 终止符是'\\0'
,而不是"\\0"
。 b
is scoped in function. b
功能范围。 Copy back the result to your original pointer. 将结果复制回原始指针。
Something like: 就像是:
strcpy(setence, b);
With this code you are trying to return b
, which is defined on stack
. 使用此代码,您将尝试返回b
,该b
是在stack
上定义的。 Once you are out of scope it will not reflect in main
. 一旦超出范围,它将不会反映在main
。
char b[80];
..
..
return b;//return b to print
Instead, you return new_b
. 相反,您返回new_b
。
char* new_b = (char*)malloc(strlen(b)+1);
strncpy(new_b,b,strlen(b)+1);
return new_b;//return b to print
You can't return an automatic array in C
. 您不能在C
返回自动数组。 When the function sweepSpace
returns, the array b
goes out of scope (it's allocated on the stack) and you return the address of a memory location to main
which is no longer available. 当函数sweepSpace
返回时,数组b
超出范围(在堆栈上分配),并且您将不再可用的内存位置地址返回给main
。 This would cause undefined behaviour and likely result in segfault. 这将导致不确定的行为,并可能导致段错误。 Also, never use gets
. 另外,请勿使用gets
。 It does not check the bound of the buffer it writes into and can overrun the buffer if the input string is too large. 它不检查它写入的缓冲区的边界,如果输入字符串太大,它可能会溢出缓冲区。 Use fgets
instead. 改用fgets
。 This would again lead to error. 这将再次导致错误。 Here's what I suggest. 这是我的建议。
#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
}
char *a="hello world";
int i;
int w=strlen(a);
for(i=0; i<=w; i++) {
if(*(a+i)==' ') {
for(j=i;j<=w-1;j++) {
*(a+i)=*(a+1);
} }
}
/* if there is a space, push all next character one byte back */
this should work. 这应该工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.