Computer Science, asked by prasannaprince21, 3 months ago

Write a program to input a line of text and a character from user to print the frequency of the

character in the line. For example

Line entered is : this is a golden pen

The character to search: e

Then output is: Frequency is 2​

Answers

Answered by janu519
5

Answer:

Create a count array to store the frequency of each character in the given string str. Traverse the string str again and check whether the frequency of that character is 0 or not. If not 0, then print the character along with its frequency and update its frequency to 0 in the hash table. This is done so that the same character is not printed again.

Explanation:

// C++ implementation to print the character and

// its frequency in order of its occurrence

#include <bits/stdc++.h>

using namespace std;

#define SIZE 26

// function to print the character and its frequency

// in order of its occurrence

void printCharWithFreq(string str)

{

// size of the string 'str'

int n = str.size();

// 'freq[]' implemented as hash table

int freq[SIZE];

// initialize all elements of freq[] to 0

memset(freq, 0, sizeof(freq));

// accumulate freqeuncy of each character in 'str'

for (int i = 0; i < n; i++)

freq[str[i] - 'a']++;

// traverse 'str' from left to right

for (int i = 0; i < n; i++) {

// if frequency of character str[i] is not

// equal to 0

if (freq[str[i] - 'a'] != 0) {

// print the character along with its

// frequency

cout << str[i] << freq[str[i] - 'a'] << " ";

// update frequency of str[i] to 0 so

// that the same character is not printed

// again

freq[str[i] - 'a'] = 0;

}

}

}

// Driver program to test above

int main()

{

string str = "geeksforgeeks";

printCharWithFreq(str);

return 0;

Answered by jai696
2

\huge\red{\mid{\fbox{\tt{Using\: Python\: 3}}\mid}}

def c_freq(line, c):

c_freq = 0

for x in list(line):

if x == c:

c_freq += 1

return c_freq

line, c = input("line: "), input("char: ")

print(f"Frequency is {c_freq(line, c)}")

\large\mathsf\color{lightgreen}useful?\: \color{white}\longrightarrow\: \color{orange}brainliest!

Similar questions