【发布时间】:2021-01-18 18:07:46
【问题描述】:
char c[50];
scanf("%s",c);
int counter;
for(int i=0;i<strlen(c);i++){
for(int j=0;j<=9;j++){
if(j==c[i]) // this line not comparing
counter+=1;
}
比较整数(j)和数组c中的数字,调试时不比较
【问题讨论】:
char c[50];
scanf("%s",c);
int counter;
for(int i=0;i<strlen(c);i++){
for(int j=0;j<=9;j++){
if(j==c[i]) // this line not comparing
counter+=1;
}
比较整数(j)和数组c中的数字,调试时不比较
【问题讨论】:
您需要比较代表数字的字符代码。例如可以通过以下方式完成
if ( j + '0' == c[i] )
注意你的代码sn-p中的变量count没有初始化。
int counter;
你需要初始化它
int counter = 0;
@pmg 指出的另一种编写循环的方法如下
for( char j = '0'; j <= '9'; j++ ){
您也可以使用标准 C 函数 isdigit 代替内部循环。
if ( isdigit( ( unsigned char )c[i] ) ) ++count;
【讨论】: