Hello All,
I have developed code for finding character that appears max times in a string. Wanted to know, any better version of this. If two characters appear same number of times then whichever appaers first is given priority.
RT
Code:#include<stdio.h> #define MAX_CHAR 26 unsigned char cFreqTable[MAX_CHAR]; main() { unsigned char string[200],cMaxChar=0; unsigned iCurrFreq,iMaxFreq; unsigned char *buf = string; iCurrFreq = iMaxFreq = 0; printf("Enter the string\n"); scanf("%s",buf); while (*buf) { // convert to lower case if ( ( (*buf) >= 'A' ) && ( (*buf) <= 'Z' ) ) (*buf) = (*buf) + ('a' - 'A') ; // Increment frequency table cFreqTable['a'-(*buf)] = cFreqTable['a'-(*buf)] + 1; iCurrFreq = cFreqTable['a'-(*buf)]; if ( iCurrFreq > iMaxFreq ) { iMaxFreq = iCurrFreq; cMaxChar = *buf; } buf++; } printf("Character %c appears for max %d times \n",cMaxChar,iMaxFreq); }



LinkBack URL
About LinkBacks


