C language statistics number of characters code sharing

  • 2020-04-02 03:07:44
  • OfStack

C language to achieve the number of statistical characters


#include<stdio.h>
int main()
{
  int sz[10]={0},zm[26]={0},z[26]={0},i,space=0,e=0,t=0;
  char c;
  printf(" Please enter a string to count the number of characters n");
  while((c=getchar())!='n')
  {
    if(c<='z'&&c>='a')
      zm[c-'a']++;
    else if(c<='Z'&&c>='A')
      z[c-'A']++;
    else if(c<='9'&&c>='0')
      sz[c-'0']++;
    else if(c==' ')
      space++;
    else
      e++;
  }
  printf("nn");
  for(i=0;i<=9;i++)
  {
    t++;
    printf(" %d The number of %d  ",i,sz[i]);
    if(t%3==0)
      printf("n");
  }
  t=0;
  printf("nnn");
  for(i=0;i<=25;i++)
  {
    t++;
    printf(" %c The number of %d  ",i+97,zm[i]);
    if(t%3==0)
      printf("n");
  }
  t=0;
  printf("nnn");
  for(i=0;i<=25;i++)
  {
    t++;
    printf(" %c The number of %d  ",i+65,z[i]);
    if(t%3==0)
      printf("n");
  }
  t=0;
  printf("nnn");
  printf("  The number of Spaces is zero %dnn",space);
  printf("  The number of other characters is %dn",e);
  return 0;
}

To a C language statistics input number of characters code


#include <stdio.h>

#define MAXWORD 30  //The maximum length of a word
#define IN 1  //Within the word, that is, no Spaces are encountered
#define OUT 0  //Outside the word, a space is encountered



int wl()
{
  char c;
  int i;
  int nc;
  int state;  //State is the state of the word: IN or OUT;
  int overflow;  //The number of words in MAXWORD
  int wl[MAXWORD]; //Character length statistics of length 1 to 30

  state = OUT;
  nc = 0;
  overflow = 0;

  for(i = 1; i < MAXWORD; ++i)
    wl[i] = 0;

  while((c = getchar()) != EOF)
    if(c == ' ' || c == 'n' || c == 't')
    {
      state = OUT;

      if(nc > 0)
        if(nc < MAXWORD)
          ++wl[nc];
        else
          ++overflow;
      nc = 0;
    }
    else if(state == OUT)
    {
      state = IN;
      nc = 1;
    }
    else
      ++nc;


    for(i = 1; i < MAXWORD; ++i)
      printf(" Length is: %d The number of words is: %d : nn", i, wl[i]);

      return 0;
}
main()
{
  wl();
}

The above is all the content of this article, I hope you can enjoy it


Related articles: