The following is the piece of code, it takes a string and outputs the characters that have repeated with corresponding frequency. The code given below is compiled and tested.


//Import the header files
#include<stdio.h>
#include<conio.h>
#include<string.h>

//Main function
main(){
  //Variables initialization
  int i=0,j,k=0,m=0,f=0,len,count=0;
  char ch,aj[30],r[30];

  //Read the string
  printf("Enter the string:\n");
  while(1){
    scanf("%c",&ch);
    if(ch=='e'){
      break;
    }
    aj[i++]=ch;
  }

  //print the enetered string
  len=strlen(aj);
  for(i=0;i<len;i++){
    printf("%c",aj[i]);
  }

  //A brute force algorithm
  for(i=0;i<len;i++){
    for(j=i+1;j<len;j++){
      if(aj[i]==aj[j]){
        for(m=0;m<strlen(r);m++){
          if(aj[i]==r[m]){
            f=1;
          }
        }
        if(f==0){
          r[k]=aj[i];
          k++;
        }
        else{
          break;
        }
      }
    }
  }

  //print the results
  printf("Characters that repeat are:\n");
  for(i=0;i<strlen(r);i++){
    count=0;
    for(m=0;m<len;m++){
      if(r[i]==aj[m]){
        count++;
      }
    }

    //Print the stats
    printf("Character\t %c\t Frequency\t %d\n",r[i],count);
  }
}



blog comments powered by Disqus