Click here to Skip to main content
15,885,213 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
Questiontrying to solve exam question and getting unreasonable length code, can it be shorter? Pin
atikot1-Jul-11 9:31
atikot1-Jul-11 9:31 
AnswerRe: trying to solve exam question and getting unreasonable length code, can it be shorter? Pin
User 74293381-Jul-11 10:47
professionalUser 74293381-Jul-11 10:47 
GeneralRe: trying to solve exam question and getting unreasonable length code, can it be shorter? Pin
atikot1-Jul-11 10:56
atikot1-Jul-11 10:56 
GeneralRe: trying to solve exam question and getting unreasonable length code, can it be shorter? Pin
User 74293381-Jul-11 11:03
professionalUser 74293381-Jul-11 11:03 
GeneralRe: trying to solve exam question and getting unreasonable length code, can it be shorter? Pin
atikot1-Jul-11 11:04
atikot1-Jul-11 11:04 
GeneralRe: trying to solve exam question and getting unreasonable length code, can it be shorter? Pin
Philippe Mori1-Jul-11 12:35
Philippe Mori1-Jul-11 12:35 
AnswerRe: trying to solve exam question and getting unreasonable length code, can it be shorter? Pin
Philippe Mori1-Jul-11 12:20
Philippe Mori1-Jul-11 12:20 
AnswerRe: trying to solve exam question and getting unreasonable length code, can it be shorter? [modified] Pin
Alain Rist3-Jul-11 23:54
Alain Rist3-Jul-11 23:54 
Hi,
atikot wrote:
can anyone help me come up with more reasonable solution?
This is shorter:
// CharCount.c : Defines the entry point for the console application.

#include <string.h>
#include <stdlib.h>
#include <stdio.h>

void CharCount(char str[], char** result)
{
	int i;
	unsigned int count[256];
	if (!strlen(str))
		return;
	if (*result = (char*)malloc(min(strlen(str), 256) * 8)) // Allocate a 'reasonable' output size
	{
		char* out = *result;
		memset(count, 0, sizeof(count));
		for (i = 0; str[i]; i++)
			++count[str[i]];
		for (i = 0; i < 256; i++)
			if (count[i])
				out += sprintf(out, "%c %u;", (char)i, count[i]);
		*--out = '\0';
	}
}

int main()
{
	char* out = NULL; 
	CharCount("abbacdeefg", &out);
	if (out)
		printf("%s\n", out);
	free(out);
}


cheers,
AR

Code edited after Stefan_Lang[^]'s excellent review.
When the wise (person) points at the moon the fool looks at the finger (Chinese proverb)
modified on Monday, July 4, 2011 10:43 AM

GeneralRe: trying to solve exam question and getting unreasonable length code, can it be shorter? Pin
Stefan_Lang4-Jul-11 0:37
Stefan_Lang4-Jul-11 0:37 
GeneralRe: trying to solve exam question and getting unreasonable length code, can it be shorter? Pin
Alain Rist4-Jul-11 5:01
Alain Rist4-Jul-11 5:01 
QuestionC++ DLL for Metatrader 4 Platform Pin
babelproofreader1-Jul-11 8:24
babelproofreader1-Jul-11 8:24 
AnswerRe: C++ DLL for Metatrader 4 Platform Pin
Philippe Mori1-Jul-11 9:07
Philippe Mori1-Jul-11 9:07 
QuestionIOCP, Completion Key and Overlapped Pin
csrss1-Jul-11 7:39
csrss1-Jul-11 7:39 
AnswerRe: IOCP, Completion Key and Overlapped Pin
Mark Salsbery1-Jul-11 9:33
Mark Salsbery1-Jul-11 9:33 
GeneralRe: IOCP, Completion Key and Overlapped Pin
csrss1-Jul-11 10:48
csrss1-Jul-11 10:48 
GeneralRe: IOCP, Completion Key and Overlapped Pin
cmk1-Jul-11 11:37
cmk1-Jul-11 11:37 
GeneralRe: IOCP, Completion Key and Overlapped Pin
Mark Salsbery1-Jul-11 13:08
Mark Salsbery1-Jul-11 13:08 
GeneralRe: IOCP, Completion Key and Overlapped Pin
csrss1-Jul-11 19:51
csrss1-Jul-11 19:51 
GeneralRe: IOCP, Completion Key and Overlapped Pin
Mark Salsbery2-Jul-11 6:03
Mark Salsbery2-Jul-11 6:03 
AnswerRe: IOCP, Completion Key and Overlapped Pin
cmk1-Jul-11 10:46
cmk1-Jul-11 10:46 
GeneralRe: IOCP, Completion Key and Overlapped Pin
csrss1-Jul-11 11:13
csrss1-Jul-11 11:13 
QuestionEncryption/Decyption of String. Pin
Le@rner1-Jul-11 2:57
Le@rner1-Jul-11 2:57 
AnswerRe: Encryption/Decyption of String. Pin
Code-o-mat1-Jul-11 3:34
Code-o-mat1-Jul-11 3:34 
AnswerRe: Encryption/Decyption of String. Pin
cmk1-Jul-11 10:56
cmk1-Jul-11 10:56 
QuestionMaking an "SDI" MFC DLL Pin
Srikant Iyer1-Jul-11 1:46
Srikant Iyer1-Jul-11 1:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.