Tag: 计数

C:读取字符串时,整个ascii表的频率计数(尽可能接近)的可读方式是什么

从这个网站http://www.programmingsimplified.com/c-program-find-characters-frequency他们有一个例子,它将计算a – z而不是AZ或空格或标准标点符号。 while ( string[c] != ‘\0’ ) { /* Considering characters from ‘a’ to ‘z’ only */ if ( string[c] >= ‘a’ && string[c] <= 'z' ) count[string[c]-'a']++; c++; } for ( c = 0 ; c < 26 ; c++ ) { if( count[c] != 0 )

计算C中的字符

我正在尝试编写一个计算字符串中所有字符的程序。 我原本拥有它,但后来意识到我无法计算空间。 我不明白为什么这不起作用。 for(m=0; z[m] != 0; m++) { if(z[m] != ‘ ‘) { charcount ++; } } 任何协助赞赏。 编辑*如果像这样扫描输入(字符串),它会有所不同吗? 是的,一切都已初始化。 我已经尝试打印z [m]评估的内容并且它不是“m”处字符串的实际值,我认为这是问题所在。 for(j=0; j<7; j++){ printf("Enter a string:\n"); scanf("%s", z); for(m=0; z[m] != 0; m++){ if(z[m] != ' '){ charcount ++; } }

C – 文件中的字符数

所以我想创建一个程序来计算文件中每个字符的出现次数。 例如: 字符0x67(g)的4个实例 11个字符0x68(h)的实例 等等 我不知道如何显示和计算实例。 有什么想法吗? #include const char FILE_NAME[] = “input.txt”; #include int main() { int count = 0; /* number of characters seen */ FILE *in_file; /* input file */ /* character or EOF flag from input */ int ch; in_file = fopen(FILE_NAME, “r”); if (in_file == NULL) { printf(“Cannot open %s\n”, […]

计算unsigned int中位转换次数的最快方法

我正在寻找计算unsigned int中位转换次数的最快方法。 如果int包含: 0b00000000000000000000000000001010 转换次数为:4 如果int包含: 0b00000000000000000000000000001001 转换次数为:3 语言是C.

康威的生命游戏,计算邻居

我的代码中某处出现错误,我想我正在进入无限循环。 基本上我得到一个矩阵和两个索引,i,j,我需要计算[i] [j]周围有多少邻居的值为1或2。 这是我的代码: int number_of_living_neighbors(matrix mat,int i, int j, int n,int m) { int counter=0,row_index=0,column_index=0; for(row_index=i-1;row_index=0)&&(row_index=0)&&(column_index<m)) { if((mat[row_index][column_index]==1)||(mat[row_index][column_index]==2)) counter++; if((row_index==i)&&(column_index==j)&&(mat[i][j]==1)) counter–; } } } printf("The number of living neighbors is %d", counter); return counter; } 它没有打印任何东西。 mat是我得到的矩阵,i,j是指针,n是行数,m是列数。

文件中注释的字符数(C编程)

我似乎无法做对,尝试了一切,但.. int commentChars() { char str[256], fileName[256]; FILE *fp; int i; do{ long commentCount=0; fflush(stdin); printf(“%s\nEnter the name of the file in %s/”, p, dir); gets(fileName); if(!(fp=fopen(fileName, “r”))) { printf(“Error! File not found, try again”); return 0; } while(!feof(fp)) { fgets(str,sizeof str,fp); for(int i=0;i<=sizeof str;i++) { if(str[i] == '/' && str[i+1] == '/') { commentCount […]

计算数组中重复的元素

计算数组中的重复元素…..输入是{1,1,1,1,2,2,2,3,3,4}输出 1=4 2=3 3=2 4=1