我刚入坑的小白,看了点书写出来,运行没有错但是感觉怪怪的
#include<stdio.h>#include<string.h>
int main()
{
char string;
int i,num=0,word=0,letter=0;
char c;
gets(string);
for(i=0;(c=string)!='\0';i++)
{if(c>='a'&&c<='z'||c>='A'&&c<='Z')
{letter++;
}
}
for(i=0;(c=string)!='\0';i++)
{if(c==' ') word=0;
else if(word==0)
{word=1;
num++;
}
}
printf("There are %d words and %d letters",num,letter);
return 0;
} 哪里感觉怪怪的呢? 你的缩进怪 代码没有问题,规范的代码应该是这样的:
#include <stdio.h>
#include <string.h>
int main()
{
char string;
int i, num = 0, word = 0, letter = 0;
char c;
gets(string);
for (i = 0; (c = string) != '\0'; i++)
{
if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z')
{
letter++;
}
}
for (i = 0; (c = string) != '\0'; i++)
{
if (c == ' ')
word = 0;
else if (word == 0)
{
word = 1;
num++;
}
}
printf("There are %d words and %d letters", num, letter);
return 0;
} zltzlt 发表于 2020-2-18 15:13
代码没有问题,规范的代码应该是这样的:
可以套用俩个一样的循环?
页:
[1]