L1-3 敲笨钟

代码

#include <stdio.h>
#include <string.h>
//处理的方式和我考虑的一样,但是有一点更为巧妙一点
//它将空格的位置固定在了倒数第三个空格上,这确实更符合常理也更容易输出
int main(){
	int n,i,j;
	int len,flagA,flagB;
	int count,pos;
	char s[101];
	scanf("%d",&n);
	getchar();	//接收回车字符 
	for(i=0;i<n;i++){
		len=0,flagA=0,flagB=0,count=0;
		gets(s);
		len=strlen(s);	//头文件<string.h>
		for(j=0;j<len;j++){//用两个flag来判断是否是符合条件
			if(s[j]==','&&s[j-3]=='o'&&s[j-2]=='n'&&s[j-1]=='g')
				flagA=1;
			if(s[j]=='.'&&s[j-3]=='o'&&s[j-2]=='n'&&s[j-1]=='g')
				flagB=1;
		}
		if(flagA==1&&flagB==1){//符合条件的话,找到第三个空格
			for(j=len-1;j>=0;j--){	//倒序遍历字符
				if(s[j]==' ')		//判断空格
					count++;
				if(count==3){
					pos=j;	//将第三个空格的下标进行存储
					break;
				}
			}
			for(j=0;j<=pos;j++){//输出到第三个空格
				printf("%c",s[j]);
			}
			printf("qiao ben zhong.\n");//符合条件,改为需要的输出
		}else{//否则  原样输出
			printf("Skipped\n");
		}
	}
	return 0;
} 

posted @ 2023-04-21 10:14  天黑星更亮  阅读(64)  评论(0)    收藏  举报