c语言编程练习题:7-174 字母串
作者:yunjinqi   类别:    日期:2023-05-31 18:35:21    阅读:248 次   消耗积分:0 分    

image.png

#include <stdio.h>
int is_alpha(char c){
    int num=0;
    if (c>='a' && c<='z'){num=1;}
    if (c>='A' && c<='Z'){num=1;}
    return num;
}
int print_value(){
    char lower_alpha[27] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
    char upper_alpha[27] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
    char pre_c='0',c,new_c_1,new_c_2;
    int bool_value = 1;
    while (scanf("%c",&c) && is_alpha(c)){
        // printf("now char is %c",c);
        if (pre_c!='0' ){
            // 如果是小写字母
            // 如果写了某个小写字母,下一个就必须写同个字母的大写,或者写字母表中前一个字母的小写;
            if (pre_c>='a' && pre_c<='z'){
                for (int i=0;i<26;i++){
                    if (lower_alpha[i]==pre_c){
                        new_c_1 = upper_alpha[i];
                        if (i!=0){new_c_2=lower_alpha[i-1];}
                        else{new_c_2='0';}
                        if (c!=new_c_1 && c!=new_c_2){bool_value =0;break;}
                        }
                }
            //printf("Y");
            // 如果是大写字母
            }else{
                for (int i=0;i<26;i++){
                        if (upper_alpha[i]==pre_c){
                            new_c_1 = lower_alpha[i];
                            if (i<25){new_c_2=upper_alpha[i+1];}
                            else{new_c_2='0';}
                            if (c!=new_c_1 && c!=new_c_2){bool_value =0;break;}
                            }
                    }
                }
        
        }
        pre_c = c;
        // printf("pre_c = %c c = %c bool_value = %d\n",pre_c,c,bool_value);
    }
    if (bool_value==1){printf("Y\n");}
    else{printf("N\n");}
    return 0;
}
int main(){
    
    int n;
    if (scanf("%d ",&n)!=EOF){
        for (int j=0;j<n;j++){
            print_value();
        }      
    }
    return 0;
}


版权所有,转载本站文章请注明出处:云子量化, http://www.woniunote.com/article/299
上一篇:c语言编程练习题:7-173 英文字母替换加密(大小写转换 后移1位)
下一篇:c语言编程练习题:7-176 降价提醒机器人