当前位置:首页 » 编程语言 » c语言找字符串程序
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

c语言找字符串程序

发布时间: 2022-06-18 15:02:50

c语言字符串查找

参考代码: #include int main(void){ char a[80]; char b[80]; char *p = a;//辅助变量p int count = 0;//用来记录子串出现的次数 printf("请输入主字符串:"); gets(a); printf("请输入要查找的子串:"); gets(b); while((p = strstr(p,b)) != NULL) { count ++; p++; } printf("子串出现的次数为%d次\n",count);return 0;}

㈡ 用C语言编写一个从普通文本字符串中查找给定字符串(关键词)的程序。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
const char *a = "This is an example of text string with LF";
const char *b = "example";
int i;
int rval;
for (i = 0; i < strlen(a) - strlen(b); i++) {
rval = strncmp(a + i, b, strlen(b));
if (!rval) {
printf("found!\n");
return 0;
}
}
printf("not found!\n");
return 0;
}

㈢ 用C语言编写一个程序查找一个字符串在另一个字符串中出现的次数怎么写

实现代码如下:

#include<stdio.h>
#include<stdlib.h>
intmain()
{
charS[100],T[20];
charch1,ch2;
printf("请输入主字符串: ");
ch1=getchar();
inti=0;
while(ch1!=' ')
{
S[i]=ch1;
i++;
ch1=getchar();
}

printf("请输入要筛选的字符串: ");
ch2=getchar();
intj=0;
while(ch2!=' ')
{
T[j]=ch2;
j++;
ch2=getchar();
}

intm,n;//m为S的下标,n为T的下标
m=0;
n=0;
intnum=0;//num用于记录选定单词出现的次数
while(m<=i&&n<=j)
{
if(S[m]==T[n])
{
m++;
n++;
}
else
{
m=m-n+1;
n=0;
}
if(n==j)
{
num++;
}
}
if(m==i+1)
{
printf("出现的次数是%d",num);
}
}

代码二:

intstrstr_cnt(constchar*string,constchar*substring)
{
inti,j,k,count=0;
for(i=0;string[i];i++)
for(j=i,k=0;string[j]==substring[k];j++,k++)
if(!substring[k+1])
count++;
return(count);
}

㈣ c语言从文件中查找字符串

c语言从文件中查找字符串的方法。

如下参考:

1.打开python命令窗口,定义并分配字符串变量s1。

㈤ c语言编写一个程序,实现查找一个字符串中的特定字符,并将其删除.

一、算法描述

逐个比较字符串source中的字符,若当前i位置的字符等于待删除字符ch,则i+1..len-1之间的子串整体前移;如此反复,直到所有待删除字符都找到并被删除为止。


二、操作过程

请输入一个字符串:Howareyou?
请输入待删除字符:o
新的字符串:Hwareyu?

㈥ c语言 查找指定字符

㈦ 编写程序实现在一个字符串中查找指定的字符(请用c语言作答)

#include&lt;stdio.h&gt;

int main()

{

int i,index,count;

char a,ch,str[80];

scanf("%c ",&a);

i=0;

index=-1;

count=0;

ch=getchar();

for(i=0;ch!=' ';i++){

str<i>=ch;

count++;

ch=getchar();

}

for(i=0;i&lt;count;i++)

if(a==str<i>)

index=i;

if(index!=-1)

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

else

printf("Not Found");

return 0;

}

(7)c语言找字符串程序扩展阅读:

getchar()用法:

getchar()函数的作用是从计算机终端(一般为键盘)输入一个字符。getchar()函数只能接收一个字符,其函数值就是从输入设备得到的字符。

例:

#include&lt;stdio.h&gt;

int main(void)

{

int c;

/*Note that getchar reads from stdin and

is line buffered;this means it will

not return until you press ENTER.*/

while((c=getchar())!=' ')

printf("%c",c);

return 0;

}

注:可以利用getchar()函数让程序调试运行结束后等待编程者按下键盘才返回编辑界面,用法:在主函数结尾,return 0;之前加上getchar();