去除多余空格

A312289520 2010-10-15 03:01:53
最近我看到一个题目:编写一个函数,从一个字符串中去除多余的空格,当函数发现字符串中如果有一个地方由一个或多个连续的空格组成,就把它们改成单个空格字符!!!
哪位大侠有最精简的算法~~~????
用C语言编写!因为我目前只会C语言!
...全文
161 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
jznhljg 2010-10-15
  • 打赏
  • 举报
回复

Write a program to copy its input to its output, replacing each string of one or more blanks by a single blank.
#include <stdio.h>

int main(void)
{
int c;
int inspace;

inspace = 0;
while((c = getchar()) != EOF)
{
if(c == ' ')
{
if(inspace == 0)
{
inspace = 1;
putchar(c);
}
}

/* We haven't met 'else' yet, so we have to be a little clumsy */
if(c != ' ')
{
inspace = 0;
putchar(c);
}
}

return 0;
}

Chris Sidi writes: "instead of having an "inspace" boolean, you can keep track of the previous character and see if both the current character and previous character are spaces:"


#include <stdio.h>

/* count lines in input */
int
main()
{
int c, pc; /* c = character, pc = previous character */

/* set pc to a value that wouldn't match any character, in case
this program is ever modified to get rid of multiples of other
characters */

pc = EOF;

while ((c = getchar()) != EOF) {
if (c == ' ')
if (pc != ' ') /* or if (pc != c) */
putchar(c);

/* We haven't met 'else' yet, so we have to be a little clumsy */
if (c != ' ')
putchar(c);
pc = c;
}

return 0;
}


Stig writes: "I am hiding behind the fact that break is mentioned in the introduction"!

#include <stdio.h>

int main(void)
{
int c;
while ((c = getchar()) != EOF) {
if (c == ' ') {
putchar(c);
while((c = getchar()) == ' ' && c != EOF)
;
}
if (c == EOF)
break; /* the break keyword is mentioned
* in the introduction...
* */

putchar(c);
}
return 0;
}


<The C Programming Lauguage>
Sou2012 2010-10-15
  • 打赏
  • 举报
回复
写几个 trimLeft trimRight trim函数。
BT六眼飞鱼 2010-10-15
  • 打赏
  • 举报
回复

void trimStr(char* _src,char* _des)
{
bool isTrim = false;
while(*_src)
{
if(*_src == ' ')
{
if(!isTrim)
{
isTrim = true;
*_des++ = *_src++;
}
else
{
_src++;
}
continue;
}
isTrim = false;
*_des++ = *_src++;
}
_des = 0;
return;
}

BT六眼飞鱼 2010-10-15
  • 打赏
  • 举报
回复
额。。。没看清 写错了
BT六眼飞鱼 2010-10-15
  • 打赏
  • 举报
回复

void trimStr(char* _src,char* _des)
{
while(*_src)
{
if(*_src!=' ')
{
*_des = *_src;
++_des;
++_src;
}
else
{
++_src;
}
}
return;
}

luciferisnotsatan 2010-10-15
  • 打赏
  • 举报
回复
新建一个字符串str2,然后从原来的字符串str1一个一个字符拷贝到str2,并判断目前要复制的字符和前次要复制的字符是否都为空格,是的话就不复制这个空格。
最后再把str2整个复制到str1就可以了
资源下载链接为: https://pan.quark.cn/s/22ca96b7bd39 在编程领域,代码格式化和规范性极为重要。为此,一款名为“替换tab键并删除多余空格”的工具应运而生。它能够帮助开发者优化源代码,使其更符合编码规范,从而提升代码的可读性和团队协作效率。 这款用C语言编写的小程序主要执行以下三类操作:一是删除多余空格。许多编程规范要求代码行尾不得有多余空格,以保持整洁。该工具会自动检测并删除行尾的多余空格,确保代码符合规范。二是替换tab键。在C语言及其他一些编程语言中,通常推荐用4个空格代替tab键进行缩进。这是因为不同编辑器或IDE对tab键的显示宽度可能不同,而使用4个空格可以保证代码在任何环境下都具有一致的缩进效果。此工具可将源代码中的tab字符替换为4个空格。三是删除无用的空行。文件末尾的多余空行对编译器无意义,但会影响代码的美观。该工具会检查并删除文件末尾的多余空行。 该工具支持.c(C语言源代码)、.h(C语言头文件)、.s(汇编语言源代码)、.txt(纯文本文件)和.sct(特定脚本或配置文件)等多种文件类型,适用范围较广。此外,配套的使用说明.txt文件详细介绍了如何运行tab_space_del.exe,包括命令行参数的使用、操作示例以及可能遇到的问题和解决方案。 对于初学者或有特定代码格式化需求的开发者而言,这款工具非常实用。在实际开发中,代码格式化工具不仅能提高个人编码效率,还能促进团队合作,因为统一的代码风格便于协作。许多版本控制系统(如Git)也支持提交前自动格式化代码,以确保代码一致性。因此,掌握这类工具的使用,对提升编程实践的专业性大有帮助。

70,026

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧