当前位置:首页 » 编程语言 » 查找关键数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函数进行关键词的比较。