3921 - 重写字符串

通过次数

0

提交次数

0

时间限制 : 1 秒 内存限制 : 128 MB
把给出的字符串,挑出其中的大写字母,重新组成一个新串
如果不存在大写字母就输出"NO"

题目输入

每组测试数据输入一行字符串,只包含大小写字母

题目输出

每组测试数据先输出,Case #x: y

再在接下来的一行输出答案

每组测试数据间保留一个空行

输入/输出样例

输入格式

AdsDCSADdsa

输出格式

Case #1:
ADCSAD

C++解答

#include <cstdio>
#include <cstring>
#define MAXN 10000000

char str[MAXN];

int main(){
    int x=1;
while(scanf("%s",&str)!=EOF){
       int count=0;
       printf("Case #%d:\n",x++);
       for(int i=0;str[i];i++){
            if(str[i]>='A'&&str[i]<='Z'){
                   printf("%c",str[i]);
                   count++;
             }
         }
       if(count==0){
             printf("NO");
        }
        printf("\n\n");
}
return 0;
}


Java解答



import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner      in    = new Scanner(System.in);
        String       str   = null;
        StringBuffer temp  = new StringBuffer();
        boolean      flag  = false;
        int          count = 0;

        while (in.hasNext())
        {
            str = in.nextLine();
            char[] ch = str.toCharArray();

            for (int i = 0; i < ch.length; i++)
            {
                if (ch[i] >= 'A' && ch[i] <= 'Z')
                {
                    flag = true;
                    temp.append(ch[i]);
                }
            }

            System.out.println("Case #" + ++count + ":");
            if (flag)
            {
                System.out.println(temp);
                flag = false;
                temp.delete(0,temp.length());
            }
            else
            {
                System.out.println("NO");
            }
            System.out.println();
        }
    }
}