Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

40,039 questions

52,004 answers

573 users

How to remove odd frequency characters from a string in C

1 Answer

0 votes
#include <stdio.h>
#include <string.h>

#define MAX_CHAR 256

void removeOddFrequencyChars(char *str) {
    int freq[MAX_CHAR] = {0};
    int len = strlen(str);

    // Count frequency of each character
    for (int i = 0; i < len; i++) {
        freq[(int)str[i]]++;
    }

    // Construct new string with only even frequency characters
    int index = 0;
    for (int i = 0; i < len; i++) {
        if (freq[(int)str[i]] % 2 == 0) {
            str[index++] = str[i];
        }
    }
    str[index] = '\0'; // Null-terminate the new string
}

int main() {
    char str[] = "c programming version 23";
    
    removeOddFrequencyChars(str);
    
    printf("%s\n", str);
    
    return 0;
}

 

/*
run:

ogmmingion

*/
 

 



answered Nov 19, 2024 by avibootz

Related questions

1 answer 192 views
1 answer 93 views
1 answer 100 views
1 answer 90 views
1 answer 81 views
1 answer 87 views
...