如何在C中仅打印a到z

如何在C中仅打印a到z,c,char,histogram,lowercase,C,Char,Histogram,Lowercase,我试图使我的直方图只打印a到z,而不是空格或其他符号。根据下面的代码,我如何实现这一点。我已经将c设置为忽略大小写,但我不知道如何忽略空格等 #include <stdio.h> #include <string.h> #include <ctype.h> #define READ 5000 main() { int c; int i; int x; int length[READ]; { for (x

我试图使我的直方图只打印a到z,而不是空格或其他符号。根据下面的代码,我如何实现这一点。我已经将c设置为忽略大小写,但我不知道如何忽略空格等

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define READ 5000

main() {
    int c;
    int i;
    int x;
    int length[READ];
    {
        for (x = 0; x < READ; x++)
            length[x] = 0;
        while ((c = getchar()) != EOF) {
            c = tolower(c); // ignores all cases.
            length[c]++;
            if (c == EOF)
                break;
        }
    }
    for (x = 0; x < READ; x++) {
        if (length[x] > 0) {
            printf("%c| ", x);
            for (i = 1; i <= length[x] / 5; ++i) {
                // since the value i is int when a value less than 5
                // such as 4/5 appears as   0. 5/5 to 9/5 is 1 "x"
                printf("X"); // prints an X for every 5 of a character
            }
            printf("\b\b\b\b\b\t\t\t(%d)", length[x]);
            printf("\n"); // aligns the histogram with new line
        }
    }
}
#包括
#包括
#包括
#定义读取5000
main(){
INTC;
int i;
int x;
整数长度[读取];
{
对于(x=0;x0){
printf(“%c |”,x);

对于(i=1;i不是100%确定您在寻找什么,但可能:

  #include <stdio.h>
  #include <string.h>
  #include <ctype.h>

  int main ( void ) {
     int c;
     int i;
     int x;
     int n;  
     int count[26];
     int N = (sizeof(count)/sizeof(count[0]));

     for(x = 0; x < N; x++) count[x] = 0;
     while ((c = getchar() ) != EOF) {
        if (!isalpha(c)) continue;
        i = tolower(c) - 'a';
        count[i]++;
     }
     for (x = 0; x < N; x++) {
        if (count[x] == 0) continue;
        printf("%c| ", x + 'a');
        n = count[x]/5;
        for(i = 1; i <= n; ++i) printf("X");
        printf(" %*s(%d)\n", 50 - n, "", count[x]);
     }
     return 0;
}
#包括
#包括
#包括
内部主(空){
INTC;
int i;
int x;
int n;
整数计数[26];
int N=(sizeof(count)/sizeof(count[0]);
对于(x=0;xisalpha()
是开始过滤非alpha
char
的最佳位置

使用
#定义读取(UCHAR\u MAX+1)

要打印直方图,请使用
printf()
中的返回值报告打印的字符数以计算偏移量。使用
“%*s”
打印所需的间隔符

for (x = 0; x < READ; x++) {
  int maxoffset = 0;
  if (length[x] > 0) {
    char buffer[100];
    int offset = sprintf(buffer, "%c| ", x);
    for (i = 1; i <= length[x] / 5; ++i) {
      offset += printf("X"); // prints an X for every 5 of a character
    }
    // Print length on the 40th column
    offset = 40 - offset;
    if (offset <= 0) offset = 1;
    printf("%*s(%d)\n", offset, "", length[x]);
  }
(x=0;x{ int maxoffset=0; 如果(长度[x]>0){ 字符缓冲区[100]; int offset=sprintf(缓冲区,“%c |”,x);
对于(i=1;我可能是<代码>(如果是α(c))(或<代码>如果((ISOPHALL(C))< /COD>,根据)有帮助吗?C和C++不是同一种语言,在很多情况下,是“自然的”。一种语言的答案可能不适用于另一种语言。选择您正在使用的语言并仅标记该语言。@crashmstr我更喜欢C。解决了我的问题@jerrycoff注意只有256个字节的值,因此5000是严重的过量(几乎比需要的长20倍)。更一般地说,使用
count[256]
,省略
-'a'
+'a'
部分。“注意:除了我们ascii以外,大多数情况下都不适用。”因此,不要继续使用ascii的神话。合理的假设是
字符
是8位,因此有256个
字符
值。
for (x = 0; x < READ; x++) {
  int maxoffset = 0;
  if (length[x] > 0) {
    char buffer[100];
    int offset = sprintf(buffer, "%c| ", x);
    for (i = 1; i <= length[x] / 5; ++i) {
      offset += printf("X"); // prints an X for every 5 of a character
    }
    // Print length on the 40th column
    offset = 40 - offset;
    if (offset <= 0) offset = 1;
    printf("%*s(%d)\n", offset, "", length[x]);
  }