01/10/2018, 15:00

In ra ký tự xuất hiện nhiều lần nhất trong chuỗi

Đề bài là tìm ra ký tự xuất hiện nhiều lần nhất trong chuỗi (trừ khoảng trắng)

Đề có kèm theo file INP như sau :
“what good is money if it can not buy happiness
do not waste your time on a man who is not willing to waste their time on you
it is what is in yourself that makes you happy or unhappy
a true friend is someone who reaches for your hand and touches your heart”

Và mình thì chỉ tìm được số lần xuất hiện của ký tự xuất hiện nhiều lần nhất trong chuỗi thôi nhưng khi in ra ký tự đó thì không biết phải làm sao (ký tự xuất hiện nhiều nhất trong chuỗi là “o” và xuất hiện 23 lần).

Đây là code của mình :

void CharAppearsTheMost(char *s)
{
	FILE *f;
    f = fopen("STRING.INP", "rt");
	int d[256] = {0};
	int max = 0;
	while (!feof(f))
	{
		fgets(s, 256, f);
		for (int i = 0;i < strlen(s);i++)
		{
			if (s[i] != ' ')
			d[s[i]]++;
		}
		for (int i = 0;i < strlen(s);i++)
		{
			if (s[i] != ' ' && d[s[i]] != 0)
			{
				if (d[s[i]] > max)
					max = d[s[i]];
			}
		}
	}
	fclose(f);
    FILE *g=fopen("STRING.OUT","wt");
    fprintf(g, "%d", max);
    fclose(g);
}

Xin cảm ơn.

*grab popcorn* viết 17:15 ngày 01/10/2018
stackoverflow.com

Printing a char with printf

c++, c, printf, sizeof
asked by user535450 on 02:58PM - 19 Jan 11
Bài liên quan
0