㈠ 输入任意多个关键字,按各种查找方法查找某个关键字。(用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语言 查找指定字符