c语言指针简单问题
#include<stdio.h>#include<stdlib.h>
void sort(char *string,int n)
{
int numA = 0, numa = 0, num1 = 0, num_ = 0;
for (int i = 0;*(string+i) = '\0'; i++)
{
if ('0' <= *(string + i) <= '9')
{
num1++;
}
else if ('A' <= *(string + i) <= 'Z')
{
numA++;
}
else if ('a' <= *(string + i) <= 'z')
{
numa++;
}
else
{
num_++;
}
}
printf("number:%d,majuscule:%d,lowercase:%d,others:%d", num1, numA, numa, num_);
}
int main()
{
char* string;
int n;
printf("Please input the number of the string:");
scanf_s("%d", &n);
getchar();
string = (char*)malloc( (n+1) * sizeof(string));
printf("Please input the string:");
gets(string);
sort(string, n);
free(string);
return 0;
}![屏幕截图(112).png](data/attachment/forum/202302/13/205840k8y0ydaon84rrb04.png)![屏幕截图(112).png](data/attachment/forum/202302/13/205842u9wrj5jnf2nef95n.png)
这里用来分别计数字符串中大小写字母,数字,其他符号个数,但是输出为0,为什么? 没看完,不过 '0' <= (string + i) <= '9' 这写法就不对吧,编程不是数学公式,要写完整的:'0' <= (string + i)&&(string + i) <= '9'
另外发代码最好用上编辑器,不然这样一坨没几个人愿意给你看。 cxxjackie 发表于 2023-2-13 22:47
没看完,不过 '0'
好的,应该就是这的问题了{:4_93:}谢谢gg、 用 ```即可包住代码高亮,例如:
论坛新版本markdown编辑器上线啦~
https://bbs.tampermonkey.net.cn/thread-1995-1-1.html
(出处: 油猴中文网)
页:
[1]