编写一个程序,输出一个字符串中的大写英文字母数,小写英文字母数以及非英文字母数。
编写一个方法,输出在一个字符串中,指定字符串出现的次数。
该列子定义了一个StringIndex 类,实现了两个方法。
import java.util.Scanner;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Administrator
*/
public class StringIndex {
private int i=0;
private int j=0;
private int k=0;
public void getNumber(String s){
// for(int n=0;n<s.length();n++){
// char c=s.charAt(n);
// if(c>='a'&&c<='z'){
// i++;
// }else if(c>='A'&&c<='Z'){
// j++;
// }else{
// k++;
// }
// }
char[] cc=s.toCharArray();
for(int n=0;n<cc.length;n++){
if(cc[n]>='a'&&cc[n]<='z'){
i++;
}else if(cc[n]>='A'&&cc[n]<='Z'){
j++;
}else{
k++;
}
}
System.out.println("该字符串共有小写英文字母数:"+i);
System.out.println("该字符串共有大写英文字母数:"+j);
System.out.println("该字符串共有非英文字母数:"+k);
}
public void getStringNumber(String s,String indexS){
int fromIndex=0;
int n=0;
int k=0;
while(fromIndex<s.length()&& (k =s.indexOf(indexS, fromIndex))!=-1){
fromIndex=k+1;
n++;
}
System.out.println("字符串"+indexS+"在字符串"+s+"中出现了"+n+"次");
}
}
定义测试类:
import java.util.Scanner;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Administrator
*/
public class StringTest {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("请输入一个字符串:");
String s=sc.next();
System.out.println(s);
new StringIndex().getNumber(s);
System.out.println("请输入要查询的字符串:");
String indexS=sc.next();
new StringIndex().getStringNumber(s,indexS);
}
}
该列子对中文字符的处理有写小毛病。 :cry:
编写一个方法,输出在一个字符串中,指定字符串出现的次数。
该列子定义了一个StringIndex 类,实现了两个方法。
import java.util.Scanner;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Administrator
*/
public class StringIndex {
private int i=0;
private int j=0;
private int k=0;
public void getNumber(String s){
// for(int n=0;n<s.length();n++){
// char c=s.charAt(n);
// if(c>='a'&&c<='z'){
// i++;
// }else if(c>='A'&&c<='Z'){
// j++;
// }else{
// k++;
// }
// }
char[] cc=s.toCharArray();
for(int n=0;n<cc.length;n++){
if(cc[n]>='a'&&cc[n]<='z'){
i++;
}else if(cc[n]>='A'&&cc[n]<='Z'){
j++;
}else{
k++;
}
}
System.out.println("该字符串共有小写英文字母数:"+i);
System.out.println("该字符串共有大写英文字母数:"+j);
System.out.println("该字符串共有非英文字母数:"+k);
}
public void getStringNumber(String s,String indexS){
int fromIndex=0;
int n=0;
int k=0;
while(fromIndex<s.length()&& (k =s.indexOf(indexS, fromIndex))!=-1){
fromIndex=k+1;
n++;
}
System.out.println("字符串"+indexS+"在字符串"+s+"中出现了"+n+"次");
}
}
定义测试类:
import java.util.Scanner;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Administrator
*/
public class StringTest {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("请输入一个字符串:");
String s=sc.next();
System.out.println(s);
new StringIndex().getNumber(s);
System.out.println("请输入要查询的字符串:");
String indexS=sc.next();
new StringIndex().getStringNumber(s,indexS);
}
}
该列子对中文字符的处理有写小毛病。 :cry: