{ "question_id": 382917, "question_title": "改写字符串", "question_content": "键盘录入一个字符串,将字符串中的大写改成小写,小写改成大写,数字改成*。例如heLLO123,输出后为HEllo***", "difficulty": "简单", "answer_id": 418046, "answer_content": "\n\n```\n import java.util.Scanner;\n\n/**\n *\n * @author sunzc 转换字符串,大写变小写,小写变大写,数字不变,其他变为*\n */\npublic class Transfer {\n public static void main(String[] args) {\n // String str = \"ABC123abcfadfjbJBHJHJDsa\";\n String str = \"\";\n Scanner s = new Scanner(System.in);\n System.out.println(\"请输入您想输入的字符串:\");\n str = s.next();\n StringBuffer sb = new StringBuffer();\n int i;\n // char ch;\n for (i = 0; i <= str.length() - 1; i++) {// 遍历字符串\n char ch;\n\n // 通过str.charAt(i)遍历出字符串中每个字符\n if (str.charAt(i) >= 'a' && str.charAt(i) <= 'z') {// 判断字符是否在a-z之间(小写)\n ch = (char) (str.charAt(i) - 32); // 如果为小写则转换为相应大写,赋值给ch\n } else if (str.charAt(i) >= 'A' && str.charAt(i) <= 'Z') {// 判断字符是否在A-Z之间(大写)\n ch = (char) (str.charAt(i) + 32); // 如果为大写则转换为相应小写,赋值给ch\n } else if (str.charAt(i) >= '0' && str.charAt(i) <= '9') {// 判断字符是否在0-9之间(数字)\n ch = '*'; // 如果为数字,则转为*号\n } else {\n ch = str.charAt(i); // 将原字符赋值给ch\n }\n sb.append(ch); // 将字符追加到sb序列\n }\n String trStr = sb.toString(); // 将StringBuffer转换为String类型\n System.out.println(sb.toString());\n }\n\n}\n```\n\n", "tag_name": "java", "java": "import java.util.Scanner;\npublic class Transfer {\n\tpublic static void main(String[] args) {\n\tString str = \"\";\n\tScanner s = new Scanner(System.in);\n\tSystem.out.println(\"请输入您想输入的字符串:\");\n\tstr = s.next();\n\tStringBuffer sb = new StringBuffer();\n\tint i;\n\tfor (i = 0; i <= str.length() - 1; i++) {\n\t\tchar ch;\n\t\tif (str.charAt(i) >= 'a' && str.charAt(i) <= 'z') {\n\t\t\tch = (char) (str.charAt(i) - 32); \n\t\t} else if (str.charAt(i) >= 'A' && str.charAt(i) <= 'Z') {\n\t\t\tch = (char) (str.charAt(i) + 32); \n\t\t} else if (str.charAt(i) >= '0' && str.charAt(i) <= '9') {\n\t\t\tch = '*'; \n\t\t} else {\n\t\t\tch = str.charAt(i); \n\t\t}\n\t\tsb.append(ch); \n\t}\n\tString trStr = sb.toString(); \n\tSystem.out.println(sb.toString());\n }\n}", "topic_link": "https://bbs.csdn.net/topics/600469971", "status": 1, "keywords": "算法高阶,字符串匹配,算法问题选编,利用有限自动机进行字符串匹配", "license": "csdn.net", "notebook": { "java": "https://codechina.csdn.net/csdn/csdn-daily-code/-/jupyter/master/data/notebook/answer/ipynb/java/0.ipynb?type=file" }, "notebook_enable": 1 }