PAT 1084. Broken Keyboard
On a broken keyboard, some of the keys are worn out. So when you type some sentences, the characters corresponding to those keys will not appear on screen.
Now given a string that you are supposed to type, and the string that you actually type out, please list those keys which are for sure worn out.
Input Specification:
Each input file contains one test case. For each case, the 1st line contains the original string, and the 2nd line contains the typed-out string. Each string contains no more than 80 characters which are either English letters [A-Z] (case insensitive), digital numbers [0-9], or "_" (representing the space). It is guaranteed that both strings are non-empty.
Output Specification:
For each test case, print in one line the keys that are worn out, in the order of being detected. The English letters must be capitalized. Each worn out key must be printed once only. It is guaranteed that there is at least one worn out key.
Sample Input:
7_This_is_a_test
_hs_s_a_es
Sample Output:
7TI
分析
用了C语言中的标准函数库中的strchr()函数可以实现查找字符串中的某个字符。
头文件: #include <string.h>
函数原型:char *strchr(const char *s, int c);
函数说明:从左向右,在字符串s中查找字符c首次出现的位置,如果找到返回c在s中的位置(指针),否则返回NULL
代码如下.
#include<iostream>
#include<algorithm>
#include<cctype>
#include<string.h>
using namespace std;
int main(){
char input1[81],input2[81],broken[81]={0};
scanf("%s%s",&input1,&input2);
int t1=0,t2=0,t=0;
while(input2[t2]!='\0'){
if(input1[t1]!=input2[t2]){
if(isalpha(input1[t1]))
input1[t1]=toupper(input1[t1]);
if(!strchr(broken,input1[t1]))
broken[t++]=input1[t1];
t1++;
}
else{
t1++; t2++;
}
}
while(input1[t1]!='\0'){
if(isalpha(input1[t1]))
input1[t1]=toupper(input1[t1]);
if(!strchr(broken,input1[t1]))
broken[t++]=input1[t1];
t1++;
}
printf("%s",broken);
return 0;
}