Find frequency of characters in a string

Problem statement:- Program to Find the frequency of characters in a string.

Data requirement:-

   Input Data:- str

  Output Data:-arr

  Additional Data:- i, len

Program in C

Here is the source code of the C Program to Find the frequency of characters in a string.

Code:

#include<stdio.h>
#include<string.h>
int main()
{
    char str[30];
    printf("Enter your String:");
    scanf("%[^\n]",str);
    int i;
    int arr[256]={0};
    for(i=0;i<strlen(str);i++)
    {
        if(str[i]==' ')
            continue;
            arr[str[i]]++;
    }
    printf("Frequency of character in a string are:\n");
    for(i=0;i<256;i++)
    {
        if(arr[i]!=0)
        {
          printf("%c occurs %d times\n",(char)(i),arr[i]);
        }}
}

Input/Output:
Enter your String:c string program
Frequency of character in a string are:
a occurs 1 times
c occurs 1 times
g occurs 2 times
i occurs 1 times
m occurs 1 times
n occurs 1 times
o occurs 1 times
p occurs 1 times
r occurs 3 times
s occurs 1 times
t occurs 1 times

Program in C++

Here is the source code of the C++ Program to Find the frequency of characters in a string.

Code:

#include<iostream>
using namespace std;
int main()
{
    string str;
    cout<<"Enter your String:";
    getline(cin,str);
    int len=0;
    while(str[len]!='\0')
    {
        len++;
    }
    int arr[256]={0},i;
    for(i=0;i<len;i++)
    {
        if(str[i]==' ')
            continue;
            arr[str[i]]++;
    }
    cout<<"Frequency of character in a string are:\n";
    for(i=0;i<256;i++)
    {
        if(arr[i]!=0)
        {
          cout<<(char)(i)<<" occurs "<<arr[i]<<" times\n";
        }}
}

Input/Output:
Enter your String:c plus plus
Frequency of character in a string are:
c occurs 1 times
l occurs 2 times
p occurs 2 times
s occurs 2 times
u occurs 2 times

Program in Java

Here is the source code of the Java Program to Find the frequency of characters in a string.

Code:

import java.util.Scanner;
public class p23 {

public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
String str1;
System.out.println("Enter your String:");
str1=cs.nextLine();
char[] str=str1.toCharArray();
int i;
int arr[]=new int[256];
    for(i=0;i<str.length;i++)
    {
        if(str[i]==' ')
            continue;
            arr[str[i]]++;
    }
    System.out.println("Frequency of character in a string are:");
    for(i=0;i<256;i++)
    {
        if(arr[i]!=0)
        {
        System.out.println((char)(i)+" occurs "+arr[i]+" times");
        }}
    cs.close();
}}

Input/Output:
Enter your String:
java program
Frequency of character in a string are:
a occurs 3 times
g occurs 1 times
j occurs 1 times
m occurs 1 times
o occurs 1 times
p occurs 1 times
r occurs 2 times
v occurs 1 times

Program in Python

Here is the source code of the Python Program to Find the frequency of characters in a string.

Code:

str=input("Enter the String:")
arr=[0]*256
for i in range(len(str)):
    if str[i]==' ':
        continue
    num=ord(str[i])
    arr[num]+=1
print("Frequency of character in a string are:")
for i in range(256):
    if arr[i]!=0:
        print((chr)(i)," occurs ",arr[i]," times")

Input/Output:
Enter the String:csinfocom
Frequency of character in a string are:
c  occurs  2  times
f  occurs  1  times
i  occurs  1  times
m  occurs  1  times
n  occurs  1  times
o  occurs  2  times
s  occurs  1  times


Post a Comment

0 Comments