Decipher messages

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_45499204/article/details/102757387

Title Description

Bob received a strange message, some of which are all symbols and numbers, but letters deciphering method given above, specifically as follows:
(1) becomes a 'A', 2 becomes 'B', ... , 26 becomes 'the Z';
(2) the '#' to a space;
(3) ignore the '-', the original letter '-' for dividing the digital only.
Now you please help Xiao Ming programming decipher this message.

Entry

A first act input integer C, represents the number of test data sets.
Next C lines of the input character string to be a deciphered string contains only numbers, '-' and '#', the length does not exceed 100.
Export

Sample input

4
9#23-9-12-12#19-20-5-1-12#1-20#12-5-1-19-20#15-14-5#10-5-23-5-12
1-14-4#12-5-1-22-5#20-8-5#13-21-19-5-21-13#9-14#20#13-9-14-21-20-5-19
1-6-20-5-18#20-8-5#15-16-5-14-9-14-7#15-6#20-8-5#5-24-8-9-2-9-20-9-15-14
7-15-15-4#12-21-3-11

Sample Output

I WILL STEAL AT LEAST ONE JEWEL
AND LEAVE THE MUSEUM IN T MINUTES
AFTER THE OPENING OF THE EXHIBITION
GOOD LUCK

Code 1

#include<stdio.h>
#include<iostream>
using namespace std;

int main() {
	int N;
	scanf("%d",&N);
	getchar();
	char str[101];
	char str1[101];
	while(N--) {
		gets(str);
		int sum=0;
		int k=0;//这俩不要放在while上面
		for(int i=0; str[i]!='\0'; i++) {
			if(str[i]>='0'&&str[i]<='9') { //第一种情况
				sum=sum*10+str[i]-'0';//char 换成 int 
				if(str[i+1]>='0'&&str[i+1]<='9') {
					sum=sum*10+str[i+1]-'0';
					str1[k++]=sum-1+'A';//sum+64也行 
					i++;//别忘了 两个字符换成一个了
					sum=0;
				} else {
					str1[k++]=sum-1+'A';
					sum=0;
				}
			} else if(str[i]=='#') { //第二种情况
				str1[k++]=' ';
			} else if(str[i]=='-') { //第三种情况
				continue;
			}
		}
		for(int i=0; i<k; i++) {
			printf("%c",str1[i]);
		}
		printf("\n");
	}
	return 0;
}

Code 2

#include<stdio.h>
#include<ctype.h>//
int main(){
	int n;
	scanf("%d",&n);
	getchar();
	char str[101];
	while(n--){
		gets(str);
		for(int i=0;str[i]!='\0';i++){
			if(isalnum(str[i])&&!isalnum(str[i+1])){//
				printf("%c",str[i]-'0'+64);
			}
			else if(isalnum(str[i])&&isalnum(str[i+1])){
				printf("%c",(str[i]-'0')*10+str[i+1]-'0'+64);
				i++;//别忘了 
			}
			else if(str[i]=='#'){
				printf(" ");
			}
		}
		printf("\n");
	}
	return 0;
} 

Guess you like

Origin blog.csdn.net/qq_45499204/article/details/102757387