不知道为什么用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;
}