一串字符串算出其中大写字母,小写字母和数字的个数,并将这个结果按倒序输出
我想请问我将Int型用valueof转换字符类型,又用tochararray转化为字符数组,从而利用数组来实现倒序输出,即例如12,输出为21,但是结果不对,我试了一下,假如用过tocharrarry,我用length去测长度结果显示为1,是不是这样,假如数字个数为14个,把他给字符数组后,不是ch【0】=4,ch【1】=1,而是ch【0】=14,那么应该怎么调整呢
import java.util.*;
public class DING2 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int a = 0;
int b = 0;
int num = 0;
char temp,temp1;
String a1,b1,num1;
String s;
Scanner r = new Scanner(System.in);
System.out.println("please input something");
s = r.next();
System.out.println(s+" ");
char[] ch = s.toCharArray();
for(int i=0;i<ch.length;i++){
if(ch[i]>'a'&&ch[i]<'z')
a++;
if(ch[i]>'A'&&ch[i]<'Z')
b++;
if(ch[i]<'9'&&ch[i]>'0')
num++;
}
System.out.println(+a+" "+b+" "+num);
a1 = String.valueOf(a);
b1 = String.valueOf(b);
num1 = String.valueOf(num);
char []ch1 = a1.toCharArray();
int j = ch1.length;
if(j%2==0&&j>10)
for(int i=0;i<ch1.length/2;i++){
temp = ch1[i];
ch1[i] = ch1[j-i-1];
ch1[j-i-1] = temp;
}
else if(j>10){
for(int i=0;i<(ch1.length-1)/2;i++){
temp1 = ch1[i];
ch1[i] = ch1[j-i-1];
ch1[j-i-1] = temp1;
}
}
System.out.print("the num is");
for(int i=0;i<ch1.length;i++)
System.out.print(+ch1[i]);
System.out.println();
char []ch2 = b1.toCharArray();
temp = 0;temp1 = 0;
int k = ch2.length;
if(k%2==0&&k>10)
for(int i=0;i<ch2.length/2;i++){
temp = ch2[i];
ch2[i] = ch2[k-i-1];
ch2[k-i-1] = temp;
}
else if(k>10){
for(int i=0;i<(ch2.length-1)/2;i++){
temp1 = ch2[i];
ch2[i] = ch2[k-i-1];
ch2[k-i-1] = temp1;
}
}
System.out.print("the num is");
for(int i=0;i<ch2.length;i++)
System.out.print(+ch2[i]);
System.out.println();
temp = 0;temp1 = 0;
char []ch3 = num1.toCharArray();
int l = ch3.length;
if(l%2==0&&l>10)
for(int i=0;i<ch3.length/2;i++){
temp = ch3[i];
ch3[i] = ch3[l-i-1];
ch3[l-i-1] = temp;
}
else if(l>10){
for(int i=0;i<(ch3.length-1)/2;i++){
temp1 = ch3[i];
ch3[i] = ch3[l-i-1];
ch3[l-i-1] = temp1;
}
}
System.out.print("the num is");
for(int i=0;i<ch3.length;i++)
System.out.print(+ch3[i]);
System.out.println();
}
}
import java.util.*;
public class DING2 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int a = 0;
int b = 0;
int num = 0;
char temp,temp1;
String a1,b1,num1;
String s;
Scanner r = new Scanner(System.in);
System.out.println("please input something");
s = r.next();
System.out.println(s+" ");
char[] ch = s.toCharArray();
for(int i=0;i<ch.length;i++){
if(ch[i]>'a'&&ch[i]<'z')
a++;
if(ch[i]>'A'&&ch[i]<'Z')
b++;
if(ch[i]<'9'&&ch[i]>'0')
num++;
}
System.out.println(+a+" "+b+" "+num);
a1 = String.valueOf(a);
b1 = String.valueOf(b);
num1 = String.valueOf(num);
char []ch1 = a1.toCharArray();
int j = ch1.length;
if(j%2==0&&j>10)
for(int i=0;i<ch1.length/2;i++){
temp = ch1[i];
ch1[i] = ch1[j-i-1];
ch1[j-i-1] = temp;
}
else if(j>10){
for(int i=0;i<(ch1.length-1)/2;i++){
temp1 = ch1[i];
ch1[i] = ch1[j-i-1];
ch1[j-i-1] = temp1;
}
}
System.out.print("the num is");
for(int i=0;i<ch1.length;i++)
System.out.print(+ch1[i]);
System.out.println();
char []ch2 = b1.toCharArray();
temp = 0;temp1 = 0;
int k = ch2.length;
if(k%2==0&&k>10)
for(int i=0;i<ch2.length/2;i++){
temp = ch2[i];
ch2[i] = ch2[k-i-1];
ch2[k-i-1] = temp;
}
else if(k>10){
for(int i=0;i<(ch2.length-1)/2;i++){
temp1 = ch2[i];
ch2[i] = ch2[k-i-1];
ch2[k-i-1] = temp1;
}
}
System.out.print("the num is");
for(int i=0;i<ch2.length;i++)
System.out.print(+ch2[i]);
System.out.println();
temp = 0;temp1 = 0;
char []ch3 = num1.toCharArray();
int l = ch3.length;
if(l%2==0&&l>10)
for(int i=0;i<ch3.length/2;i++){
temp = ch3[i];
ch3[i] = ch3[l-i-1];
ch3[l-i-1] = temp;
}
else if(l>10){
for(int i=0;i<(ch3.length-1)/2;i++){
temp1 = ch3[i];
ch3[i] = ch3[l-i-1];
ch3[l-i-1] = temp1;
}
}
System.out.print("the num is");
for(int i=0;i<ch3.length;i++)
System.out.print(+ch3[i]);
System.out.println();
}
}
...全文
相关推荐
编写一个程序,实现字符串大小写的转换并倒序输出
Java下使用for循环实现转从最后一个字符开始遍历,2) 遍历的当前字符如果是大写字母,就使用toLowerCase()方法将其转换为小写字母, 如果是小写字母则使用toUpperCase() 方法将其转换为大写字母
数字倒序输出
数字倒序输出的c语言实现的两种解法: 一种是字符处理, 一种是数字方法
python解决字符串倒序输出的问题
如下所示: #python解决字符串倒序输出 def string_reverse(m): num=len(m) a=[] for i in range(num): a.append(m[num-1-i])#从最后一位的元素开始往新list内添加元素 return a 以上这篇python解决字符串倒序输出的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持软件开发网。 您可能感兴趣的文章:Python实现字符串逆序输出功能示例python让列表倒序输出的实例Python字符串处理实现单词反转Python给
Python3倒序输出字符串的N种方法(含代码和介绍)
Python3倒序输出字符串的N种方法,介绍了包括直接reverse方法,for循环正序和逆序法等三种方法来进行字符串倒序
C#数组倒序输出
简单的C#倒序输出C#使用教程电子工业出版社出版课后习题


Eclipse
