简体   繁体   中英

most frequent character in 2d array with pointer

i want to find the most frequent character for 5 strings,i thought of this ,i have done it for a specific character,in my ocasion s ,but how can i make it for every character,its only part of my code

Ch is a pointer!!!

for(i = 0; i < five; i++){
    ch = strchr(cities[i], 's'); 

    if (ch != NULL){ //if this is true 
        ch1++;
        printf("exists in %s\n", cities[i]); //print
    }
    else
    {
        printf ("doesnt exist in %s\n", cities[i]); //print
    }
}
printf("The character 's' appears in %d cities\n", ch1);

You have to somehow store the number of occurences for each character somewhere.

With that given a hint would be to keep one variable with storing the max occurence of the character and the character itself. Nothing else. And for looping you need to consider all characters from a to z .

The code will be something like:-(Only look if you feel you have tried enough).

To make you aware of what this code does - it counts the character which occurs in maximum number of cities. In case there is a tie, it selects the smaller alphabet first (lexicographically).

 for( char c = 'a'; c <= 'z';c++){ int ch1 = 0, mxch1 = 0; char mch='?'; for(i = 0; i < numOfCities; i++){ ch=strchr(cities[i],c); if( ch ){ printf("Exist in %s\\n",cities[i]); ch1++; } else printf("Doesn't Exist in %s\\n",cities[i]); } if( ch1 > mch1) { mch1 = ch1; mch = c; } printf("The character %c appears in %d cities\\n",c,ch1); } printf("Character which occured in max number of cities is %c %d",mch,mch1); 

The code for most frequent character will be a bit different.

 int charMap[26]={0}; for(size_t i = 0; i < numOfCities; i++){ for(size_t j = 0; cities[i][j]; j++) charMap[cities[i][j]-'a']++; } int mx = 0; mxi = 0; for(size_t i = 0; i < sizeof(charMap)/sizeof(charMap[0]); i++){ if(charMap[i]>mx){ mx = charMap[i]; mxi = i; } printf("Character which occured max number of times is %c %d", (char)(mxi+'a'),mx); 

You can use count sort to achieve this task in O(n) time complexity and O(1) space complexity if your input strictly consists of ASCII characters only.

You keep an array int count[128] = {0}; which keeps track of how many time each character occurred. You use the index of the array as the ASCII value of the character.

for(int i = 0; i < 5; i++)
    for(int j = 0; cities[i][j] != '\0'; j++)
         count[cities[i][j]]++;  

If you want to ignore non-graphic characters or you just want to count alphanumeric characters or whatever your requirements maybe, you will have to add an if check with the correct combination of functions ( isalpha , isalnum , etc) from ctype.h

You now have to run through the count array and find the largest number. The index at which the largest number was found is the ASCII value of the most common character.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM