Thursday, September 13, 2018

FREQUENCY OF EACH

  • Problem Description

    Write a C program to count frequency of each character in a string using loop.
  • CODING ARENA
  • #include <stdio.h>
    #include<string.h>
    int main()
    {
      char str[100];
      int c=0;
      int count[26]={0},x;
      scanf("%s",str);
      while(str[c]!='\0')
      {
        if(str[c]>='a'&&str[c]<='z')
        {
          x=str[c]-'a';
          count[x]++;
        }
    c++;
    }
    for(c=0;c<26;c++)
    {
      if(count[c]!=0)
      printf("%c = %d\n",c+'a',count[c]);
    }
    return 0;
    }
  • Test Case 1

    Input (stdin)
    srmuniversitylearningcentre
    
    
    Expected Output
    a = 1
    
    c = 1
    
    e = 4
    
    g = 1
    
    i = 3
    
    l = 1
    
    m = 1
    
    n = 4
    
    r = 4
    
    s = 2
    
    t = 2
    
    u = 1
    
    v = 1
    
    y = 1
  • Test Case 2

    Input (stdin)
    srmapplelab
    
    
    Expected Output
    a = 2
    
    b = 1
    
    e = 1
    
    l = 2
    
    m = 1
    
    p = 2
    
    r = 1
    
    s = 1

No comments:

Post a Comment