字符串分隔/华为机试

题目描述

•连续输入字符串,请按长度为8拆分每个字符串后输出到新的字符串数组; 
•长度不是8整数倍的字符串请在后面补数字0,空字符串不处理。 

输入描述:

连续输入字符串(输入2次,每个字符串长度小于100)

输出描述:

输出到长度为8的新字符串数组

示例1

输入

abc
123456789

输出

abc00000
12345678
90000000

代码1:

#include<iostream>
#include<string>
#include<string.h>
using namespace std;
int main()
{
	string str1;
	string str2;
	getline(cin, str1);
	getline(cin, str2);
	int nLength1 = str1.length();
	int nLength2 = str2.length();
	int nSize1 = 0;
	int nSize2 = 0;
	if (nLength1 != 0)
	{
		nSize1 = nLength1 / 8 + (((nLength1 % 8) == 0) ? 0 : 1);
	}
	if (nLength2 != 0)
	{
		nSize2 = nLength2 /8 + (((nLength2 % 8) == 0) ? 0 : 1);
	}
	int nSize = (nSize1 + nSize2) * 8;
	char* newStr;
	newStr = new char[nSize];
	int sTep = 0;
	for (int i = 0; i < nSize1; i++)
	{
		strncpy(newStr + sTep, str1.c_str() + sTep, 8);
		sTep += 8;
	}
	int sTep2 = 0;
	for (int i = 0; i < nSize2; i++)
	{
		strncpy(newStr + sTep, str2.c_str() + sTep2, 8);
		sTep += 8;
		sTep2 += 8;
	}
	sTep = 0;
	for (int i = 0; i < nSize; i++)
	{
		if (sTep != 8)
		{
			if (newStr[i] != '\0')
			{
				cout << newStr[i];
			}
			else
			{
				cout << "0";
			}
			sTep++;
		}
		else
		{
			cout << endl;
			if (newStr[i] != '\0')
			{
				cout << newStr[i];
			}
			else
			{
				cout << "0";
			}
			sTep = 1;
		}
	}
	cout << endl;
	delete[] newStr;
	return 0;
}

代码2:

#include<iostream>
#include<string>
#include<string.h>
using namespace std;
int main()
{
	string str1;
	string str2;
	getline(cin, str1);
	getline(cin, str2);
	int nLength1 = str1.length();
	int nLength2 = str2.length();
	int nSize1 = 0;
	int nSize2 = 0;
	if (nLength1 != 0)
	{
		nSize1 = nLength1 / 8 + (((nLength1 % 8) == 0) ? 0 : 1);
	}
	if (nLength2 != 0)
	{
		nSize2 = nLength2 / 8 + (((nLength2 % 8) == 0) ? 0 : 1);
	}
	int nSize = (nSize1 + nSize2) * 8;
	char* newStr = new char[nSize];
	memcpy(newStr, str1.c_str(), nLength1);
	memset(newStr + nLength1, 0, nSize1 * 8 - nLength1);
	memcpy(newStr + nSize1 * 8, str2.c_str(), nLength2);
	memset(newStr + nSize1 * 8 + nLength2, 0, nSize2 * 8 - nLength2);
	int sTep = 0;
	for (int i = 0; i < nSize; i++)
	{
		if (sTep != 8)
		{
			if (newStr[i] != '\0')
			{
				cout << newStr[i];
			}
			else
			{
				cout << "0";
			}
			sTep++;
		}
		else
		{
			cout << endl;
			if (newStr[i] != '\0')
			{
				cout << newStr[i];
			}
			else
			{
				cout << "0";
			}
			sTep = 1;
		}
	}
	cout << endl;
	delete[] newStr;
	return 0;
	
}

代码3:转载https://blog.csdn.net/codeforcer/article/details/56480337

#include <iostream>
#include <string.h>
using namespace std;
char str[2000];
int main()
{
    while (cin>>str)
    {
        int k=8;
        for (int i=0;str[i];i++)
        {
            cout<<str[i];
            k--;
            if (k==0)
            {
                k=8;
                cout<<endl;
            }
        }
        if (k!=8)
        {
            while (k--)
            {
                cout<<0;
            }
            cout<<endl;
        }
    }
    return 0;
}

代码4:https://blog.csdn.net/hechao3225/article/details/59161110

#include <iostream>
#include <vector>
#include<string>
using namespace std;
vector<string> strSplitByLen(string str, int len) 
{
	vector<string> res;
	if (str.size()<len)
	{
		//直接补0
		while (str.size() != len)str += '0';
		res.push_back(str);
		return res;
	}
	for (int i = 0; i<str.size(); i += len) 
	{
		string subStr;
		if (str.size() - i >= 8) 
		{
			subStr = str.substr(i, len);
		}
		else 
		{
			subStr = str.substr(i, str.size() - i);//不够8位的子串,取剩下所有字符
			while (subStr.size() != len)subStr += '0';//补0
		}
		res.push_back(subStr);
	}
	return res;
}
int main() 
{
	/*int n;
	while (cin >> n) 
	{*/
		string str;
		vector<string> vec_strs;
		cin >> str;
		vec_strs.push_back(str);
		cin >> str;
		vec_strs.push_back(str);
		/*for (int i = 0; i<n; i++) 
		{
			cin >> str;
			vec_strs.push_back(str);
		}*/
		for (auto str : vec_strs) 
		{
			vector<string> vec_out = strSplitByLen(str, 8);
			for (auto e : vec_out) 
			{
				cout << e << endl;
			}
		}
	/*}*/
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值