Ad

In this challenge, first we want to find out the maximum value between number of occurrences for each characters, then only return characters which have the same number of occurrences as maximum value.

For example, "abcde" is a valid string, because max count of characters is 1, and other all character counts are one, so we can return "abcde". On the other hand, "abcdeee" is not valid, because count of 'e' is 3, not 1 as the others. So we need to get rid of "a","b","c" and "d", so we can return "eee".

Specification

string_editor(str)
Remove all characters from string according to maximum count of a character in the input String

Parameters
str: char* - input to work on

Return Value
char* - final decision about that string

Constraints
Input string consists of English alphabet characters
All of the characters are lowercase

Examples
"cac" => returns "cc"
"aabcc" => returns "aacc"
"aaabbbg" => returns "aaabbb"
"pppenm" => returns "ppp"
"ekekua" => returns "ekek"

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

char* string_editor(const char* str)
{
	size_t s = 0;
	size_t count[256] = { 0 };
	size_t size = strlen(str);
	size_t max = 0;
	char *new = (char*)calloc(size + 1, sizeof(char));
	for (size_t i = 0; i < size; ++i)
		++count[str[i]];
	for (size_t i = 0; i < size; ++i)
		if (count[str[i]] > max)
			max = count[str[i]];
	for (size_t i = 0; i < size; ++i)
		if (count[str[i]] == max)
			new[s++] = str[i];
	new = (char *)realloc(new, (s + 1) * sizeof(char));
	new[s] = '\0';
	return new;
}

Examples

"cac" => returns "cc"

"aabcc" => returns "aacc"

"aaabbbg" => returns "aaabbb"

"pppenm" => returns "ppp"

"ekekua" => returns "ekek"

Code
Diff
  • #include <stdlib.h>
    #include <string.h>
    
    char* string_editor(const char* str)
    {
    	size_t s = 0;
    	size_t size = strlen(str);
    	char *new = (char*)calloc(size+1,sizeof(char));
    	for (size_t i = 0; i < size; i++)
    		for (size_t j = 0; j < size; j++)
    			if ((str[i] == str[j]) && (i != j))
    			{
    				new[s++] = str[i];
    				break;
    			}
    	new = (char *)realloc(new, (s + 1) * sizeof(char));
    	new[s] ='\0';
    	return new;
    }
    • #include <stdlib.h>
    • #include <stdio.h>
    • #include <string.h>
    • char* string_editor(char* str)
    • char* string_editor(const char* str)
    • {
    • char temp = 0;
    • char size = (char)strlen(str);
    • char *new = (char*)calloc(size,sizeof(char));
    • for (char i = 0; i < size; i++)
    • for (char j = 0; j < size; j++)
    • size_t s = 0;
    • size_t size = strlen(str);
    • char *new = (char*)calloc(size+1,sizeof(char));
    • for (size_t i = 0; i < size; i++)
    • for (size_t j = 0; j < size; j++)
    • if ((str[i] == str[j]) && (i != j))
    • new[temp++] = str[i];
    • new[temp] ='\0';
    • {
    • new[s++] = str[i];
    • break;
    • }
    • new = (char *)realloc(new, (s + 1) * sizeof(char));
    • new[s] ='\0';
    • return new;
    • }
Code
Diff
  • #include <stdlib.h>
    #include <string.h>
    
    char *string_editor(const char *str_in) {
    	  size_t lens = strlen(str_in), s = 0;
    	  char *str_out = (char *) calloc(lens + 1, sizeof(char));
    	  for(size_t i=0; i<lens; i++) {
            char curr = str_in[i];
    		    for(size_t j=0; j<lens; j++) {
    			      if(str_in[j] == curr && i != j) {
    				        str_out[s++] = curr;
                    break;
                }
            }
        }
        str_out = (char *) realloc(str_out, (s + 1) * sizeof(char));
        return str_out;
    }
    • #include <stdlib.h>
    • #include <string.h>
    • char *string_editor(const char *str_in) {
    • size_t lens = strlen(str_in), s = 0;
    • char *str_out = (char *) calloc(lens + 1, sizeof(char));
    • for(size_t i=0; i<lens; i++) {
    • char curr = str_in[i];
    • for(size_t j=0; j<lens; j++) {
    • if(str_in[j] == curr && i != j) {
    • str_out[s++] = curr;
    • break;
    • }
    • }
    • }
    • str_out = (char *) realloc(str_out, (s + 1) * sizeof(char));
    • return str_out;
    • }

Examples
"cac" => returns "cc"
"aabcc" => returns "aacc"
"aaabbbg" => returns "aaabbb"
"pppenm" => returns "ppp"
"ekekua" => returns "ekek"

Examples
"cac" => returns "cc"
"aabcc" => returns "aacc"
"aaabbbg" => returns "aaabbb"
"pppenm" => returns "ppp"
"ekekua" => returns "ekek"

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

char* string_editor(char* str)
{
	char temp = 0;
	char size = (char)strlen(str);
	char *new = (char*)calloc(size,sizeof(char));
	for (char i = 0; i < size; i++)
		for (char j = 0; j < size; j++)
			if ((str[i] == str[j]) && (i != j))
				new[temp++] = str[i];
	new[temp] ='\0';
	return new;
}