输入一段英文及一个单词,写C语言程序找出这个单词出现的次数,if后面不对,应该怎么写

作者&投稿:汗国 (若有异议请与网页底部的电邮联系)
用c语言编写输入一段英文,1 统计这段英文的单词数2 输入特定单词,统计本单词在上述英文中出现的次数~

#include #include #include
char * getword(FILE *fp);
int main()
{
puts("输入一段英文:");
FILE *fp;
if((fp=fopen("en.txt","w"))==NULL) { puts(" can't write ! ");exit(1);}
char words[2000],word[50],search[50];
gets(words);
fputs(words,fp);
fclose(fp);
if((fp=fopen("en.txt","r"))==NULL) { puts(" can't write ! ");exit(1);}
int num=0;
while(1)
{
strcpy(word,getword(fp));
if(strcmp(word,"")==0) break;
else num++;
}
printf("此段英文的单词个数为:%d
",num);
printf("请输入你要查找的单词
");
gets(search);
num=0;
rewind(fp);
while(1)
{
strcpy(word,getword(fp));
if(strcmp(word,"")==0) break;
else if(strcmp(word,search)==0) num++;
}
printf("此段英文中该单词 %s 个数为:%d
",search,num);
fclose(fp);
remove("en.txt");
return 0;
}

char * getword(FILE *fp)
{

char f,word[50];
do{ int flag=0,i=0;
while(flag==0)
{
f=fgetc(fp);
if((f>='a'&&f='A'&&f<='Z')) word[i++] =f;
else flag=1;
}
word[i]='\0';
if(f==EOF) break;;
}while(word[0]=='\0');
return word;
}


#include
#include
#include
#include
using namespace std;
//定义存储单词和出现次数的结构体
typedef struct{
string word;
int num;
}count;
int main()
{
vector v; //定义count类型的向量,动态存储count变量
count tempstr; //临时存储count变量
tempstr.num=0; //num初始化为0
ifstream in("english.txt"); //打开文件
string temp; //临时变量,存储文件的一行信息
string str; //临时变量,存储单个单词
int count=0; //记录单词字符个数
int j=0;

//按行读取文件,对每行信息截取单词并计数
while(getline(in,temp))
{
for(int i=0;i<temp.length ();i++)
{
if((temp[i]>='a'&&temp[i]='A'&&temp[i]<='Z'))
count++; //如果是英文字符,则计数加1
else if(count) //单词遇非英文字符时,计数终止,截取单词
{
str=temp.substr (i-count,count); //取子串(截取单词)
if(v.size ()) //若向量的长度不为0,则将单词与已有单词比较
{
for(j=0;j<v.size ();j++)
if(str.compare(v[j].word )==0)
{
v[j].num ++; //单词相同,则将相应单词的数目加1
count=0; //计数变量重新赋值为0,以便记录新的单词
break;
}
} //end if
if(j>=v.size ()) //单词第一次出现,将其添加至向量中
{
tempstr.word = str;
tempstr.num =1;
v.push_back (tempstr);
count = 0; //单词添加完毕,计数变量归0,记录新单词
} //end if
} //end elseif
} //end for
} //end while
//打印单词及出现次数
for(int i=0;i<v.size ();i++)
cout<<"the word is:"<<v[i].word<<", the times is:"<<v[i].num<<endl;
return 0;
}

#include<stdio.h>
#include<string.h>
int main()
{
int i,num=0;
char a[200],b[20];
gets(a);

gets(b);
for(i=0;a[i]!='\0';i++) //检查到串结束即可
{
if( strncmp(b,&a[i],strlen(b))==0) //检查b的长度个数
{
num+=1;
}
}
printf("The word '%s' appears %d time in the text.
",b,num); //字符串输出用%s
return 0;
}


不该用strcmp。
该函数的描述是:
This function starts comparing the first character of each string. If they are equal to each other, it continues with the following pairs until the characters differ or until a terminating null-character is reached.
使用strncmp来比较。


输入一段英文,再输入一个单词,查找该单词在该段英文中出现的次数及位置...
int i,add=0,position[20]={0};char str[]="are you a dog you are a pig you are everything";for (i=0;str[i]!='0';i++){ if (str[i]=='y' && str[i+1]=='o' && str[i+2]=='u' ){ printf("第%d次,下标%d\\n",add+1,i);} } } 请采纳。

求一片英语小作文,120个单词左右
Gradually adapt to a university study. 步入一个新的阶段,新的环境,这其中由不适应到适应。Entered a new stage and new environment, which is not suited to meet them by. 记得刚上大学时,上课总是不能专心听讲,因为还不习惯,不适应新老师的缘故吧!I recall that university, the school...

python编程:任意输入一段英文,统计出最长单词及长度。用replace、split...
text = input("输入:")words = text.split()words = {i:len(i) for i in words} max_length_word = max(words.items(),key=lambda x:x[1])print(max_length_word)测试如下:输入:Today I consider myself the luckiest man on the face of the earth.输出:('consider', 8)

输入一行英文句子,按空格分出若干个单词,每一行只输出一个单词。 c...
include <stdio.h>int main(){ char string[81]; int i,j,num=0,word=0; char b,c; printf("Please input a sentence,Split by space!\\n"); gets(string); word = 1; for (i=0;(c=string[i])!='\\0';i++) { if(c==' ') \/\/\/遇到空格,将...

c语言编程,要求输入一段英文,输出最长的字母,我编了个程序,不知道哪错...
point=0; \/\/第一个单词的开始位置赋给point for(int j=1;j<k+1;j++) \/\/循环找出最长的单词 { if(max<(a[j]-a[j-1]-1)){ max=a[j]-a[j-1]-1;point=a[j-1]+1;} } } printf("the largest word is:"); \/\/输出最长的单词 for(i=0;i<max;i++){ printf("%c",...

从键盘输入一段英文,找出长度最长和最短的单词,以及出现次数最多的字符...
很简单,c中本来就有字符串长度计算函数,至于出现次数最多的字符,英文不过26个,可以用个笨方法,用26个变量分别计数各自字母出现的个数,加个比较不就行了

要求输入“粘贴的一段英文文章”将每个单词都分个且不重复输出!_百度...
int n=0,k=0,i,flag=1;\/\/s1='\\0';\/\/gets(ss); \/\/加上这行可以输入(粘贴)字符串 temp=ss;do { chlast=*temp++;\/\/从第一个字母开始保存于last中,遇到非字母则得到一个单词 if(isalpha(chlast)) { last[k++]=chlast; last[k]='\\0'; } else { for(i=0;i<=n;i++)if(...

.输入一行字符,统计其中有多少个单词,单词之间用空格分隔。
scanf("%c",&string[i]);这样写,你输入的字符串必须是100个字符啊 应该用gets(string)include<stdio.h>int main(){char string[100]; int i,c,num=0; char b;printf("请输入一段英文,每个单词空格隔开:\\n"); gets(string);if(string[0]!=' ') num=1; for (i=1;string...

如何用c#编写一段英文,统计并输出单词个数
using System;using System.Text;namespace ConsoleApplication1 { class Program { static void Main(string[] args){ string b;Console.WriteLine("请输入一段英文");b = Convert.ToString(Console.ReadLine());Console.WriteLine("单词个数为:{0}",b.Split(' ').Length);} } } ...

用C或C++编写:输入一段英文句子,输出其中最长的单词
include <iostream>#include <string>#include <algorithm> #include <sstream>int main(){ std::string t, word, long_word; getline(std::cin, t); std::istringstream iss(t); while (iss >> word) { std::string result; std::remove_copy_if(word.begin(), wo...

五指山市15137892559: 输入一段英文及一个单词,写C语言程序找出这个单词出现的次数,if后面不对,应该怎么写 -
端妻永瑞: #include<stdio.h>#include<string.h> int main() { int i,num=0; char a[200],b[20]; gets(a); gets(b); for(i=0;a[i]!='\0';i++) //检查到串结束即可 { if( strncmp(b,&a[i],strlen(b))==0) //检查b的长度个数 { num+=1; } } printf("The word '%s' appears %d time in the text.\n",b,num); //字符串输出用%s return 0; }

五指山市15137892559: 1.输入一段英文,将其中最长的一个单词打印出来.求c语言程序 -
端妻永瑞: //输入一段英文,将其中最长的一个单词打印出来.求c语言程序#include<stdio.h> //这里的单词之间以空格分开,否则不能知道那个是单词啊.#include<string.h> int main() { char ch[200]; gets(ch); char str[200]; //用以存放最大的单词,假设就是...

五指山市15137892559: c语言编程:做出输入英文单词,就能输出此单词的字母个数的程序 -
端妻永瑞: #include <stdio.h> typedef char* string;int main() {string str;int length; printf("请输入单词:\n");scanf("%s",str);length = strlen(str);printf("单词 %s 的长度为:%d",str,length); return 0; }

五指山市15137892559: 用c语言如何编写一段英文单词 -
端妻永瑞: 存储成字符数组 然后用printf或者puts输出 比如char s[100] = "English"; printf("%s",s); puts(s);都可以.

五指山市15137892559: 用C语言怎么实现输入一段英文文章.谢谢了! -
端妻永瑞: #include<stdio.h>#define Tmpfile"tmp.txt" main() {FILE*fp; char str; fp=fopen(Tmpfile,"w"); printf("请在下面写你要输入的文章\n"); while((str=fgetc(stdin))!='\t') { fprintf(fp,"%c",str);}; fclose(fp); }/* 我设置的是Tab再按回车键文件输入结束,所以在未输入完之前请不要按Tab键,否则按过Tab后输入的都将不被保存*/

五指山市15137892559: 求助,使用C语言编写一个为英文文章每个单词标序号并输出结果的程序. -
端妻永瑞: 先提供思路,你可以自己尝试先写一下代码.1. 读取文件内容;2. 读取出的内容写入buff;3. 遍历全部内容,根据非数字和非字母的字符进行拆解,将拆解出的单词存入链表;3.1 遍历拆解时使用两个指针head 和tail,一开始都指向buff的首地址;...

五指山市15137892559: 用c语言编程,要求从键盘读入一个由单词和空格组成的英文长句,分解其中单词,并按照字典顺序排列输出 -
端妻永瑞: 用二维数组吧,比如定义a[10][10],开始读到a[0][10]里,遇到空格就换到a[1][10],依次类推.之后就是排序了,随你用什么方法.

五指山市15137892559: c语言编写一个程序,在一维数组里输入一句英文,统计该句子里出现的单词个数 -
端妻永瑞: 声明一个较大的char型数组,待输入完成后依次检查数组元素,除第一个单词可能需要个别处理外,其余以空格后跟字母或其它字符后跟字母的就认为是一个单词,累加计数即可.代码如下:int main(int argc,char *argv[]){ char text[300];//定义...

五指山市15137892559: 先输入一串字符,再输入一个单词,求单词出现的次数 C语言程序 -
端妻永瑞: #include "Stdio.h" #include "Conio.h"main() { int i,n=0; char a[20],c,w; gets(a); printf("in put the word:\n"); scanf("%c",&w);for(i=0;(c=a[i])!='\0';i++){if(c==w)n+=1;}printf("%d",n);getch(); }望采纳

五指山市15137892559: 用C语言编写一段程序 -
端妻永瑞: //=== #include //--- void count(char a[100],int b[27],int c[27],int i) { int j; for(j=0;j='a'&&a[j]<='z') //统计各小写字母的个数 { b[a[j]-'a']++; } else if(a[j]>='A'&&a[j]<='Z') //统计各大写字母的个数 { c[a[j]-'A']++; } else //统计其他字符的个数 c[26]++; } } //-- void ...

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网