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

c語言查找輸入的內容

發布時間: 2022-05-10 18:51:47

㈠ 輸入任意多個關鍵字,按各種查找方法查找某個關鍵字。(用c語言描述)

//二分查找 來實現
#include "stdio.h"

typedef struct
{
char *elem;
int length;
}sstable;

void create(char **t)
{
int i;
static char a[11];
*t=a;
for(i=1;i<=10;i++)
{ //輸入任意多個關鍵字
printf("A[%d] is:",i);
scanf("%c",&a[i]);
if (a[i] != '\n') getchar();
}
}

int searth(char *t,char k)
{
int i;
for (i=10;i>=0 && t[i]!=k ;i--);
return i;
}

void output(char *t)
{
int i;
for (i=1;i<=10;i++)
printf("\n A[%d] is %c",i,t[i]);
}

void px(char *t)
{
char s;
int i,j;
for (i=1;i<=10;i++)
for (j=i+1;j<=10;j++)
{
if (t[i]>t[j]) {s=t[i];t[i]=t[j];t[j]=s;}
}
}

int search_bin(char *t,char k)
{
int low=1,high=10,mid;
while (low<=high)
{
mid=(low+high)/2;
if (k==t[mid]) return mid;
else if (k<t[mid]) high=mid-1;
else low=mid+1;
}
return 0;
}

main()
{
char *t,k;
int s;
create(&t);
output(t);

printf("\nplease input you search char:"); //輸入你要查找的某個關鍵字
k=getchar();
s=searth(t,k);

if (s>=0) printf("1: use search find is A[%d]\n",s);
else printf("1:can not find it\n");

px(t);
output(t);

s=search_bin(t,k);
if(s==0) printf("\n1:can not find it \n");
else printf("\n2:use search_bin find is A[%d]\n",s);

getchar();
}

㈡ C語言程序 如何從文件中查找特定的字元

打開文件,遍歷文件內容然後一個一個匹配查找就好了。
下面是一段示例代碼:
#include <stdio.h>

int main()
{
FILE *fp;
char filename[100];
printf("請輸入文件名:\n");
gets(filename);
fp=fopen(filename,"r");
char c,x,flag=0;
printf("請輸入要查找的字元:\n");
scanf("%c",&x);
while(fscanf(fp,"%c",&c)!=EOF)
{
if(c==x)
{
flag=1;
break;
}
}
if(flag==1)
printf("文件中含有字元%c\n",x);
else
printf("文件中沒有字元%c\n",x);
return 0;
}

㈢ C語言輸入字元,查找文件內容,如果存在,重寫那一行在內的三行內容,

這個文件操作只能將整個文件後面的都讀出來然後重寫,不能在中間插入

㈣ c語言如何查找字元串

C語言中的標准函數庫中的strchr()函數可以實現查找字元串中的某個字元。
C語言strchr()函數:
查找某字元在字元串中首次出現的位置
頭文件:#include
<string.h>
strchr()
用來查找某字元在字元串中首次出現的位置,其原型為:
char
*
strchr
(const
char
*str,
int
c);
【參數】str
為要查找的字元串,c
為要查找的字元。
strchr()
將會找出
str
字元串中第一次出現的字元
c
的地址,然後將該地址返回。
注意:字元串
str
的結束標志
NUL
也會被納入檢索范圍,所以
str
的組後一個字元也可以被定位。
【返回值】如果找到指定的字元則返回該字元所在地址,否則返回
NULL。
返回的地址是字元串在內存中隨機分配的地址再加上你所搜索的字元在字元串位置。設字元在字元串中首次出現的位置為
i,那麼返回的地址可以理解為
str
+
i。
提示:如果希望查找某字元在字元串中最後一次出現的位置,可以使用
strrchr()
函數。

㈤ c語言 鍵盤輸入字元串,再輸入要查找的字元,沒有就輸出沒有找到,並且可以多次查找字元

#include <stdlib.h>
#include <conio.h>
#include <stdio.h>

#define N 20

int main()
{
char str[N]={NULL},ch,choose;
int count=0,i;

do{
count=0;
fflush(stdin);
system("cls");
printf("請輸入字元串: \n");
scanf("%s",str);
fflush(stdin);
printf("請輸入要查找的字元: \n");
scanf("%c",&ch);

for(i=0;str[i]!='\0';i++)
{
if(str[i]==ch)
{
printf("字元串%s的第%d個字元是%c。\n",str,i+1,ch);
count++;
}
}

if(count==0) printf("沒有找到。\n");

printf("\n還繼續嗎?(n: 退出\t其他輸入: 繼續)");
fflush(stdin);
scanf("%c",&choose);

if(choose=='n') break;
}while(1);

system("pause");

return 0;
}

㈥ C語言:編寫一個查找數據的功能菜單

#include <stdio.h>
#include <stdlib.h>

typedef struct student
{
int num; //學號
int score; //成績
struct student *next; //節點的next
}stu; //學生信息節點

void main()
{
void creat(stu *);
void select(int,stu*);
void show(stu*); //函數聲明
stu *L;
int flag=1,sno;
char choice;
L=(stu*)malloc(sizeof(stu));
L->next=NULL; //初始化鏈表
creat(L); //創建學生信息鏈表
show(L); //顯示鏈表中所有學生的信息
while(flag) //控制自動循環查找
{
printf("do you want to sele\n");
getchar(); //吸收回車符
scanf("%c",&choice);
if(choice=='y'||choice=='Y')
{
printf("input the num\n");
scanf("%d",&sno);
select(sno,L);
} //用戶要查找(輸入'y;或者'Y'),查找學生信息
else
{
printf("select is over\n");
flag=0;
} //用戶不要求查找,則退出程序
}
}

void creat(stu *L)
{
stu *r;
int number,score,flag=1;
char choice;
printf("please input the infor of student\n");
while(flag) //控制循環輸入
{
printf("do you want to creat\n");
scanf("%c",&choice);
if(choice=='y'||choice=='Y')
{
printf("number:");
scanf("%d",&number);
printf("score:");
scanf("%d",&score); //輸入學生信息
r=(stu*)malloc(sizeof(stu));
r->num=number;
r->score=score;
r->next=L->next;
L->next=r;
getchar();
} //用頭插法將學生信息鏈入表中
else
{
printf("input over\n");
flag=0;
} //輸入結束

}
}

void select(int number,stu *L)
{
stu *p;
p=L->next;
while(p!=NULL&&p->num!=number)//鏈表未結束並且未找到信息
p=p->next; //遍歷鏈表查找對應學號
if(p->num==number)
{
printf("the infor of this stu is:\n");
printf("num:%d,score:%d\n",p->num,p->score);
} //找到對應學號,則輸出節點內容
else if(p==NULL)
printf("can not find\n");
}//查找學號 //未找到學號信息

void show(stu *L)
{
stu *p;
p=L->next;
while(p!=NULL) //鏈表未結束
{
printf("num:%d,score:%d",p->num,p->score);//輸出鏈表中內容
p=p->next; //指針後移
}
printf("\n");
}//顯示鏈表中內容

程序在VC6.0中調試通過!按照提示輸入信息即可

㈦ c語言如何利用查找法定義輸入輸出

可利用線性(順序)查找法,查找演算法是指:從一些數據之中,找到一個特殊的數據的實現方法。查找演算法與遍歷有極高的相似性,唯一的不同就是查找演算法可能並不一定會將每一個數據都進行訪問,有些查找演算法如二分查找等,並不需要完全訪問所有的數據。

㈧ C語言中如何輸入若干行文字,再輸入一個字元串,查找並輸出含有該字元串的那這些行

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

intmain(void)
{
chartxt[20][100]={"0"};
charstr[100]={"0"};
intn=0,i=0;

printf("pleaseinputthenumberoflines: "); //輸入你要輸入的行數
scanf("%d",&n);

printf("pleaseinput%dlinestext: ",n); //輸入你說的若干行文本
for(i=0;i<n;i++){
scanf("%s",txt[i]);
}
printf("pleaseinputthestring: "); //輸入要匹配的字元串
scanf("%s",str);

for(i=0;i<n;i++){
if(strstr(txt[i],str))
printf("%s ",txt[i]);
}

getch();
return0;
}

㈨ C語言題目:在數組中查找指定元素

#include <stdio.h>

#define MAXN 10

int search( int list[], int n, int x );

int main()

{

int i, index, n, x;

int a[MAXN];

printf("輸入個數: ");

scanf("%d",&n);

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

scanf("%d", &a[i]);

printf("輸入x: ");

scanf("%d", &x);

index = search( a, n, x );

if( index != -1 )

printf("index = %d ", index);

else

printf("Not found ");

return 0;

}

int search( int list[], int n, int x ){

int i;

for(i=0;i<n;i++){

if(x==list[i])

return i;

}

return -1;

}

(9)c語言查找輸入的內容擴展閱讀:

數組使用規則:

1.可以只給部分元素賦初值。當{ }中值的個數少於元素個數時,只給前面部分元素賦值。例如:static int a[10]={0,1,2,3,4};表示只給a[0]~a[4]5個元素賦值,而後5個元素自動賦0值。

2.只能給元素逐個賦值,不能給數組整體賦值。例如給十個元素全部賦1值,只能寫為:static int a[10]={1,1,1,1,1,1,1,1,1,1};而不能寫為:static int a[10]=1;請注意:在C、C#語言中是這樣,但並非在所有涉及數組的地方都這樣,資料庫是從1開始。

3.如不給可初始化的數組賦初值,則全部元素均為0值。

4.如給全部元素賦值,則在數組說明中, 可以不給出數組元素的個數。例如:static int a[5]={1,2,3,4,5};可寫為:static int a[]={1,2,3,4,5};動態賦值可以在程序執行過程中,對數組作動態賦值。這時可用循環語句配合scanf函數逐個對數組元素賦值。

網路-數組

㈩ c語言 查找指定字元