Click here to Skip to main content
15,883,767 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Is this possible String Encryptin result contains only alphanumric value using Base64. Pin
Randor 4-Jul-11 3:34
professional Randor 4-Jul-11 3:34 
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 
hi, i have C exam next week, so i started solving questions from previous years exams and run into this question, the answer i come up to is far far from having reasonable length, it's too complicated and long , but i can't come up anything shorter.

the question is: write a function that receives an array of letters , and a pointer to final result string that hasn't allocated yet, the function supposed to take the array of letters and count the appearance of every letter and make the final result string look like that:

received array of letters : "abbacdeefg"
result string: "a 2;b 2;c 1;d 1;e 2;f 1;g 1"
in the result it should be alphabetically printed

here is the code that i wrote, it just doesn't makes sense that this is the actual solution:

void sortarray(char str[],char * final)
{
	int i = 1,j, extra = 0;
	char * letters, *lerrptr;
	int * count, * cerrptr;
	int size = 0, flag = 0;

	// allocate memory for one array storing letter and one for storing it's appearance
	letters = (char *) malloc(sizeof(char) * 1);
	if(letters == NULL) exit(1);
	count = (int *) malloc(sizeof(int) * 1);
	if(count == NULL) exit(1);

	// fill the first letter to array
	letters[0] = str[0];
	count[0] = 1;
	size++;

	// scan the whole string
	while(str[i] != 0)
	{
		// initiate flag for new run
		flag = 0;

		for(j = 0 ; j < size ; j++)
		{
			// if the letter already appeared before just increment the count and raise the flag that no new allocating is necessary
			if(str[i] == letters[j]) 
			{
				count[j]++;
				flag = 1;
			}
		}	
		// if the flag of already appeared hasn't been raised make new allocation for new letter
		if(!flag)
		{
			flag = 0 ;
			size++;
			lerrptr = letters;
			letters = (char *) realloc(letters,sizeof(char) * size);
			if(letters == NULL)
			{
				free(lerrptr);
				free(count);
				exit(1);
			}
			cerrptr = count;
			count = (int *) realloc(count,sizeof(int) * size);
			if(letters == NULL)
			{
				free(cerrptr);
				free(letters);
				exit(1);
			}
			// insert new letter and 1 for the count
			letters[size-1] = str[i] ;
			count[size-1] = 1 ;
				
		}

		i++;
	}
		
	// bubble sort the letters 
	for(i = 0 ; i < size - 1; i++)
	{
		for(j = 0 ; j < size - 1 - i ; j++)
		{
			if(letters[j] < letters[j - 1])
			{
				letters[j] ^= letters[j+1];
				letters[j+1] ^= letters[j];
				letters[j] ^= letters[j+1];
				count[j] ^= count[j+1];
				count[j+1] ^= count[j];
				count[j] ^= count[j+1];
			}
		}
	}
	
	// check for more 9 appearances to reserve more letters for one more digit
	for(i = 0 ; i < size ; i++) if(count[i] > 9) extra++;

	// allocate the memory for the final array that is going to be printed
	final = (char *) malloc(sizeof(char) * (size * 4) + extra + 1);
	
	j = 0;
	for(i = 0 ; i < size ; i++)
	{
		// insert the letter
		final[i*4 + j] = letters[i];
		// insert space
		final[i*4 + 1 + j] = ' ';

		// if more then one digit used for the count of appearances
		if(count[i] > 9)
		{
			// first digit
			final[i*4 + 2 + j] = count[i]/10 + 48;
			j++;
			// second
			final[i*4 + 2 + j] = count[i]%10 + 48 ;
		}
		else final[i*4 + 2 + j] = count[i]  + 48;
		
		// print ; 
		final[i*4 + 3 + j] = ';';
	}

	// add terminating character
	final[i*4 + j] = 0;
	// print the result
	printf(" %s ", final);
}

can anyone help me come up with more reasonable solution?
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 
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 

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.