㈠ 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=='