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