网上找的程序,比较简洁,用到了strncmp函数:
// huawei_22.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
char a[11][11]={"zero","one","two","three","four","five","six","seven","eight","nine","double"};
char *s,out[20];
int i=0,j=0,f=0,d=0,k=0,count=0;
s=(char *)malloc(sizeof(char));
gets(s);
while(*s!='\0')
{
f=0;
if(*s=='Z'||*s=='O'||*s=='T'||*s=='F'||*s=='S'||*s=='E'||*s=='N'||*s=='D')
*s+=32;
for(i=0;i<11;i++)
{
if(!strncmp(s,a[i],strlen(a[i])) && i<10)
{ out[k++]=i+'0';
s=s+strlen(a[i]);
f=1;
if(d==1)
{
out[k++]=i+'0';
d=0;
}
}
else if(!strncmp(s,a[i],strlen(a[i])) && i==10)
{
d=1;
f=1;
s=s+strlen(a[i]);
count++;
}
}
if(f==0|| count>1)
{
printf("error!\n");
return 0;
}
}
out[k]='\0';
if(d==1)
printf("error!\n");
else
puts(out);
getchar();
}