Find the second most frequent character in a given string

 Problem statement:- Program to Find the second most frequent character in a given string.

Example:-

              Input: Given

                                 String=C STRING PROGRAM

             Output:G

            Input: Given

                                String=string question

             Output: e

Data requirement:-


   Input Data:- str

  Output Data:-
sec_max

  Additional Data:- 
i, arr, max

Program in C

Here is the source code of the C Program to Find the second most frequent character in a given string.

Code:

#include<stdio.h>
#include<string.h>
main()
{
    char str[30];
    printf("Enter your String:");
    scanf("%[^\n]",str);
    int i,max=0,sec_max=0;
    int arr[256]={0};
    for(i=0;i<strlen(str);i++)
    {
        if(str[i]==' ')
            continue;
            arr[str[i]]++;
    }
    for (i = 0; i <256; i++) {
    if (arr[i]>arr[max]) {
            sec_max=max;
            max = i;
    }
    else if(arr[i]>arr[sec_max] && arr[i]!=arr[max]){
            sec_max=i;
   }
  }
    printf("The Second Most occurring character in a string is %c",sec_max);
}

Input/Output:
Enter your String:C STRING PROGRAM
The Second Most occurring character in a string is G

Program in C++

Here is the source code of the C++ Program to Find the second most frequent character in a given string.

Code:

#include<iostream>
#include <cstring>
using namespace std;
main()
{
    string str;
    cout<<"Enter your String:";
    getline(cin,str);
   int i,maximum=0,sec_max=0,len,in;
    for(in=0;str[in]!='\0';in++)
    {
        len++;
    }
    int arr[256]={0};
    for(i=0;i<len;i++)
    {
        if(str[i]==' ')
            continue;
            arr[str[i]]++;
    }
    for (i = 0; i <256; i++) {
    if (arr[i]>arr[maximum]) {
            sec_max=maximum;
            maximum = i;
    }
    else if(arr[i]>arr[sec_max] && arr[i]!=arr[maximum]){
            sec_max=i;
   }
  }
    cout<<"The Second Most occurring character in a string is "<<char(sec_max);
}

Input/Output:
Enter your String:
string question
The Second Most occurring character in a string is e

Program in Java

Here is the source code of the Java Program to Find the second most frequent character in a given string.

Code:

import java.util.Scanner;
public class SecondMostOccurringCharacter {

public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
String str;
System.out.println("Enter your String:");
str=cs.nextLine();
int i,max=0,sec_max=0;
    int arr[]=new int[256];
    for(i=0;i<str.length();i++)
    {
        if(str.charAt(i)!=' ')
           arr[str.charAt(i)]++;
    }
    for (i = 0; i <256; i++) {
        if (arr[i]>arr[max]) {
                sec_max=max;
                max = i;
        }
        else if(arr[i]>arr[sec_max] && arr[i]!=arr[max]){
                sec_max=i;
       }
      }
System.out.print("The Second Most occurring character in a string is "+(char)(sec_max));
cs.close();
}
}

Input/Output:
Enter your String:
java string question
The Second Most occurring character in a string is e

Program in Python

Here is the source code of the Python Program to Find the second most frequent character in a given string.

Code:

str=input("Enter Your String:")
arr=[0]*256
max=0
sec_max=0
i=0
for i in range(len(str)):
    if str[i]!=' ':
        num=ord(str[i])
        arr[num]+=1
for i in range(256):
    if arr[i] > arr[max]:
        sec_max = max
        max = i
    elif arr[i]>arr[sec_max] and arr[i]!=arr[max]:
        sec_max = i
print("The Second Most occurring character in a string is "+(chr)(sec_max))

Input/Output:
Enter Your String:python string program
The Second Most occurring character in a string is g

More:-

Post a Comment

0 Comments