1.將阿拉伯數字轉成中文數字,例如,輸入字符串”我愛12你好34”,輸出”我愛一二你好三四”(知識點:漢字及數字的替換)
#include <iostream>
#include <string>
using namespace std;
char* changeNumberToHanZi(string s,int length)
{
int n = 0;
for(int i = 0;i <= length;i++)
{
if(s[i] >= '0' && s[i] <= '9')
{
n++;
}
}
int b_length = length + n + 1;
char* b = new char[b_length];
b[b_length - 1] = '\0';
char* chinese[] = {"零","一","二","三","四","五","六","七","八","九"};
for(int i = length - 1,j = b_length - 2;i >= 0;i--,j--)
{
if(s[i] >= '0' && s[i] <= '9')
{
int t = s[i] - '0';
char* hanzi = chinese[t];
b[j] = hanzi[1];
j--;
b[j] = hanzi[0];
}
else
{
b[j] = s[i];
}
}
return b;
}
int main()
{
string s = "我20你";
int length = s.size();
s = changeNumberToHanZi(s,length);
cout<<s<<endl;
return 0;
}
2.字符串分解:一段文本以逗號分開,寫一個函數將各段文本打印出來
例如,
輸入:hello, world , good, morning
輸出:
hello
world
good
morning
#include <iostream>
using namespace std;
int main()
{
string s;
cin>>s;
int i = 0,j = 0;
while(s[i] != '\0')
{
while(s[j] != ',' && s[j] != '\0')
{
j++;
}
for(int t = i;t < j;t++)
{
cout<<s[t];
}
cout<<endl;
if(s[j] == ',')
{
j++;
i = j;
}
if(s[j] == '\0')
{
break;
}
}
return 0;
}