int i,len, num=0; len=strlen(string); for(i=0;i<len; ) { while(string[i]==' ') i++; if(i<len) num++; while(string[i]!=' ' && i<len)i++; }
时间: 2023-12-06 15:03:46 浏览: 34
这段代码的作用是计算字符串中单词的数量。具体来说,它通过遍历字符串中的每个字符,跳过空格并计算单词的数量。下面是代码的解释:
```c
int i, len, num = 0; // 定义变量i、len和num,其中i是循环计数器,len是字符串的长度,num是单词的数量
len = strlen(string); // 获取字符串的长度
for (i = 0; i < len;) { // 循环遍历字符串中的每个字符
while (string[i] == ' ') i++; // 跳过空格
if (i < len) num++; // 如果还没有到字符串的末尾,则单词数量加1
while (string[i] != ' ' && i < len) i++; // 继续循环,直到找到下一个空格或到达字符串的末尾
}
```
相关问题
#include <stdio.h> #include <string.h> #define N 1000 void get(int *array) { char str[N] = {'\0'}; int len = 0; scanf("%s", str); len = strlen(str); for (int i = 0;i < len;i++) { array[i] = str[len - i - 1] - '0'; } array[len] = -1; } void big_multi(int *num1, int *num2, int *result) { int num1_index = 0; int num2_index = 0; int result_index = 0; int tmp_carry = 0; int tmp_result = 0; for (num1_index = 0; num1[num1_index] != -1; num1_index++) { for (num2_index = 0; num2[num2_index] != -1; num2_index++) { tmp_result = num1[num1_index] * num2[num2_index] + result[num1_index + num2_index] + tmp_carry; result[num1_index + num2_index] = tmp_result % 10; tmp_carry = tmp_result / 10; } result_index = num1_index + num2_index - 1; if (tmp_carry) { result[result_index++] = tmp_carry; } } result[result_index++] = -1; } void print(int *array) { int i = 0; int len = 0; for (i = 0; array[i] != -1; i++); len = i; printf("Product : "); for (i = len - 1; i >= 0; i--) { printf("%d", array[i]); } if (len == 0) { printf("0"); } printf("\n"); } int main(int argc, char **argv) { int num1[N] = {0}; int num2[N] = {0}; int result[2 * N] = {0}; //将数用数组来存,并从低位向高位存. get(num1); get(num2); //执行相乘操作 big_multi(num1, num2, result); print(result); return 0; }对该段代码进行完善
以下是对代码进行的一些完善和优化:
1. 在函数 `get()` 中,可以增加对输入字符串长度的判断,如果长度超过了数组的长度,则提示用户重新输入;
2. 在函数 `get()` 中,可以增加对负数和小数的判断,如果输入的字符串不符合要求,则提示用户重新输入;
3. 在函数 `big_multi()` 中,可以增加对数组越界的判断,如果结果数组长度超过了数组的长度,则提示用户重新输入;
4. 在函数 `print()` 中,可以将变量 `len` 的计算放到函数 `get()` 中,避免重复计算;
5. 在函数 `print()` 中,可以增加对结果数组中的所有数字都为零的情况的判断,如果结果数组中的所有数字都为零,则直接输出 "Product : 0"。
以下是完善后的代码:
```c
#include <stdio.h>
#include <string.h>
#define N 1000
void get(int *array) {
char str[N] = {'\0'};
int len = 0;
do {
printf("Please enter a non-negative integer: ");
scanf("%s", str);
len = strlen(str);
if (len > N) {
printf("The length of the input integer exceeds the maximum length of the array!\n");
} else {
int is_valid = 1;
for (int i = 0; i < len; i++) {
if (str[i] < '0' || str[i] > '9') {
printf("The input integer contains non-numeric characters!\n");
is_valid = 0;
break;
}
}
if (is_valid) {
for (int i = 0; i < len; i++) {
array[i] = str[len - i - 1] - '0';
}
array[len] = -1;
return;
}
}
} while (1);
}
void big_multi(int *num1, int *num2, int *result) {
int num1_index = 0;
int num2_index = 0;
int result_index = 0;
int tmp_carry = 0;
int tmp_result = 0;
for (num1_index = 0; num1[num1_index] != -1; num1_index++) {
for (num2_index = 0; num2[num2_index] != -1; num2_index++) {
if (num1_index + num2_index >= N) {
printf("The length of the result array exceeds the maximum length of the array!\n");
return;
}
tmp_result = num1[num1_index] * num2[num2_index] + result[num1_index + num2_index] + tmp_carry;
result[num1_index + num2_index] = tmp_result % 10;
tmp_carry = tmp_result / 10;
}
result_index = num1_index + num2_index - 1;
if (tmp_carry) {
if (result_index >= N) {
printf("The length of the result array exceeds the maximum length of the array!\n");
return;
}
result[result_index++] = tmp_carry;
}
}
result[result_index++] = -1;
}
void print(int *array) {
int i = 0;
int len = 0;
for (i = 0; array[i] != -1; i++);
len = i;
printf("Product : ");
if (len == 0 || (len == 1 && array[0] == 0)) {
printf("0");
} else {
for (i = len - 1; i >= 0; i--) {
printf("%d", array[i]);
}
}
printf("\n");
}
int main(int argc, char **argv) {
int num1[N] = {0};
int num2[N] = {0};
int result[2 * N] = {0}; //将数用数组来存,并从低位向高位存.
get(num1);
get(num2);
big_multi(num1, num2, result);
print(result);
return 0;
}
```
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include<string.h> int main() { char a[100]; gets(a); int t; int len = strlen(a); int count[27] = { 0 }; char s[27] = { 0 }; int num[27] = { 0 }; for (int i = 1; i <= len; i++) { if (a[i] > 'a' && a[i] < 'z') { count[a[i] - 'a' + 1]++; } if (a[i] > 'A' && a[i] < 'Z') { count[a[i] - 'A' + 1]++; } } for (int j = 1, i = 1; i <= len; j++, i++) { num[j] = count[i]; } for (int j = 1; j < 26; j++) { for (int i = 1; i < 26 - i; i++) { if (count[i] < count[i + 1]) { t = count[i]; count[i] = count[i + 1]; count[i + 1] = t; } } } int max = count[1]; char c; int flag = 0; while (max >= 0) { for (int i = 1; i <= 26; i++) { if (num[i] == max) { c = i + 'A'; if (flag == 0) { printf("%c-%d", c, max); flag = 1; } else printf(" %c-%d", c, max); } } max--; } return 0; }这个代码怎么修改
这个代码有几个问题需要修改:
1. 使用 `gets()` 函数会存在安全隐患,应该改用 `fgets()` 函数。
2. 在 `for` 循环中,数组下标应该从0开始,而不是从1开始。
3. 最后的输出格式不够清晰,应该加上换行符。
下面是修改后的代码:
```c
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
int main() {
char a[100];
fgets(a, 100, stdin);
int len = strlen(a);
int count[26] = { 0 };
int num[26] = { 0 };
for (int i = 0; i < len; i++) {
if (a[i] >= 'a' && a[i] <= 'z') {
count[a[i] - 'a']++;
}
if (a[i] >= 'A' && a[i] <= 'Z') {
count[a[i] - 'A']++;
}
}
for (int j = 0, i = 0; i < 26; j++, i++) {
num[j] = count[i];
}
for (int j = 0; j < 26; j++) {
for (int i = 0; i < 26 - i - 1; i++) {
if (count[i] < count[i + 1]) {
int t = count[i];
count[i] = count[i + 1];
count[i + 1] = t;
}
}
}
int max = count[0];
int flag = 0;
while (max >= 0) {
for (int i = 0; i < 26; i++) {
if (num[i] == max) {
char c = i + 'A';
if (flag == 0) {
printf("%c-%d", c, max);
flag = 1;
}
else {
printf(" %c-%d", c, max);
}
}
}
max--;
}
printf("\n");
return 0;
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![](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)