华为机试题: 将数组中的字符串按指定长度重新分割(java)

描述: 

请实现接口 convertStringArray。

输入一个字符串数组, 请按指定长度iInputLenth拆分数组中的每个字符串,输出到新的字符串数组中。长度不是iInputLenth整数倍的字符串请在后面补数字0。空字符串不处理,遇到空字符串则表示该数组结束。

/*

    功能:请编写一个函数,输入为一个字符串数组,

    请按指定长度iInputLenth拆分数组中的每个字符串,输出到新的字符串数组中。长度不是iInputLenth整数倍的字符串请在后面补数字0

    空字符串不处理,遇到空字符串则表示数组结束,函数返回。

    输入:

        String inputStrArray     字符串数组指针 字符串个数最大为50,字符串长度最大255

        int iInputLenth   指定的分割长度 iInputLenth>=1 && <=32 <><=32 <><=32 <>

         

    返回:字符串数组指针     

 

    示例 按长度8拆分

    输入: abc 

           12345789 

    返回: abc00000

           12345678

           90000000

 

    */

 

    public static String[] convertStringArray(String[] inputStrArray, int iInputLenth)

    {

        return null;

    }


package huawei;

import java.util.Vector;

public final class Demo {
	/*
	功能:请编写一个函数,输入为一个字符串数组,
	请按指定长度iInputLenth拆分数组中的每个字符串,输出到新的字符串数组中。长度不是iInputLenth整数倍的字符串请在后面补数字0。
	空字符串不处理,遇到空字符串则表示数组结束,函数返回。
	输入:
	    String inputStrArray     字符串数组指针 字符串个数最大为50,字符串长度最大255
	    int iInputLenth   指定的分割长度 iInputLenth>=1 && <=32 
	     
	返回:字符串数组指针     

	示例 按长度8拆分
	输入: abc 
	       12345789 
	返回: abc00000
	       12345678
	       90000000

	*/

	public static String[] convertStringArray(String[] inputStrArray, int iInputLenth)
	{
		/*入参判断*/
		if (iInputLenth < 1 || iInputLenth > 32)
		{
			return null;
		}
		
		Vector<String> vec = new Vector<String>();//保存生成的字符串
		for(int i = 0; i < inputStrArray.length; i++)
		{
			/*把这个字符串取出来*/
			String str = inputStrArray[i];
			char[] src = str.toCharArray();
			
			/*这个字符串不为空*/
			if(str.length() > 0)
			{
				int curIndex = 0;
				for(; (curIndex + iInputLenth) < str.length(); curIndex += iInputLenth)
				{
					/*新建一个StringBuilder对象*/
					StringBuilder temp = new StringBuilder();
					for(int j = 0; j < iInputLenth; j ++)
					{
						temp.append(src[curIndex + j]);
					}
					vec.add(temp.toString());
				}
				
				int count = 0;
				StringBuilder temp1 = new StringBuilder();
				
				for(; curIndex < str.length(); curIndex++)
				{
					temp1.append(src[curIndex]);
					count++;
				}
				
				/*补0*/
				for(; count < iInputLenth; count++)
				{
					temp1.append('0');
				}
				vec.add(temp1.toString());
			}
		}
		
		if(vec.size() > 0)
		{
			String[] ret = new String[vec.size()];
			ret	= vec.toArray(ret);
			return ret;
		}
		
	    return null;
	}





}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dmfrm

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值