• 词组缩写(isalpha()的应用)


    Problem Description
    定义:一个词组中每个单词的首字母的大写组合称为该词组的缩写。
    比如,C语言里常用的EOF就是end of file的缩写。
     
    Input
    输入的第一行是一个整数T,表示一共有T组测试数据;
    接下来有T行,每组测试数据占一行,每行有一个词组,每个词组由一个或多个单词组成;每组的单词个数不超过10个,每个单词有一个或多个大写或小写字母组成;
    单词长度不超过10,由一个或多个空格分隔这些单词。
     
    Output
    请为每组测试数据输出规定的缩写,每组输出占一行。
     
    Sample Input
    1 end of file
     
    Sample Output
    EOF
    本题要考虑的情况:
       asd   asd
    asd asd   asd
     
     
     1 #include <stdio.h>
     2 #include <math.h>
     3 #include <queue>
     4 #include <vector>
     5 #include <stack>
     6 #include <map>
     7 #include <string>
     8 #include <string.h>
     9 #include <algorithm>
    10 #include <iostream>
    11 using namespace std;
    12 char str[1000],s[1000];
    13 int main( )
    14 {
    15     int t;
    16     scanf( "%d%*c",&t );
    17     while( t-- )
    18     {
    19            gets( str );
    20            int i = 0,c = 0;
    21            while( str[i] )
    22            {
    23                   if( !isalpha( str[i] ) )//如果输入字符是一个英文字母,即 a-z或A-Z,返回非零值(具体返回多少要看系统实现),否则返回0.
    24                   {
    25                       ++i;
    26                       continue;
    27                   }
    28                   if( str[i] > 'Z' )
    29                       str[i] -= 32;
    30                   s[c++] = str[i];
    31                   while( isalpha( str[i] ) )
    32                          ++i;
    33                   }
    34              s[c] = 0;
    35            puts( s );
    36            }
    37     return 0;
    38 }
    分解:
     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 #include<string.h>
     4 char a[120],b[120];
     5 int main()
     6 {
     7     int T;
     8     int i;
     9     int len=0;
    10     int k;
    11     scanf("%d",&T);
    12     getchar();
    13     while(T--)
    14     {
    15         gets(a);
    16         len=strlen(a);
    17         strupr(a);
    18         k=0;
    19         for(i=0;i<len;i++)
    20         {
    21             if(i==0)
    22             {
    23                 if(a[i]==' ' && a[i+1]!=' ')
    24                      b[k++]=a[i+1];
    25                 else if(a[i]!=' ')
    26                     b[k++]=a[i];
    27             }
    28             else 
    29             {
    30                 if(a[i]==' ' && a[i+1])
    31                     b[k++]=a[i+1];
    32             }
    33         }
    34         for(i=0;i<k;i++)
    35         {
    36             if(b[i]!=' ')
    37                 printf("%c",b[i]);
    38         }
    39         printf("
    ");
    40     }
    41     return 0;
    42 }
     
  • 相关阅读:
    写简单游戏,学编程语言-python篇
    RSS阅读器python实现概述
    python简易爬虫来实现自动图片下载
    SQL SERVER 2008 R2 SP3 发布
    动态规划问题总结 (转载)
    typedef with const 联合的说明
    C++ 初始化列表
    C++de构造函数
    排序算法温习
    java中读取properties配置文件用例
  • 原文地址:https://www.cnblogs.com/wangmengmeng/p/4714966.html
Copyright © 2020-2023  润新知