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

C / C++ / MFC

 
AnswerRe: Is this possible String Encryptin result contains only alphanumric value using Base64. Pin
jschell4-Jul-11 6:53
jschell4-Jul-11 6:53 
QuestionRandom string generation not working fine? [modified] Pin
Le@rner1-Jul-11 23:40
Le@rner1-Jul-11 23:40 
AnswerRe: Random string generation not working fine? Pin
Richard MacCutchan2-Jul-11 0:47
mveRichard MacCutchan2-Jul-11 0:47 
GeneralRe: Random string generation not working fine? Pin
Le@rner2-Jul-11 1:23
Le@rner2-Jul-11 1:23 
GeneralRe: Random string generation not working fine? Pin
Richard MacCutchan2-Jul-11 2:35
mveRichard MacCutchan2-Jul-11 2:35 
GeneralRe: Random string generation not working fine? Pin
Le@rner3-Jul-11 20:57
Le@rner3-Jul-11 20:57 
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 
It's probably not the shortest possible solution, but to give you an idea maybe:

void sortarray(char str[],char *final)
{
	int counter[26] = {0};			// Counter for each of 26 letters, initialised at 0.
	int finallength = 0;			// Length of the final array.
	int i;

	// Count occurences.
	for(i=0;str[i];i++)			// Loop until the terminating zero.
		counter[str[i]-'a']++;		// [str[i]-'a'] is a number 0-25 for the letters a-z.

	// Calculate length of final array.
	for(i=0;i<26;i++){			// Based on number of occurences for each letter.
		if(counter[i])
			finallength += 4;
		if(counter[i] > 9)
			finallength ++;
	}
	
	// Allocate final array.
	final = (char*)malloc(finallength+1);
	final[0] = 0;				// Initialise to empty string for strcatting.

	// Fill the final array.
	char part[6];
	for(i=0;i<26;i++)
		if(counter[i]){
			sprintf(part,"%c %d;",i+'a',counter[i]);
			strcat(final,part);
		}
	final[finallength-1] = 0;		// Remove the last semicolon.

	// Print the final array.
	printf(final);
}


I copied some behaviour from your code which I would like to comment on though:
1. sortarray receives char * final, i.e. a single pointer. While you can use malloc on this, the function which calls sortarray, does not get its pointer to final updated. To solve this, you would need to pass a double pointer (so char **).
2. We're ignoring the case where a letter may occur more than 99 times in a string. It's probably a fair assumption but it's good to be aware of this. Smile | :)

modified 13-Sep-18 21:01pm.

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 
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 

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.