本文共 1180 字,大约阅读时间需要 3 分钟。
#includeusing 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/