请用C语言编写一个函数,用来删除字符串中的所有空格,加上注释哟
答案:4 悬赏:70
解决时间 2021-04-28 08:51
- 提问者网友:巴黎塔下许过得承诺
- 2021-04-28 04:43
请用C语言编写一个函数,用来删除字符串中的所有空格,加上注释哟
最佳答案
- 二级知识专家网友:伤口狠精致
- 2021-04-28 06:01
很简单的程序,遍历输入字符串,如果字符不是空格,就赋值到输出字符串中,如果是空格,就跳过这个字符。
#include <stdio.h>
#include <string.h>
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!
#include <stdio.h>
#include <string.h>
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!
全部回答
- 1楼网友:厭世為王
- 2021-04-28 08:51
#include
#define n 60 // 数值自己确定
char delete(char s[ ])
{ int i,j=0;
char c[n];
for(i=1;i<=n;i++)
{
if(s[i]!=' ') c[j++]=s[i];
if(s[i]==俯供碘佳鄢簧碉伪冬镰9;\0')) break;
}
return c[n];
}
main()
{
char a[n]={“asd de”};//数值数组自己设定,也可改成从键盘输入的,随意
printf(“%c”,delete( a[n]));
}
——物联网校企联盟 技术部
- 2楼网友:何以畏孤独
- 2021-04-28 07:30
很简单的程序,遍历输入字符串,如果字符不是空格,就赋值到输出字符串中,如果是空格,就跳过这个字符。
#include <stdio.h>
#include <string.h>
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楼网友:一起来看看吧
- 2021-04-28 06:08
很简单的程序,遍历输入字符串。
1、如果字符不是空格,就赋值到输出字符串中。
2、如果是空格,就跳过这个字符。
例如:
#include <stdio.h>
#include <string.h>
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!
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息!
大家都在看
推荐信息