(2)开头是0,由于是输入正整数,所以开头的0不考虑:
3.问题分析: 思路:
(1) 求出整数位数,以便动态分配数组空间; (2) 依次分离出每一位的数存储下来; (3) 从0到9逐个统计个数;
(4) 对于个数为0的不输出,大于0的输出统计结果。
八、第八题:8、(选作题)第一将整数翻译程序中翻译千位整数的函数直接屏幕输出英文改为形成翻译字符串返回给主调函数;第二将子
串个数统计程序改为使用库函数strncmp()来实现。 1.源程序代码: (1)
#include
\, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \, \};
static char *shiwei[]= {
\, \,
\, \, \, \, \, \, \, \
};
char *m(int a) {
int b = a % 100;
char output[50] = \; if(a / 100 != 0) {
strcat(output,gewei[a/100]); strcat(output, \); if(b!=0)
strcat(output, \); }
if(b < 20)
strcat(output, gewei[b]); else {
strcat(output, shiwei[b/10]); if(b!=0) {
strcat(output, gewei[b]); } }
return output; }
void main() {
int n;
char str[200] = \; cout<<\请输入n:\; cin>>n;
int a=n/1000000,b=(n00000)/1000,c=n00; if(a!=0) {
strcat(str, m(a));
strcat(str, \); }
if(b!=0) {
strcat(str, m(b));
strcat(str, \); }
if(c!=0) {
if(c<100)
}
strcat(str, \); strcat(str, m(c)); }
cout << str << endl;
(2)
#include
int count(char *pi,char *pj) {
int num=0; char *pt=pi;
int pjn=strlen(pj);
while( *pt!='\\0' && strlen(pt) >= pjn) {
if(strncmp(pt, pj, pjn) == 0) num++; pt++; }
return num; }
void main() {
char *pa=\,*pb=\; cout << \母串:\ << pa << endl; cout << \子串:\ << pb << endl;
cout << \子串个数:\ << count(pa,pb) << endl; }
2.实验结果: (1)
(2)