Zliczanie wystąpień tego samego znaku pod rząd

0

Napisałem taki program:


#include "stdafx.h"
#include "string.h"
#include "ctype.h"

int count_nonspace(const char* str)
{
	int count = 0;
	while (*str)
	{
		if (!isspace(*str++))
			count++;
	}
	return count;
}


int _tmain(int argc, _TCHAR* argv[])
{

	

	int a[127];
	int i = 0, j = 0, count[127] = { 0 };
	int cur_count = 1; /* Gets compared with value in count[] */
	char cur_char = '\0';
	char string[100] = "Hellooooo world";




	for (i = 0; i < strlen(string); i++)
	{
		if (cur_char == string[i])
		{
			cur_count++;
		}
		else
		{
			if (32 < cur_char && cur_char < 127)
			{
				if (cur_count > count[cur_char])
				{
					count[cur_char] = cur_count;
				}
			}
			cur_char = string[i];
			cur_count = 1;
			if (32 < cur_char && cur_char < 127)
			{
				if (!(count[cur_char]))
				{
					count[cur_char] = cur_count;
				}
			}
		}
	}

	/* Find the most consecutive char and print it. */
	char max_char = '\0';
	int max_count = 0;
	for (j = 0; j < 127; j++)
	{
		if (max_count < count[j])
		{
			max_count = count[j];
			max_char = j;
		}
	}
	printf("%c\n", max_char);
}

 

Zlicza on ile dany znak wystepuje pod rzad i drukuje ten który występuje największa ilosc razy ale uzywa w tym celu stringów, chciałbym to zamienić na program który robi to samo ale w pliku, jak to zrobić? Bardzo proszę o konkretne odpowiedzi, które doprowadzą mnie do konkretnych rozwiązań. Jakby ktoś miał wątpliwości uzywam visual studio 2013.

0

wczytaj plik to jednej zmiennej i zrob to samo co na stringu.

0
 // ConsoleApplication23.cpp : Defines the entry point for the console application.
//



#include "stdafx.h"
#include "string.h"
#include "ctype.h"

int count_nonspace(const char* str)
{
	int count = 0;
	while (*str)
	{
		if (!isspace(*str++))
			count++;
	}
	return count;
}


int _tmain(int argc, _TCHAR* argv[])
{
	int a[127];
	int i = 0, j = 0, count[127] = { 0 };

	int cur_count = 1; /* Gets compared with value in count[] */
	char cur_char = '\0';
	char buffer[100];
	char max_char = '\0';
	int max_count = 0;
	FILE *fp = fopen("a.txt", "wr");                 
	while (fgets(buffer, sizeof(buffer), fp)) {
		for (i = 0; i < strlen(buffer); i++)
		{
			if (cur_char == buffer[i])
			{
				cur_count++;
			}
			else
			{
				if (32 < cur_char && cur_char < 127)
				{
					if (cur_count > count[cur_char])
					{
						count[cur_char] = cur_count;
					}
				}
				cur_char = buffer[i];
				cur_count = 1;
				if (32 < cur_char && cur_char < 127)
				{
					if (!(count[cur_char]))
					{
						count[cur_char] = cur_count;
					}
				}
			}
		}

		/* Find the most consecutive char and print it. */
		
		for (j = 0; j < 127; j++)
		{
			if (max_count < count[j])
			{
				max_count = count[j];
				max_char = j;
			}
		}
	}
	printf("%c\n", max_char);
	fclose(fp);
}

Napisałem coś takiego, a to nadal nie czyta tego pliku prawidłowo, moim zdaniem nie zlicza w ogole bo dostaje printf jakiegoś znaku który nie występuje pliku, co więc napisałem źle?

1 użytkowników online, w tym zalogowanych: 0, gości: 1