c程序设计用sizeof统计p指向的字符串中字符的个数
时间: 2023-09-14 09:09:17 浏览: 62
使用 `sizeof()` 函数统计字符串中字符的个数并不可取,因为 `sizeof()` 函数返回的是变量或类型所占用的内存字节数。对于一个指向字符串的指针变量,它所占用的内存字节数是固定的,与字符串中的字符个数无关。
如果非要使用 `sizeof()` 函数统计字符串中字符的个数,可以使用以下方法:
```c
#include <stdio.h>
int main() {
char *p = "hello, world!";
int len = sizeof(p) / sizeof(char) - 1;
printf("The length of string is %d\n", len);
return 0;
}
```
其中,`sizeof(p)` 返回的是指针变量 `p` 所占用的内存字节数,而 `sizeof(char)` 返回的是 `char` 类型所占用的内存字节数,通常为 1。因此,`sizeof(p) / sizeof(char)` 的结果就是字符串中字符的个数,需要减去字符串结束符的长度 1。
但是,这种方法并不可靠,因为在不同的编译器和平台上,指针变量所占用的内存字节数可能不同,甚至可能与 `char` 类型的内存字节数相等,导致计算结果错误。因此,不建议使用 `sizeof()` 函数来统计字符串中字符的个数。
相关问题
用C语言统计字符串中字母数字空格和其他字符的个数
可以使用C语言中的字符处理函数,如isdigit()、isalpha()、isspace()等来判断字符的类型,然后统计每种类型的字符出现的次数即可。具体实现如下:
```c
#include <stdio.h>
#include <ctype.h>
int main() {
char str[100];
int i, alpha_count = 0, digit_count = 0, space_count = 0, other_count = 0;
printf("请输入一个字符串:");
fgets(str, sizeof(str), stdin);
for (i = 0; str[i] != '\0'; i++) {
if (isalpha(str[i])) {
alpha_count++;
} else if (isdigit(str[i])) {
digit_count++;
} else if (isspace(str[i])) {
space_count++;
} else {
other_count++;
}
}
printf("字母个数:%d\n", alpha_count);
printf("数字个数:%d\n", digit_count);
printf("空格个数:%d\n", space_count);
printf("其他字符个数:%d\n", other_count);
return 0;
}
```
这里使用了fgets()函数来读取输入的字符串,因为gets()函数已经被淘汰了。
C语言统计字符串中字母、数字、空格和其他字符的个数
可以使用C语言中的字符数组和循环语句来统计字符串中字母、数字、空格和其他字符的个数。以下是示例代码:
```
#include <stdio.h>
#include <ctype.h>
int main() {
char str[100]; // 定义字符数组
int i, letters, digits, spaces, others;
letters = digits = spaces = others = 0; // 初始化计数器
printf("请输入一个字符串:");
fgets(str, sizeof(str), stdin); // 读取输入的字符串
for (i = 0; str[i] != '\0'; i++) { // 遍历字符串中的每个字符
if (isalpha(str[i])) { // 判断是否为字母
letters++;
} else if (isdigit(str[i])) { // 判断是否为数字
digits++;
} else if (isspace(str[i])) { // 判断是否为空格
spaces++;
} else { // 其他字符
others++;
}
}
printf("字母个数:%d\n", letters);
printf("数字个数:%d\n", digits);
printf("空格个数:%d\n", spaces);
printf("其他字符个数:%d\n", others);
return 0;
}
```
以上代码中,使用了`fgets()`函数读取输入的字符串,它可以读取一行字符串,包括空格,并将其存储到字符数组`str`中。然后使用循环语句遍历字符串中的每个字符,使用`isalpha()`、`isdigit()`和`isspace()`函数判断每个字符的类型,并分别累加到对应的计数器中,最后输出各种类型字符的个数。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)