编写一个统计有多少个字母的程序,我遍了一个,但运行不了啊?哪里错了啊?
#include<stdio.h>
int found_letter(int j);
int main()
{
int i, j;
char a[81];
printf("Please input sentance:\n");
for(i=0;i<=81;++i)
scanf("%c",a[i]);
printf("%d",found_letter(j));
return 0;
}
int found_letter(int j)
{
int i;
char a[81];
j=0;
if(a[i]>='A'&&a[i]<='z')
++j;
return j;
}
----------------解决方案--------------------------------------------------------
编写一个统计有多少个字母的程序,我遍了一个,但运行不了啊?哪里错了啊?
#include<stdio.h>
int found_letter(int j);
int main()
{
int i, j;
char a[81];
printf("Please input sentance:\n");
for(i=0;i<=81;++i)//。。。
scanf("%c",a[i]);//是这么写么?
printf("%d",found_letter(j)); //j都没初始化
return 0;
}
int found_letter(int j)
{
int i; //什么意思?
char a[81]; //这又是什么?
j=0; //。。。。
if(a[i]>='A'&&a[i]<='z') //不知道说什么好了。。
++j; //
return j; //
}
建议楼主需要好好补下基础,至少保证思路正确。
[此贴子已经被作者于2006-11-28 16:41:21编辑过]
----------------解决方案--------------------------------------------------------
补充一点,定义char a[81].能用的只能到a[80].
函数内定义的局部变量和调用函数的函数所用到的变量是不 同的,所以main()的a[],和found_letter()中的a[]是不同的.
再者这个子函数乱套了.,再改改吧.
----------------解决方案--------------------------------------------------------
for(i=0;i<=81;++i)看看这样写法正确吗,for(i=0;i<81;i++)
----------------解决方案--------------------------------------------------------
哦,我再自己研究一下吧!~~晕啊!~~全错了
----------------解决方案--------------------------------------------------------
看不懂!
----------------解决方案--------------------------------------------------------
慢慢来,不用急.
----------------解决方案--------------------------------------------------------
改为了这一个,觉得应该清楚了不少,但还是错的!~~~晕啊!!~~~帮忙啊!~~~
#include <stdio.h>
void main()
{ int i=0,j=0,num=0,num2=0;
char str[100],c;
gets(str);
do
{ while((c=str[i])==' ')
i++;
if(c!='\0')
num++;
while((c=str[i])!=' '&&c!='\0')
i++;
} while(c!='\0');
do
{while(c=str[j]>='A'&&(c=str[j])<='z')
j++;
if(c!=' '&&c!='\0')
num2++;
while((c=str[j])!=' '&&c!='\0')
j++;
}while(c!=' ');
printf("number of letters:%d\n",num2);
printf("number of word:%d\n",num);
}
----------------解决方案--------------------------------------------------------
居然晚熄灯半个小时 睡觉去了
#include<stdio.h>
void main()
{
int i=0,word=0,num=0;
char sentence[100];
bool flag=false;
printf(\"input the sentence:\");
gets(sentence);
while(sentence[i]!='\0')
{
if((sentence[i]>='a'&&sentence[i]<='z')||(sentence[i]>='A'&&sentence[i]<='Z')) num++;
if(sentence[i]==' '||sentence[i]==','||sentence[i]=='.'||sentence[i]=='\n')
flag=false;
if(!flag)
{
flag=true;
word++;
}
i++;
}
printf(\"the number of words is: %d\",word);
printf(\"\nthe number of letters is: %d\n\",num);
}
----------------解决方案--------------------------------------------------------
晚上网速好快 玩会网游一点都不卡 呵呵 睡觉前再完善下刚刚写的
void main()
{
int i=0,word=0,num=0;
char sentence[100];
bool flag=false;
printf(\"input the sentence:\");
gets(sentence);
while(sentence[i]!='\0')
{
while(sentence[i]==' '||sentence[i]==','||sentence[i]=='.')//去掉空格等间隔字符
{
flag=false;
i++;
if(sentence[i]=='\0') flag=true;
}
if((sentence[i]>='a'&&sentence[i]<='z')||(sentence[i]>='A'&&sentence[i]<='Z')) num++;//统计字母
if(!flag)//统计单词
{
flag=true;
word++;
}
i++;
}
printf(\"the number of words is: %d\",word);
printf(\"\nthe number of letters is: %d\n\",num);
}
----------------解决方案--------------------------------------------------------