【每日一题】2012.5.27:删除多余的空格-非原创
2012-05-27 16:58
232 查看
#include <stdlib.h> #include <stdio.h> #include <assert.h> #include <string.h> int Remove(char str[]) { if (NULL == str) { return 0; } //删除空格计数 int removeCnt = 0; //扫描头 int i = 0; //当前字符为'\n',new_line为真,初始为真,可去除开头的空格 bool newline = true; while (str[i]) { str[i - removeCnt] = str[i]; //当前为空格 if (' ' == str[i]) { //新行后的空格,应该删除 if (newline) { ++removeCnt; } //空格后的空格,应该删除 else if (' ' == str[i + 1]) { ++removeCnt; } //换行前的空格,应该删除 else if ('\n' == str[i + 1]) { ++removeCnt; } //结尾的空格,应该删除 else if (0 == str[i + 1]) { ++removeCnt; } } else if ('\n' == str[i]) { newline = true; } else { newline = false; } ++i; } str[i - removeCnt] = 0; return removeCnt; } void main() { char s[] = "abc \n b d "; printf("%d\n",Remove(s)); puts(s); }
相关文章推荐
- 【每日一题】2012.5.27:删除多余空格-非原创-方法2
- 老男孩教育每日一题-2017年4月13日-当你删除的多个文件中包含空格或其他特殊字符,请问如何做
- 新浪微博笔试题:删除字符串中多余的空格
- 如何删除word中多余的空格和空行
- 单词首字母大写,删除多余空格
- 删除字符串中多余的空格
- 【原创】如何删除明细库中多余的数…
- 删除字符串多余空格
- 删除多余空格
- 删除字符串中多余的空格]给定字符串,删除开始和结尾处的空格,并将中间的多个连续的空格合并成一个
- vector应用,删除句子中多余空格与换行
- 压缩aspx页面删除多余空格的两种方法
- 删除字符串中多余的空格 shrink_space
- 删除字符串中多余的空格 美团校园招聘模拟测试题
- 面试100题系列之2字符串删除多余空格
- 删除字符串中的多余空格
- 删除字符串多余空格
- C语言练习之删除字符间多余的空格
- C语言删除一个字符串中的多余空格字符
- 删除多余的空格