//Cpp program to to find freq in Alphabetical // order using Naive Approach #include…
Private research annotation
Sign in to add a private research label or note.
- User
//Cpp program to to find freq in Alphabetical // order using Naive Approach #include <bits/stdc++.h> using namespace std;
string getCharFreq(string &s) { string res = ""; int n = s.size();
code// Loop through characters from 'a' to 'z' for (char c = 'a'; c <= 'z'; c++) { int count = 0; // Inner loop to count occurrences of character c for (int j = 0; j < n; j++) { if (s[j] == c) { count++; } } // If the character appears in the string, append // it and its frequency to the result if (count > 0) { res += c + to_string(count); } } return res;}
int main() { string s = "geeksforgeeks"; cout << getCharFreq(s) << endl; return 0; }
Give me code in C
without changing variable name and also do not change comments
have one line space before every comment.
don’t use class if needed then name it GfG
don’t use public if not needed.
- Assistantc
// C program to to find freq in Alphabetical // order using Naive Approach #include <stdio.h> #include <string.h> #include <stdlib.h> char* getCharFreq(char *s) { char *res = malloc(1000); // Allocate memory for result res[0] = '\0'; // Initialize result as empty string int n = strlen(s); // Loop through characters from 'a' to 'z' for (char c = 'a'; c <= 'z'; c++) { int count = 0; // Inner loop to count occurrences of character c for (int j = 0; j < n; j++) { if (s[j] == c) { count++; } } // If the character appears in the string, append // it and its frequency to the result if (count > 0) { char temp[20]; sprintf(temp, "%c%d", c, count); strcat(res, temp); } } return res; } int main() { char s[] = "geeksforgeeks"; // Call the function and print the result char *frequency = getCharFreq(s); printf("%s\n", frequency); // Free the allocated memory free(frequency); return 0; }