當前位置:首頁 » 編程語言 » 查找關鍵數c語言
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

查找關鍵數c語言

發布時間: 2022-11-07 20:50:27

1. 怎樣在c語言中查找數字

#include<stdio.h>
void search(int x[],int y,int n);
void main()
{
int a[10],i,key,n;
printf("How many numbers you want to input(n<=10):\n");
scanf("%d",&n);
printf("Please input the array!\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
puts("The key you want to search:");
scanf("%d",&key);
search(a,key,n);
getch();
}
void search(int x[],int y,int n)
{
int i,j=-1;
for(i=0;i<n;i++)
if(y==x[i])
j=i;
if(j!=-1)
{
printf("下標為:%d\n",j);
printf("它是該數列中的第 %d 個數。",j+1);
}
else
printf("NOT FOUNDED!");
}

2. 在一個數組中查找一個數,用C語言怎麼寫代碼

#include<stdio.h>

intmain(void)

{

inti,j,k=0,sz[10]={5,75,89,428,576,5986,7543,8524,9805,1057};

printf("請輸入要查找的數:");

scanf("%d",&j);

for(i=0;i<10;i++)

if(sz[i]==j)

{

printf("sz[%d]=%d ",i,sz[i]);

k++;

}

if(!k)

printf("數組中沒有您要查找的數。 ");

return0;

}

3. 用C語言編寫一個從普通文本字元串中查找給定字元串(關鍵詞)的程序。(急,求真大神解答)

問題是這樣:C語言編寫函數int fun(char *s,char *c), 函數的功能是查找母串s中,字串c出現的次數.
答案是這樣:
#include<stdio.h>
#include<string.h>
int fun(char*s,char*c)
{
int i=0,j=0,k,n=strlen(c),a=0;
while(s[i])
{
if(s[i]==c[j])
{ for(k=1;(s[i+k]&&c[i+k])&&(s[i+k]==c[j+k]);k++);
if(k==n)
a++;
i+=n;
}
else
i++;
}
return a;
}
void main()
{
char s[40],c[20];
int m=0;
gets(s);
gets(c);
m=fun(s,c);
printf("%d\n",m);
}
你把它改為文件不就行了!你也是知道的演算法思想是一樣的!

4. C語言鏈表關鍵字檢索

//#include "stdafx.h"//vc++6.0加上這一行.
#include "stdio.h"
#include "string.h"

struct A{
char str[20];
struct A* next;
}
void main(void){
char x;
struct A *p=head;//head是鏈表頭指針,必須是已知的
printf("Enter the characters to be retrieved:ch=");
x=getchar();
fflush(stdin);
for(;*p;p=p->next){
if(strchr(p->str,x))
printf("%s ",p->str);
}
printf("\n");
}

5. 求一個C語言的程序 要求能檢索關鍵字

#include<stdio.h>
#include<string.h>

intcount(constchar*data,constchar*key)
{
/*在data中查找key出現的次數*/
constchar*pos=data;
intresult=0,len=strlen(key);

while((pos=(strstr(pos,key)))!=NULL)
{
++result;
if(pos[len]=='')
break;
else
pos+=len;
}

returnresult;
}

intmain()

{
printf("%d ",count("abcabcdef","abc"));/*測試查找函數*/
return0;
}

6. C語言用二分法查找關鍵字

#include<stdio.h>
#include<stdlib.h>
#define Size 15

int main()
{
int binarySearch(int [], int, int, int);
void printHeader(void);
void printRow(int [],int,int,int);

int a[Size],i,key,element;
for(i=0;i <= Size-1;i++)
a[i]=2*i;

printf("Enter a number between 0 and 28:");
scanf("%d",&key);
printHeader();

element=binarySearch(a, key, 0, Size-1);
if(element!=-1)
printf("\n%d found in array element %d !\n",key,element);
else
printf("\n%d is not found!\n",key);

system("pause");
}

void printHeader()
{
int i;
printf("\nSubscripts:\n");
for(i=0;i<=Size-1;i++)
printf("%3d",i);
printf("\n");
for(i=1;i<=4*Size;i++)
printf("-");
printf("\n");
}

int binarySearch(int array[], int searchKey, int low, int high)
{
void printRow(int array[],int low,int middle,int high);
int middle;

while(low<=high){
middle=(low+high)/2;
printRow(array,low,middle,high);
if(searchKey==array[middle])
return middle;
else if(searchKey<array[middle])
high=middle-1;
else
low=middle+1;
}

return -1;
}

void printRow(int array[],int low,int middle,int high)
{
int i;
for(i=0;i<=Size-1;i++)
if(i<low||i>high)
printf(" ");
else if(i==middle)
printf("%3d*",array[i]);
else
printf("%3d",array[i]);
printf("\n");
}

效率分析:線型查找擺脫了數組排序的約束,不足之處是不適合大型數據查找,並且查找方法比較老套,如果要找的數是數組中最後一個數n,那麼搜索從0開始,一直檢索到n,要經過n次遍歷,時間復雜度:O(n),而二分查找法中如果查找關鍵字小於數組中間的元素,就查找數組的頭半部分,否則查找數組的後半部分,時間復雜度:O(log2N),如果在指定子數組中還沒有查找到關鍵字,就再把子數組折半,反復進行這種查找,直到要查找的關鍵字等於子數組中間的元素,或沒有找到關鍵字為止。在最壞的情況下,用二分法查找有1024個元素的數組也只需要比較10次,即用2除1024,連續除10次得到1為止,如果有1048576(2的20次方)個元素,用二分法只要比較20次就可以找到要查找的元素,而用簡單的線型查找則需要進行2的20次方查找,可見二分法比線型查找法的效率要高得多,對10億個元素的數組來說,平均比較5億次和30次簡直是天壤之別!所以掌握二分法對在龐大的數組庫處理是很有效的!

7. c語言中如何從一個字元串中查詢是否有給定的關鍵詞!

  1. strstr() 函數搜索一個字元串在另一個字元串中的第一次出現。找到所搜索的字元串,則該函數返回第一次匹配的字元串的地址;如果未找到所搜索的字元串,則返回NULL。
    包含文件:string.h
    函數名: strstr
    函數原型:
    extern char *strstr(char *str1, const char *str2);
    語法:
    strstr(str1,str2)
    str1: 被查找目標string expression to search.
    str2: 要查找對象The string expression to find.
    返回值:若str2是str1的子串,則先確定str2在str1的第一次出現的位置,並返回此str1在str2首位置的地址。;如果str2不是str1的子串,則返回NULL。

  2. 常式:

    #include<stdio.h>
    #include<string.h>
    intmain(){
    constchar*a="abcdefgh";
    constchar*b="abc";
    if(strstr(a,b)!=NULL)printf("found! ");
    elseprintf("notfound! ");
    return0;
    }

8. C語言如何查找並輸出數組中含有某一關鍵字的所有元素

strcmp 是整個字元串比較的,不能用 strcmp,可以用 strstr() 函數,strstr 是在一個字元串中查找一個子串,如果查到返回子串在字元串的位置,查找不到返回NULL。例如:

constchar*p=strstr("清炒土豆絲","土豆");

9. 幫忙用c語言寫一個關於查找關鍵字的程序~~~~~

#include<stdio.h>
#include<string.h>
ha(FILE *fp,char *y)
{char c,k[20];
int i=0;
if(feof(fp))
return 0;
while(!feof(fp))
{c=fgetc(fp);
if(c!='\n')
{k[i]=c;
i++;}
else {k[i]='\0';
strcpy(y,k);
return 0;}
}
}
sho(FILE *fp,char *a)
{FILE *fp1;
int i=0,k=0;
char c;
while(!feof(fp))
{c=fgetc(fp);
if(c==*(a+i))
i++;
else i=0;
if(i==strlen(a))
{k++;
i=0;}
}
return k;
}
main()
{FILE *fp2,*fp1,*fp3;
char a[20];
int i=0;
fp1=fopen("key.txt","r");
fp3=fopen("Out.txt","w");
while(!feof(fp1))
{fp2=fopen("old.txt","r");
if(ha(fp1,a))
return 1;
i=sho(fp2,a);
printf("%d",i);
getch();
if(i!=0)
{fprintf(fp3,"%s:%d\n",a,i);
printf("%s:%d\n",a,i);
i=0;}
fclose(fp2);
}
}
文件KEY.TXT的關鍵字格式為:
int
char
FILE
if

關鍵字的格式要加回車

10. 怎樣使用c語言實行將打開指定文件,並搜索其要尋找的關鍵詞個數

可以在文件打開後,使用strtok函數進行詞語的截取,然後使用strcmp函數進行關鍵詞的比較。