Home > Java > javaTutorial > body text

A small question in java

巴扎黑
Release: 2016-12-10 09:20:40
Original
981 people have browsed it

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);  
          
          
    }  
}
Copy after login


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("是一个回文字符串");  
        }  
          
  
    }  
  
}
Copy after login


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!