兄弟单词

来源:互联网 发布:angular.js下载 编辑:程序博客网 时间:2024/05/03 16:06
#include <stdio.h>struct word{unsigned char ucWord[51];struct word *pNext;};/*获取字符串长度*/unsigned int MyStrLen(const char* p){if(NULL == p)return 0;unsigned int len = 0;while('\0'!=*p++)len++;return len;}/* 字符串比较函数 * =0,单词相等;>0,str1在str2之后;<0,str1在str2之前;-26,参数错误 */int MyStrCmp(const char *str1,const char *str2){if(NULL == str1 || NULL == str2 || *str1 == '\0' || *str2 == '\0')return -26;while((*str1 == *str2 )&& *str1){str1++;str2++;};    if( *str1 > *str2  )return 1;else if(*str1 < *str2 )return -1;elsereturn 0;}/*兄弟单词判断*/bool IsBrotherWord(const char* word1,const char* word2){if(NULL==word1||NULL==word2)return false;if(MyStrLen(word1) != MyStrLen(word2))return false;if(0 == MyStrCmp(word1,word2))return false;int AlphaCnt1[26]={0};int AlphaCnt2[26]={0};while('\0'!=*word1&&'\0'!=*word2){AlphaCnt1[(*word1++)-97]++;AlphaCnt2[(*word2++)-97]++;}for(int i=0;i<26;i++){if(AlphaCnt1[i] != AlphaCnt2[i])return false;}return true;}void main(){//char str[]="ILOVEYOU";//int len = MyStrLen(str);//printf("len=%d\n",len);    //printf("MyStrCmp rt=%d\n",MyStrCmp("ll",""));bool bFlag = IsBrotherWord("love","vole");printf("bFlag = %d\n",bFlag);getchar();}


                                             
0 0
原创粉丝点击