㈠ 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<stdio.h>
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<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<stdio.h>
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();