Home >Java >javaTutorial >A small question in java
1. Given a string String s="abcdefg";
Reverse the string into a new string
2. Determine whether a string is a palindrome string For example: "abcdcba" "Shanghai tap water comes from the sea"
1. Solve the
Java code
package com.newer.cjl.api; public class zuoye1 { public static void main(String[] args) { String s="abcdefg"; String s1=""; for(int i=s.length()-1;i>=0;i--){ char c = s.charAt(i); s1=s1+c; } System.out.println(s1); } } package com.newer.cjl.api; public class zuoye1 { public static void main(String[] args) { String s="abcdefg"; String s1=""; for(int i=0;i<=s.length()-1;i++){ char c = s.charAt(i); s1=c+s1; } System.out.println(s1); } }
2. Solve the
Java code
import java.util.Scanner; public class zuoye2 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入字符串:"); String str = sc.next(); boolean isOK=true; for(int i=0;i<str.length()/2;i++){ if(str.charAt(i)!=str.charAt(str.length()-1-i)){ System.out.println("不是回文字符串!"); isOK=false; break; } } if(isOK){ System.out.println("是一个回文字符串"); } } }