读取这一行字符串,每个字符串长度小于80个字符
对于每组数据,输出每行字符串的加密字符串。
Hello! How are you!
Ifmmp! Ipx bsf zpv!
zzz
aaa
#include<bits/stdc++.h> using namespace std; int main() { string input; while (cin >> input) { for (int i = 0; i < input.size(); i++) { if (input[i] == 'Z' || input[i] == 'z') { input[i] = input[i] - 'z' + 'a'; } if (input[i] < 'z'&&input[i] >= 'a') { input[i] ++; } if (input[i] < 'Z'&&input[i] >= 'A') { input[i] ++; } } cout << input<<" "; } return 0; }
#include<iostream> #include<string> #include<cstring> using namespace std; int main() { string s; getline(cin, s); char ch[80]; strcpy(ch, s.c_str()); for (int i = 0; i < s.length(); i++) { if ((ch[i] >= 'a'&&ch[i] <= 'z') || ch[i] >= 'A'&&ch[i] <= 'Z') ch[i]++; } for (int i = 0; i < s.length(); i++) cout << ch[i]; return 0; }
import java.util.Scanner;
/**
Created by wanyu on 2018/3/19.
*/
public class K {
public static void main(String[] args) {
Scanner cin=new Scanner(System.in);
String str=cin.nextLine();
char a[]=str.toCharArray();
for(int i=0;i<a.length;i++){
int k=a[i];
char b=1;
if((k>=65&&k<=90)||(k>=97&&k<=122)){
b=(char)(++k);
System.out.print(b);
}else {
System.out.print(a[i]);
}
}
}
}
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String s = scanner.nextLine(); char[] array = s.toCharArray(); StringBuilder builder = new StringBuilder(); for (char c : array) { if (c>='a'&&c<'z') builder.append((char)(c+1)); else if (c>='A'&&c<'Z') builder.append((char)(c+1)); else if (c=='Z') builder.append('A'); else if (c=='z') builder.append('a'); else builder.append(c); } System.out.println(builder.toString()); } }
//收集一行字符串且中间有空格时, //用getline(cin,string) #include<iostream> #include<cstdio> using namespace std; int main(){ int CaseNumber; string str; while(getline(cin,str)){ for(int i=0; i<str.size(); i++){ if(str[i]=='z'){ str[i] = 'a'; } else if(str[i]=='Z'){ str[i] = 'A'; } else if(str[i]>='a'&&str[i]<='y'){ str[i] += 1; } else if(str[i]>='A'&&str[i]<='Y'){ str[i] += 1; } } cout<<str<<endl; } return 0; }
#include<iostream> #include<string> using namespace std; int main() { string st; while(getline(cin,st)) { for(int i=0;i<st.size();i++) { if(st[i]>='a'&&st[i]<='y') st[i]++; else if(st[i]>='A'&&st[i]<='Y') st[i]++; else if(st[i]=='z') st[i]='a'; else if(st[i]=='Z') st[i]='A'; } cout<<st<<endl; } return 0; }
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { public static void main(String[] args) { Scanner scan=new Scanner(System.in); String str=scan.nextLine(); Pattern p=Pattern.compile("[a-yA-Y]|[zZ]"); Matcher m=p.matcher(str); StringBuffer sb=new StringBuffer(); while(m.find()){ if(m.group().matches("[zZ]")){ m.appendReplacement(sb,(char)(m.group().charAt(0)-25)+""); }else{ m.appendReplacement(sb,(char)(m.group().charAt(0)+1)+""); } } m.appendTail(sb); System.out.println(sb); } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input=new Scanner(System.in); while(input.hasNext()){ String str=input.nextLine(); System.out.println(Tran(str)); } } public static String Tran(String str){ char c[]=str.toCharArray(); StringBuffer buf=new StringBuffer(); for(int i=0;i<c.length;i++){ if(c[i]=='Z'){ buf.append("A"); }else if(c[i]=='z'){ buf.append("a"); }else if((c[i]>='a'&&c[i]<='y')||(c[i]>='A'&&c[i]<='Y')){ buf.append((char)(c[i]+1)); }else{ buf.append(c[i]); } } return String.valueOf(buf); } }
package com.speical.improve;
import java.util.Scanner;
/**
*
* @author special
* @date 2017年12月25日 上午11:07:52
*/
public class Pro32Improve1 {
private static boolean isUpper(char ch) { return ch >= 'A' && ch <= 'Z'; }
private static boolean isLower(char ch) { return ch >= 'a' && ch <= 'z'; }
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
while(input.hasNext()){
int n = input.nextInt();
input.nextLine();
while(n-- > 0){
String password = input.nextLine();
for(int i = 0; i < password.length(); i++){
char letter = password.charAt(i);
if(isUpper(letter)){
System.out.print((char)(letter != 'Z' ? letter + 1 : 'A'));
}else if(isLower(letter)){
System.out.print((char)(letter != 'z' ? letter + 1 : 'a'));
}else {
System.out.print(letter);
}
}
System.out.println();
}
}
}
}
#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> using namespace std; const int maxn = 80; char ch[maxn] = {0}; int main() { int n, len; while(gets(ch)) { n = atoi(ch); while(n--) { gets(ch); len = strlen(ch); for(int i = 0; i < len; ++i) { // 'a'-'y' 和 'A'-'Y' if((ch[i] >= 'a' && ch[i] <= 'y') || (ch[i] >= 'A' && ch[i] <= 'Y')) { cout << (char)(ch[i]+1); } else if(ch[i] == 'z') { cout << 'a'; } else if (ch[i] == 'Z') { cout << 'Z'; } else { cout << ch[i]; } } cout << endl; } } return 0; }
本套6道题全部pass的C++代码已挂到了我的GitHub(https://github.com/shiqitao/NowCoder-Solutions)
牛客网上的其他题目解答也在持续更新。
【题解】简单的字符串处理。
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char str[80];
gets(str);
for (int i = 0; i < strlen(str); i++) {
if ((str[i] >= 'a'&&str[i] < 'z') || (str[i] >= 'A'&&str[i] < 'Z')) {
str[i] += 1;
}
else if (str[i] == 'z' || str[i] == 'Z') {
str[i] -= 25;
}
}
cout << str;
return 0;
}
#include <stdio.h> #include <string.h> #define N 100 int main() { char s[N]; while(gets(s)) { int len=strlen(s); for(int i=0; i<len; i++) { if('a'<=s[i]&&s[i]<='y') s[i]++; else if('A'<=s[i]&&s[i]<='Y') s[i]++; else if(s[i]=='z') s[i]='a'; else if(s[i]=='Z') s[i]='A'; } puts(s); } return 0; }
//这个题有点问题 题目给的样例中第一行是一个数字,但是输出却没处理数字 我以为是表示要输入的数据规模,结果怎么搞 //都是运行时间超过限制,题目的测试案例应该是一行行的字符串 根本没数据规模的数字 #include <stdio.h> #include <stdlib.h> #include <ctype.h> int main(){ char c; while((c = getchar())!='\n'){ if(isalpha(c)){ if(c!='z'&&c!='Z'){ putchar(c+1); }else putchar(c=='z'?'a':'A'); }else putchar(c); } putchar('\n'); return 0; }
#include <iostream> #include <string> using namespace std; int main() { string line1="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; string line2="bcdefghijklmnopqrstuvwxyzaBCDEFGHIJKLMNOPQRSTUVWXYZA"; string a; while(getline(cin,a)){ for(int i=0;i<a.length();i++){ if(line1.find(a[i])==-1){ }else{ a[i]=line2[line1.find(a[i])]; } } cout<<a<<endl; } return 0; }
#include<cstring> (803)#include<iostream> using namespace std; int main() { string s; getline(cin, s); for(int i=0; i<s.size(); ++i) s[i] = (s[i]==90 || s[i]==123)?s[i]-25:((s[i]>=65 && s[i]<90) || (s[i]>=97 && s[i]<123))?s[i]+1:s[i]; cout<<s<<endl; }C++核心一行