C 查找字符串中字母的总数。请也签出我的代码
我正试图写这段代码,虽然构建成功,但输出只有一个字符 我正在用x代码写这个代码。我从一门课上得到了这个密码。这完全是在他的代码块上工作。我试着在代码块上做,但在代码块上也不起作用。如果是错的,请帮助我C 查找字符串中字母的总数。请也签出我的代码,c,C,我正试图写这段代码,虽然构建成功,但输出只有一个字符 我正在用x代码写这个代码。我从一门课上得到了这个密码。这完全是在他的代码块上工作。我试着在代码块上做,但在代码块上也不起作用。如果是错的,请帮助我 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> #include <ctype.h> int compute(char c,
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <ctype.h>
int compute(char c, char str[])
{
int r=0;
long len=strlen(str);
int i;
for (i=0; i<len; i++)
{
if (str[i]==c)
{
r++;
}
}
return r;
}
int main()
{
char text[]="C language is a very powerful language that allows programmers to fully control their computers";
int i;
long len=strlen(text);
for (i=0; i<len; i++)
{
char c=text[i];
c= tolower(c);
text[i]=c;
}
bool seen[256];
for (i=0; i<256; i++)
{
seen[i]=false;
}
for (i=0; i<len; i++)
{
char c= text[i];
if (seen[c]==true)
continue;
seen[c]=true;
int ocs=compute(c, text);
if (ocs>0)
{
printf("%c : %d -:",c,ocs);
}
return 0;
}
}
#包括
#包括
#包括
#包括
#包括
int compute(char c,char str[])
{
int r=0;
长len=strlen(str);
int i;
对于(i=0;i将返回0;
置于for
循环之外。关于:
long len=strlen(text);
long len=strlen(text);
for (i=0; i<len; i++)
{
char c=text[i];
c= tolower(c);
text[i]=c;
}
bool seen[256];
for (i=0; i<256; i++)
{
seen[i]=false;
}
函数:strlen()
size_t len = strlen( text );
顺便说一句:注意水平间距的适当使用,以便于人类阅读
关于:
long len=strlen(text);
long len=strlen(text);
for (i=0; i<len; i++)
{
char c=text[i];
c= tolower(c);
text[i]=c;
}
bool seen[256];
for (i=0; i<256; i++)
{
seen[i]=false;
}
当遇到尾随NUL字节时,它将停止循环
关于:
long len=strlen(text);
long len=strlen(text);
for (i=0; i<len; i++)
{
char c=text[i];
c= tolower(c);
text[i]=c;
}
bool seen[256];
for (i=0; i<256; i++)
{
seen[i]=false;
}
请记住,0以外的任何值都被视为“true”
建议的守则如下:
#include <stdio.h> // printf()
#include <ctype.h> // tolower()
#define MAX_CHAR 256
int main( void )
{
char text[]="C language is a very powerful language that allows programmers to fully control their computers";
// set all text to lower case
for ( int i=0; text[i]; i++ )
{
text[i] = (char)tolower( text[i] );
}
// init work array to all 0's
int seen[ MAX_CHAR ] = {0};
// count how many times each char in text
for (size_t i=0; text[i]; i++)
{
seen[ (int)text[i] ]++;
}
// loop through array, displaying each char in text
// and how many times it was seen
for( int i=0; i<MAX_CHAR; i++ )
{
if( seen[i] )
{
printf( "%c : %d -:", i, seen[i] );
}
}
}
干净地编译
执行所需的功能
包含每个代码块的适当注释
使用文本末尾的NUL字节终止某些循环
避免使用“神奇”数字(如256)
不包括未使用的头文件
更正最终循环中“返回”的逻辑错误
现在,拟议的守则:
#include <stdio.h> // printf()
#include <ctype.h> // tolower()
#define MAX_CHAR 256
int main( void )
{
char text[]="C language is a very powerful language that allows programmers to fully control their computers";
// set all text to lower case
for ( int i=0; text[i]; i++ )
{
text[i] = (char)tolower( text[i] );
}
// init work array to all 0's
int seen[ MAX_CHAR ] = {0};
// count how many times each char in text
for (size_t i=0; text[i]; i++)
{
seen[ (int)text[i] ]++;
}
// loop through array, displaying each char in text
// and how many times it was seen
for( int i=0; i<MAX_CHAR; i++ )
{
if( seen[i] )
{
printf( "%c : %d -:", i, seen[i] );
}
}
}
建议重新格式化输出以提高可读性