请用C语言编写一个函数,用来删除字符串中的所有空格,加上注释哟

2024-12-02 22:10:05
推荐回答(3个)
回答1:

很简单的程序,遍历输入字符串。
1、如果字符不是空格,就赋值到输出字符串中。
2、如果是空格,就跳过这个字符。
例如:
#include
#include

int main()
{
const char * input = "Hello World! Welcome To Beijing!";
char output[1024];
int i, j, input_len;

input_len = strlen(input);
j = 0;
for(i = 0; i < input_len; i++)
{
if (input[i] != ' ')
{
output[j] = input[i];
j++;
}
}
output[j] = '\0';

printf("Input string is: %s\n", input);
printf("After spaces were removed: %s\n", output);
return 0;
}

具体的输出效果为:
Input string is: Hello World! Welcome To Beijing!
After spaces were removed: HelloWorld!WelcomeToBeijing!

回答2:

很简单的程序,遍历输入字符串,如果字符不是空格,就赋值到输出字符串中,如果是空格,就跳过这个字符。

#include
#include

int main()
{
const char * input = "Hello World! Welcome To Beijing!";
char output[1024];
int i, j, input_len;

input_len = strlen(input);
j = 0;
for(i = 0; i < input_len; i++)
{
if (input[i] != ' ')
{
output[j] = input[i];
j++;
}
}
output[j] = '\0';

printf("Input string is: %s\n", input);
printf("After spaces were removed: %s\n", output);
return 0;
}

具体的输出效果为:
Input string is: Hello World! Welcome To Beijing!
After spaces were removed: HelloWorld!WelcomeToBeijing!

回答3:

函数库有自带的这类函数