㈠ c語言詞語搜索
/*本程序已在tc2.0上運行成功,根據提示輸入txt文件名(帶擴展名)和要查的單詞*/
#include<stdio.h>
#include<stdlib.h>
void main()
{
FILE *fp;
char fname[10],ch,word[100],wp[20],pre;
int i,result=0;
int x,n;
printf("Input filename:");
scanf("%s",fname);
if((fp=fopen(fname,"r"))==NULL)
{
printf("can't open file!\n");
exit(0);
}
getchar();
printf("Input the word:");
scanf("%s",wp);
getchar();
x=1;
n=0;
pre=' ';
while(!feof(fp))
{
ch=getc(fp);
if(ch=='\n')
x++;
if(pre=='\n')
n=0;
if(pre==' '&&ch!=' ')
n++;
if(pre==' ')
{
i=0;
while(ch==wp[i]&&wp[i]!='\0')
{
ch=getc(fp);
i++;
}
if(wp[i]=='\0'&&ch==' ')
{
printf("%s:row=%d,number=%d\n",wp,x,n);
result=1;
break;
}
}
pre=ch;
}
if(result==0)
printf("Can't fine the word!\n");
fclose(fp);
getch();
}
㈡ c語言怎麼查詢一短語中的某個單詞
用strstr這個函數
㈢ 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語言怎麼提取字元串單詞
我是提取一段文字,也在網上找了好久,沒找到合適的,只好自己編寫了一個函數,你看看對你有沒有幫助。
// 匹配字元串 m_Yuantext 是被提取的文件, m_guanjian 是索引關鍵字
int CMy500Dlg::Guanjian(CString m_Yuantext, CString m_guanjian)
{
// TODO: 在此處添加實現代碼.
int ddr = 0;//
for (int dd = m_Yuantext.GetLength(); dd; dd--)
{
ddr = m_Yuantext.Find(m_guanjian.Mid(0), ddr);//查找第一個關鍵字
if (ddr != -1)//找到第一個關鍵字
{
int addr = m_guanjian.GetLength();
int addr2 = addr;
addr--;//去掉已經匹配的第一個關鍵字
for (; addr; addr--)//如果有兩個或者兩個以上的關鍵字,就依次匹配 addr 個關鍵字
{
if (m_Yuantext.Mid(ddr + (addr2 - addr)) != m_guanjian.Mid(addr2 - addr))//依次匹配關鍵字
{
break;//匹配失敗跳出本次循環
}
}
return ddr;//返回第一個關鍵字的索引
}
else
{
break;//匹配失敗跳出本次循環
}
}
return -1;//返回匹配失敗
}
㈤ 求大神解答一道C語言題:用二分法在順序排列的字典中查找單詞
#include <stdio.h>
#include <string.h>
void binary_search(char key[100], char a[100][100], int n) /*自定義函數binary_search*/
{
int low, high, mid, count = 0, count1 = 0;
low = 0;
high = n - 1;
while (low < high) /*當查找范圍不為0時執行循環體語句*/
{
count++; /*count記錄查找次數*/
mid = (low + high) / 2; /*求出中間位置*/
if (strcmp(key,a[mid])<0) /*當key小於中間值*/
high = mid - 1; /*確定左子表范圍*/
else if (strcmp(key,a[mid])>0) /*當key大於中間值*/
low = mid + 1; /*確定右子表范圍*/
else if (strcmp(key,a[mid])==0) /*當key等於中間值證明查找成功*/
{
printf("success!\nsearch %d times!a[%d]=%d", count, mid, key);
/*輸出查找次數及所查找元素在數組中的位置*/
count1++; /*count1記錄查找成功次數*/
break;
}
}
if (count1 == 0) /*判斷是否查找失敗*/
printf("no found!"); /*查找失敗輸出no found*/
}
main()
{
char key[100],a[100][100];
int i,n;
printf("please input the length of array:\n");
scanf("%d", &n); /*輸入單詞個數*/
printf("please input the element:\n");
for (i = 0; i < n; i++)
scanf("%s", a[i]); /*輸入有序單詞到數組a中*/
printf("please input the number which do you want to search:\n");
scanf("%s", key); /*輸入要查找的關鍵字*/
binary_search(key, a, n); /*調用自定義函數*/
}
㈥ 寫一條查找單詞數的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語言寫:檢索英文單詞程序
#include<stdio.h>
#include<string.h>
#define MAX_size 1000
int flag=1,degree=0;
void Index(char str[],char word[],int position[])
{
int i,len_str,len_word,pos_str,pos_word,k=0,word_number=0;//word_number代表短文中單詞的個數
len_word=strlen(word);
len_str=strlen(str);
for(i=0;i<len_str;)
{
while(str[i]==' ')
i++;
word_number++; //單詞個數加一
for(pos_str=i,pos_word=0;pos_str<len_str && pos_word<len_word;pos_str++,pos_word++)
{
if(str[pos_str]!=word[pos_word])
break;
}
if(pos_word==len_word && (str[pos_str]=='\0'|| str[pos_str]==' ' )) //表明找到相等的單詞
{
position[k++]=word_number;
degree++; //相等的次數加1
flag=0;
}
else
{
while(str[pos_str]!=' ' && pos_str<len_str)
pos_str++;
}
i=pos_str;
}
}
void main()
{
char str[MAX_size],word[20];
int position[100],i;
printf("請輸入一行英文短文: \n");
gets(str);
printf("請輸入要檢索的單詞: \n");
gets(word);
Index(str,word,position);
if(flag)
printf("您輸入的單詞不在短文中。\n");
else
{
printf("您輸入的單詞在短文中,它共出現 %-d 次\n",degree);
printf("出現的位置為: \n");
for(i=0;i<degree;i++)
printf("第%-2d個單詞\n",position[i]);
}
}//未考慮逗號,若要考慮則加幾個判斷條件即可
㈧ 查詢單詞 C語言如何實現
你一個一個字元的來判斷.你可以設定一個變數來表示一個單詞的開始和結束.flag=0時是沒有單詞,當flag=1時一個單詞的開始.那flag=1即單詞開始時掃描的是字元繼續下個字元的,如果是非字元flag=0;到下個字元開始是flag=1;那一個單詞的開始到結束就是一個單詞了,你可以設定一個變數來記錄單詞的個數.
㈨ c語言編程在一個一直的字元串中查找最長的單詞,
給出代碼:
#include<stdio.h>
#include<string.h>
intmain()
{
chars[128];
char*p1,*p2;
intmax=0,len=0;
printf("Inputastring:");
gets(s);//此處用get更好,get會將空格也輸入
p1=s;
for(inti=0;i<=strlen(s);i++)
{
if((s[i]=='')||(s[i]=='