PAT_乙级_C++|散列|1033 旧键盘打字 (20分)

不知道为什么用scanf不行?
估计 是字符串的输入用getline比较好
就像c语言中scanf和gets的。区别吧

//gets在PAT用不了解决方法
https://blog.csdn.net/qq_36525099/article/details/86631881

在这里插入图片描述
c语言中数组的下标可以是字符吗?

C语言数组的标只能是合法的整型值。
当下标为字符时
以字符常量作为下标。如a[‘A’];表示’A’的ascii码对应的数组偏移量。
在实际应用中不常见,但是从语法上来说是合法的。

C++中strlen()函数

注意:一个中文字占两个字符

# include "stdio.h"
# include "iostream.h"
# include "string.h"

//关于strlen函数,求括号里字符串的长度 
void main()
{
    char a[10]={0};
    char b[10]={'a','s','d'};
    char c[10]="早上好";
    cout<<strlen(a)<<endl;//0
    cout<<strlen(b)<<endl;//3
    cout<<strlen(c)<<endl;//6

}
//坏键以大写给出
//上档键坏了,大写英文字母不出现
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

bool hashTable[256];//散列数组,用以记录键盘的字符是否完好//true表示所有键都完好
const int maxn = 100010;
char str[maxn];//每段文字是不超过 10^5个字符的串7_This_is_a_test.

int main(){
    memset(hashTable,true,sizeof(hashTable));
    cin.getline(str,maxn);//scanf("%s",&str);//读入所有失效的键位7+IE.
    int len = strlen(str);
    for(int i=0;i<len;i++){
        if(str[i]>='A'&&str[i]<='Z'){
            str[i]=str[i]-'A'+'a';//如果时大写字母,则转为小写字母
        }
        hashTable[str[i]]=false;//设置键str[i]失败
    }
    cin.getline(str,maxn);//读入要输入的字符串
    len=strlen(str);
    for(int i=0;i<len;i++){
        if(str[i]>='A'&&str[i]<='Z'){
            int low=str[i]-'A'+'a';//如果是大写字母,则化为小写字母
            if(hashTable[low]==true&&hashTable['+']==true){
                printf("%c",str[i]);//只有小写字母的键位与上档键逗号,才能输出大写字母
            }
        }else if(hashTable[str[i]]==true){
            printf("%c",str[i]);//对于其他自键,只要键位完好,输出
        }
    }
    printf("\n");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值