博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C 字符串读入与取出空白符
阅读量:4216 次
发布时间:2019-05-26

本文共 1180 字,大约阅读时间需要 3 分钟。

去掉所有首尾空白;中间的多个空白替换为一个空格。所谓空白指的是:空格、制表符、回车符。 

#include
using namespace std;const int BUF_SIZE = 1024;char a[100],b[100];/* 读取字符串 (包含空格) */ void read_line(char *str) { char c; int i = 0; while(isspace(c = getchar()))//防止换行符 等空白字符 ; str[i++] = c; //这里要注意 不能丢弃第一个字符 while((c = getchar()) != '\n' && c != EOF){ if(i < BUF_SIZE){ str[i++] = c; } } str[i] = '\0';//不要忘记末尾结束符 } /* 取出首尾空白符以及把中间多个空白符置为一个空格,包括(/n,/t ,' ') */ void f(char *from, char *to) { char *p_from = from; char *p_to = to; while(isspace(*p_from)) p_from++; do{ if(isspace(*p_from)){ while(isspace(*p_from))//处理中间多个空白符情况 p_from++; if(*p_from)//判断是否到字符串末尾 *p_to++ = ' ';//此位置为空格 后往后移一位 } }while(*p_to++ = *p_from++); } /* char* fgets(char *s, int n, FILE *stream); char* gets(char *s); */ int main() { //read_line(a); char *p = fgets(a,sizeof(a),stdin); //从标准输入读取字符串 返回指针 f(p,b); printf("%s",b); return 0; }
int n;		char a[55];		scanf("%d",&n);		getchar();		while(n--){			fgets(a,sizeof(a),stdin);//从标准输入读到a中			a[strlen(a)-1] = '\0';//fgets 读取末尾的换行符 并保存 需要去除 			if(check(a)){				printf("yes\n");								}			else{				printf("no\n");			}		}

转载地址:http://orimi.baihongyu.com/

你可能感兴趣的文章
Java并发编程从入门到精通 张振华.Jack --我的书
查看>>
【屌丝程序的口才逆袭演讲稿50篇】第十二篇:世界上最快的捷径【张振华.Jack】
查看>>
Android中Java代码和XML布局效率问题
查看>>
android TextView属性大全(转)
查看>>
Conclusion for Resource Management
查看>>
Conclusion for Constructors,Destructors,and Assignment Operators
查看>>
Conclusion for Accustoming Yourself to C++
查看>>
面试题1:赋值运算函数(offer)
查看>>
Mark : MessagePack简介及使用
查看>>
Mark : hive文件存储格式
查看>>
mark : hadoop 四种压缩格式
查看>>
All Things OpenTSDB
查看>>
单例模式(singleton),工厂方法模式(factory),门面模式(facade)
查看>>
抽象模式,适配器模式(Adapter),模板方法模式(Template method)
查看>>
建造者模式(builder),桥梁模式(bridge mode),命令模式(Command mode)
查看>>
装饰模式(Decorator),迭代器模式(Iterator),组合模式(composite)
查看>>
观察者模式(Observer),责任链模式,访问者模式(Visitor)
查看>>
状态模式(State)
查看>>
快速排序
查看>>
插入算法
查看>>