Hi all :)

Recently i got a an assignment to be done which need to deal with c++ pointer..

This is the question :

Write a function that accepts a pointer to a C-string as its argument. The function should return the character that appears most frequently in the string. Demonstrate the function in a complete program.

So i've tried written my codes this way :

#include <iostream>
using namespace std;

char large(char *p,int a)
{
int large=0,x=1,b;
char r;

for (int i=0;i<a;i++)
{
for(int j=0;j<a;j++)
{
if(*p+j==*p+i && i!=j)
{
x++;
}
if (x>large)
{
large=x;
b=i;
}
x=0;
}


r=*(p+b);
return r;
}
}


int main()
{

char test[100];
cout<<" Enter a word :";
cin.get(test,100);
cout<<endl;

char *p;
p=test;
int a=strlen(test);

char ans= large(p,a);
cout<<ans<<endl;

system ("pause");

return 0;
}

I dont know where i got it wrong, but the output is not correct..
Help me please..this need to be passed by this sunday..

Thank You :)

Dani AI

Generated

A short expert note about common pitfalls and a robust alternative: the thread shows typical C-string mistakes (pointer vs pointer-arithmetic, counter initialization, and an accidental early return). provided the original attempt and pointed out useful corrections. A clearer, safer solution uses a single-pass frequency table (O(n) time) instead of nested counting (O(n^2)). The implementation below accepts a pointer to a C-string, handles empty input, and avoids signed-char indexing bugs.

#include <array>
#include <cstddef>

// returns '\0' when input is null or empty
char most_frequent_char(const char* s) {
    if (!s || !*s) return '\0';

    std::array<int, 256> freq = {}; // byte-oriented frequency table
    const unsigned char* p = reinterpret_cast<const unsigned char*>(s);
    while (*p) ++freq[*p++];

    int best = -1;
    unsigned char best_ch = 0;
    for (std::size_t i = 0; i < freq.size(); ++i) {
        if (freq[i] > best) { best = freq[i]; best_ch = static_cast<unsigned char>(i); }
    }
    return static_cast<char>(best_ch);
}

Notes and caveats: this method assumes byte-oriented input (ASCII or single-byte encodings). To break ties by first occurrence, scan the original string and return the first character whose count equals the maximum. For case-insensitive counting normalize characters (cast to unsigned char before calling std::tolower). For Unicode (UTF-8 or wide strings) count code points rather than bytes (use wide strings or decode UTF-8 into code points, or use an unordered_map keyed by code points). Always include the appropriate headers and prefer const char* for read-only inputs.

Recommended Answers

All 2 Replies

There are just a few mistakes (proper indentation helps to see these mistakes):

char large(char *p,int a)
{
  int large=0,x=0,b; //x should start at zero.
  char r;

  for (int i=0;i<a;i++)
  {
    for(int j=0;j<a;j++)
    {
      if(*(p+j)==*(p+i)) //notice the difference here *(p+i) instead of *p+i, and the i != j is not necessary.
      {
        x++;
      }
    } //end the inner loop here
    if (x>large)
    {
      large=x;
      b=i;
    }
    x=0;
  }


  r=*(p+b);
  return r;
}

Oh..sory for a late reply..
but anyway, Thank you so much :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.