|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
题目要求是设计一个可以统计中文字符以及中英文混合字符的程序c语言程序。 要求使用fgets 函数读取用户输入的字符串(英文),并用指针法来计算字符串的字符个数且不能使用strlen 函数
- #include <stdio.h>
- #include <stdlib.h>
- #define MAX 1024
- int main()
- {
- char str[MAX];
- char *target = str;
- char ch;
- int length = 0;
- printf("请输入一个字符串:");
- fgets(str, MAX, stdin);
- while (1)
- {
- ch = *target++;
- if (ch == '\0')
- {
- break;
- }
- if ((int)ch < 0)
- {
- target += 2;
- }
- length++;
- }
- printf("您总共输入了 %d 个字符!\n", length - 1);
- system("pause");
- return 0;
- }
复制代码
但是小甲鱼一个中文字符是占了4个字节,我一个中文字符是占了3个字节,要怎么样更改小甲鱼的程序才能实现小甲鱼的程序结果
- #include <stdio.h>
- #include <stdlib.h>
- #define MAX 1024
- int main()
- {
- char str[MAX];
- char *target = str;
- char ch;
- int length = 0;
- printf("请输入一个字符串:");
- fgets(str, MAX, stdin);
- while (1)
- {
- ch = *target++;
- if (ch == '\0')
- {
- break;
- }
- if ((int)ch < 0)
- {
- target += 1;
- }
- length++;
- }
- printf("您总共输入了 %d 个字符!\n", length - 1);
- system("pause");
- return 0;
- }
复制代码
又放错了,我是S*
|
-
-
|