码迷,mamicode.com
首页 > 编程语言 > 详细

Java字符串练习题

时间:2020-03-27 00:56:09      阅读:285      评论:0      收藏:0      [点我收藏+]

标签:目录   substring   pre   产生   生成   err   substr   fas   用户   

1、"a"+"b"+"c"+"d"+"e"+"f"+"g"+"h"改写上面的代码,打印结果

package work1;

public class Work1 {
	public static void main(String[] args) {
		StringBuffer s = new StringBuffer();
		s= s.append("a");
		s= s.append("b");
		s= s.append("c");
		s= s.append("d");
		s= s.append("e");
		s= s.append("f");
		s= s.append("g");
		s= s.append("h");
		System.out.println(s);
	}
}

2、给定一个长度,随机产生一个该长度的字符串,由大写,小写字母以及数字组成

Java中随机数的生成:
java.util.Random r = new java.util.Random();
int a = r.nextInt(100): a 0-99的随机数

第一种:
package work2;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

public class Work2 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		Random r = new Random();
		
		System.out.println("请输入生成字符串的长度");
		int length = sc.nextInt();

		String s = "";
		for(int i = 0;i<length;i++){
			int random = r.nextInt(2);
			if(random==1){
				int num = r.nextInt(10);//随机产生数字
				s=s+s.valueOf(num);
			}
			else {
				int randomChar = r.nextInt(2);
				if(randomChar==1){//随机产生大写字母
					char str =(char)(r.nextInt(26)+‘A‘);
					s=s+s.valueOf(str);
				}
				else{//随机产生小写字母
					char str =(char)(r.nextInt(26)+‘a‘);
					s=s+s.valueOf(str);
				}
			}
		}
		System.out.println(s);
	}
}

第二种:
package work2;


import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

public class Work2 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		Random r = new Random();
		
		System.out.println("请输入生成字符串的长度");
		int length = sc.nextInt();
        
		//创建一个集合存储大写小写字母以及数字
		List<Character> list = new ArrayList<Character>();
		for(char c =‘A‘;c<=‘Z‘;c++)
			list.add(c);
		for(char c =‘a‘;c<=‘z‘;c++)
			list.add(c);
		for(char c =‘0‘;c<=‘9‘;c++)
			list.add(c);
		
		String s = "";//保存结果
		for(int i = 0;i<length;i++){
			//随机获取字符下标
			int a = r.nextInt(list.size());
			s = s+list.get(a);
		}
        //打印
		System.out.println(s);
		
		
	}
}

3、给定一个字符串,判断该字符串中是否包含某个子串.如果包含,求出子串的所有出现位置.

如:"abcd23abc34bcd"中,"bc"子串的出现位置为: 1,7,11.字符串和子串均由用户输入

package work3;

import java.util.Scanner;

public class Work3 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入字符串");
		String s1 = sc.next();
		System.out.println("请输入子串");
		String s2 = sc.next();

		int index =0;
		while((index=s1.indexOf(s2, index))!=-1){
			System.out.print(index+" ");
			index = index +s2.length();
		}
	}
}

4、给定一个由数字组成的字符串,如:"1239586838923173478943890234092",统计出每个数字出现的次数

第一种:
package work4;

import java.util.Scanner;

public class Work4 {
	public static void main(String[] args) {
		String s = "1591231231231326560686464506565791310665979803";
		int[] count = new int[10];
		for(int i=0;i<10;i++){
			String s1 = s;
			int index=0;
			while(true){
				index = s1.indexOf(String.valueOf(i));
				if(index!= -1){
					s1=s1.substring(index+1);//截取字符串
					count[i]++;
				}
				else break;
			}
		}
		for (int i=0;i<count.length;i++) {
			System.out.println(i+" 出现的次数 "+count[i]);
		}
	}
}
第二种:
package work4;

import java.util.Scanner;

public class Work4 {
	public static void main(String[] args) {
		String s = "1591231231231326560686464506565791310665979803";
		
		int[] arr = new int[10];//创建数组记录次数
		for(int i =0;i<s.length();i++){
			arr[s.charAt(i)-‘0‘]++;
		}
		//遍历
		for (int i=0;i<arr.length;i++) {
			System.out.println(i+"的次数是"+arr[i]);
		}
	}
}

5、给定一个字符串,请输出该字符串由哪些字符组成,每个字符出现几次?

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class Work5 {
	public static void main(String[] args) {
		String s ="s13fs54fas87f8sf40";
		
		Map<Character, Integer> map = new HashMap<Character, Integer>();//一个key值对应一个value
		
		for(int i=0;i<s.length();i++){
			Character c =s.charAt(i);
			if(map.get(c)==null)//第一次加入到map集合
				map.put(c, 1);
			else
				map.put(c, map.get(c)+1);
			
		}
		Set<Character> keySet = map.keySet();
		for (Character character : keySet) {
			System.out.println(character+"----"+map.get(character));
		}
	}
}

6、字符编码 "aaaaaaabbbbbcerrrrggggggggsssssspoqqqqq"==> "a7b5cer4g8s6poq5"


public class Work6 {
	public static void main(String[] args) {
		String str = "aaaaaaabbbbbcerrrrggggggggsssssspoqqqqq";
		StringBuilder sb = new StringBuilder("");
		
		char current = str.charAt(0);
		int count = 1;
		
		for(int i = 1 ; i < str.length() ; i++){
			char c = str.charAt(i);
			if (c == current){
				count++;
			}
			else{
				if (count == 1) sb.append(current);
				else sb.append(current).append(count);
			
				current = c;
				count = 1;
				
			}
		}
		if (count == 1) sb.append(current);
		else sb.append(current).append(count);
		
		System.out.println(sb.toString());
	}
}


Java字符串练习题

标签:目录   substring   pre   产生   生成   err   substr   fas   用户   

原文地址:https://www.cnblogs.com/shellxx/p/12578522.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!