C语言 汉字转拼音

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define N 100

char * retpinyin()
{
    static char name[10];
    srand((unsigned)time(NULL)); 
  int iRange1 = 0xf7 - 0xb0; 
    int iRange2 = 0xfe - 0xa1; 
   
     char iCode1 = rand()%iRange1 + 0xb0; 
     char iCode2 = rand()%iRange2 + 0xa1; 
     char chh[3] = {iCode1,iCode2,0}; 
     strcpy(name,chh);
    return name;
}
int StrReplace(char strRes[],char from[], char to[]) {
    int i,flag = 0;
    char *p,*q,*ts;
    for(i = 0; strRes[i]; ++i) {
        if(strRes[i] == from[0]) {
            p = strRes + i;
            q = from;
            while(*q && (*p++ == *q++));
            if(*q == '\0') {
                ts = (char *)malloc(strlen(strRes) + 1);
                strcpy(ts,p);
                strRes[i] = '\0';
                strcat(strRes,to);
                strcat(strRes,ts);
                free(ts);
                flag = 1;
            }
        }
    }
    return flag;
}
int main() {
int zheng=0;
int a=0;
FILE *fp;
char str[N + 1],ch[2],ch1[20],ch2[20],chx[10],str1[N+1];
char * chh;
//判断文件是否打开失败
if ( (fp = fopen("duizhao1.txt", "rt")) == NULL ) {
puts("Fail to open file!");
exit(0);
}
while(1)
{
chh=retpinyin();
printf("%s\n",chh);
scanf("%s",&chx);

while( fgets(str, N, fp) != NULL ) {
//printf("%s", str);
//printf("%d\n",strstr(str,ch));

    
if(strstr(str,chh)!=0)
{
    strcpy(str1,str);
    StrReplace(str1,chh,"");
    StrReplace(str1," ","");
    StrReplace(str1,"\n","");
    
    //if(strstr(str,chx)!=0)
    if(strcmp(str1,chx)==0)
      {
      printf("对了!\n");
      zheng=0;
      //break; 
      }
    else 
     {
      printf("错了!\n");
      printf("正确答案:%s\n",str1);
      zheng=1;
      //break;
     }
     break;
}

}
if(zheng==1)
printf("对照表中没有%s\n",chh);
zheng=0;

}
//操作结束后关闭文件
fclose(fp);
return 0;
}

 

上一篇:Java switch使用字符串比较


下一篇:string