㈠ c语言编程题 折半法找单词
查找基本算法:
折半算法:
intSearch_Bin(intdata[],intkey,intlength)
{ // 在数组data中折半查找其值等于key的数据元素
int low,high,mid;
low=0; // 置区间初值
high=length-1;
while(low<=high){
mid=(low+high)/2;
if (key==data[mid]) // 找到待查元素
return mid;
else
if (key<data[mid])high=mid-1; // 继续在前半区间进行查找
else
low=mid+1; // 继续在后半区间进行查找
}
return -1;// 顺序表中不存在待查元素
}
㈡ 用C语言编程,如何查找一段文字(有英有汉)中的某些英文单词,并统计其数量
//search.c
#include <stdio.h>
#include <conio.h>
main()
{
FILE*fp;
char key[20],ch;
int i,l,num=0;
if((fp=fopen("d:\\word.dat","w+"))==NULL)
{ printf("can not open file");
getch();
exit(0);
}
printf("输入文章(按#键结束)_\n ");
do{
ch=getchar();
fputc(ch,fp);
}while(ch!='#');
getchar();
printf("\n输入要查询的关键字_ ");
gets(key);
rewind(fp);
ch=fgetc(fp);
while(ch!=EOF)
{
if(ch==key[0])
{
l=strlen(key);
for(i=1;i<l;i++)
{
ch=fgetc(fp);
if(ch!=key[i]) break;
}
num++;
}
ch=fgetc(fp);
}
printf("共计: %d (%s)\n",num,key);
getch();
}
㈢ C语言文件中单词的查找与替换
天下武功,唯坚不破:下面是我完整的代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
unsigned char s[100]={0};
int find(char* s)
{
char word[100] = {0};
char new_word[100] = {0};
int i = 0 ;
int match_sum = 0;
char c;
char* ptr = s;
printf("input the word your want to find out:\n");
while( (c= getchar()) !='\n')
word[i++] = c;
i = 0;
printf("input your new word:\n");
while( (c= getchar()) !='\n')
new_word[i++] = c;
printf("the word your inputed is:\t%s\n",word);
printf("the new word your inputed is:\t%s\n",new_word);
while( *ptr ){
if(*ptr == *word){
char*p = ptr;
char* pp =ptr;
char* p1 = ptr;
char* p2 = word;
char* p3 = new_word;
while( (*p2) && (*p1) ){
if( *p2++ != *p1++)
break;
}
if( (!*p2) && *(--p2) == *(--p1)){
match_sum++;
int len_old = strlen(word);
int len_new = strlen(new_word);
char s_tmp[100]={0};
char* t = s_tmp;
if( len_old < len_new){
char* tt = p1 + len_old-1;
while(*t++ = *tt++);
printf("s_tmp = %s\n",s_tmp);
while( *p3){
*p++ = *p3++;
}
t = s_tmp;
while(*t)
*(ptr+len_new ++) = *t++;
}
else {
char* tt = p1 + len_new;
while(*t++ = *tt++);
printf("s_tmp = %s\n",s_tmp);
while(*p3){
*p++ = *p3++;
}
t = s_tmp;
while(*t )
*(pp+ len_new ++) = *t++;
}
}
}
ptr++;
}
printf("there is %d word matched!\n",match_sum);
printf("your new string :\n%s\n",s);
return match_sum;
}
int input(char *s)
{
printf("input your string:\n");
int i;
unsigned char c;
char *tmp = s;
for(i = 0;i<100 && (c = getchar()) !='\n';i++)
*tmp++ = c;
printf("the string your inputed is:\n%s\n",s);
return 0;
}
int main()
{
input(s);
if( find(s) == 0 )
printf("your word doesn't exist!\n"),exit(-1);
}
㈣ 求c语言大神帮助!!!求 最快 算法啊!!!从上千万的单词里面找出重复的单词,并算每个重复单词重复次数
已通过测试,望采纳。
#include <stdio.h>
#include <string.h>
main()
{ char string[100000],select[15];
int n=0;
char *p,*q;
int i,length,flag;
printf("Input a string:");
gets(string);
printf("Input the string you want to be changed:");
scanf("%s",select);
length=strlen(select);
p=q=string;
for(;*p;p++)
if(*p==select[0])
{
flag=1;
q=p;
for(i=0;i<length;i++)
{ if(*q!=select[i])
{ flag=0;
break;
}
else q++;
}
if(flag==1)
{ n++;
p=q-1;
}
}
printf("The word %s comes %d times in the string.\n",select,n);
}
㈤ C语言 单词恢复
#include<string.h>#include<stdio.h>void main(){int n,n1,sum1,sum2;scanf("%d",&n); for(i=1;i<=n;i++) {int si[ ];gets(si);}for(i=1;i<=n;i++) puts(si);int s[ ];gets(s);for(i=1;i<=n;i++) { if(strlen(si)=strlen(s)) {for(j=1;j<n1=strlen(s);j++)</p><p> sum1+=si[j],sum2+=s[j];</p><p> if(sum1==sum2)</p><p> puts(si);}} }大概思路是先把正确及不正确的单词赋给数组、然后用字符串长度函数对比单词找出与错误单词同长的单词、再把选出的数组内所有元素求和与错误单词数组内所有元素求和比较、相等则输出选出的正确数组、复杂了点、想不出来简单点儿的算法、、花括符没有对齐、方便看些、希望可以帮到你、不明白的可以问下、楼组自己注意下语法问题额、、思路是没错的、
㈥ 写一条查找单词数的C语言
#include <stdio.h>
main()
{
char c[10],d[10];
int i=0,a=0,p=1,k=0;
printf("输入一个单词,以空格结束\n");
while(i<10)
{c[i]=getchar();
i++;
if (c[i-1]==' ')
{printf("请输入文章,以0结束\n");break;}}
show:
while(a<10)
{ d[a]=getchar();
if(d[a]=='0')
break;
if (d[a]==' ')
{ i=0;
while ((i<=9)&&(c[i]==d[i]))
i++;
if(i==a+1)
{ p++;k++;a=0;
goto show;}
else
{k++;
a=0;
if(k<=1000)
goto show;}
}
a++;
}
printf("一共有%d个",p);
}
这里只给出有几个,位置就自己加一下吧,我也刚学
㈦ c语言如何实现在给定文档中查找想要的单词或句子,就好像word中一样,50分求解!
//这里提供一个在字符串中查找字串的函数findstr(),并由运行截图,供参考。
#include<stdio.h>
intfindstr(char*s,char*d){
char*p=s,*q;
inti;
while(*p){
i=p-s;
q=d;
while(*p==*q){p++;q++;}
if(*q=='