C语言编程练习5:词组缩写

  要注意

所有单词由一个或多个空格分隔,有如下几种特殊情况;

1,“end of file ”
3,“  end    of   file”
3,“end    of   file  ”
4,“  end    of   file  ”
#include <stdio.h>
#include <iostream>
#include <cstring>

using namespace std;

int main()
{
	int n;
	char s[110];
	scanf("%d",&n);
	getchar();//取换行符
	while(n--)
	{
		gets(s);//不要用scanf,cin遇到空格会默认结束
		for(int i = 0;i < (int)strlen(s);i++)
		{
			if(i==0)//单独考虑第一个字符
			{
				if(s[i] >= 'a'&& s[i] <= 'z')
				{
					s[i]-=32;
					printf("%c",s[i]);
				}
				else if(s[i] >= 'A'&& s[i] <= 'Z')
				{
					printf("%c",s[i]);
				}
			}
			if(s[i] == ' ')//跳过所有空格
			{
				while(s[i] == ' ')
				{
					i++;
				}
				if(s[i] >= 'a'&& s[i] <= 'z')//空格后遇到的第一个字符
				{
					s[i]-=32;
					printf("%c",s[i]);
				}
				else if(s[i] >= 'A'&& s[i] <= 'Z')
				{
					printf("%c",s[i]);
				}
			}
		}
		printf("\n");
	}
	return 0;
}

 

posted @ 2021-01-20 19:43  FantasticDoulbeFish  阅读(501)  评论(0编辑  收藏  举报