當前位置:首頁 » 編程語言 » c語言指定子串出現次數
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言指定子串出現次數

發布時間: 2023-04-11 18:45:05

c語言如何統計特定字元串出現次數

#include<stdio.h>
intmain(){
charcmp[3]="aba";
chara[256];
scanf("%s",a);
inti;
intcount=0;
for(i=0;a[i]!='';i++){
if(a[i]==cmp[0]){
intj=1;
intsite=i+1;
intflag=1;
for(j=1;cmp[j]!='';j++,site++){
if(a[site]!=''){
flag=0;
break;
}
if(cmp[j]!=a[site]){
flag=0;
break;
}
}
if(flag==1)count++;
}
}
printlf("%d ",count);
}

Ⅱ 如何用c語言編寫一個程序:輸入字元串,統計出某指定字元在字元串中出現的次數

#include "stdio.h"
void main()
{
int i,a=0,b=0,c=0,d=0,n=0;/*分別用於統計英文字元、空格、數字、其它字元、所有字元的統計*/
printf("Please input a string!\n");
while(getchar()!='\n')
n++;
i = getchar();
if(65<=i<=90||97<=i<=122)
a++;
else if(i=0)
b++;
else if(48<=i<=57)
c++;
else
d++;
printf("所輸入的字元串中英文字元有%d個,空格有%d個,數字有%d個,其它字元有%d個,總共%d個!\n",a,b,c,d,n);
return 0;
}

Ⅲ C語言程序,在文件中查找指定字元串出現的次數,對文件操作實在沒寫過,看看怎麼寫

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

#define FILE_NAME_MAX 50
#define SEPERATE_STRING_MAX 100

int StrCount(FILE *file,char *str);

int main()
{
char *filename,*spestr;
FILE *fp;
filename=(char *)malloc(FILE_NAME_MAX);
spestr=(char *)malloc(SEPERATE_STRING_MAX);
printf("Input the filename:");
while(1)
{
scanf("%s",filename);
fp=fopen(filename,"r");
if(fp!=NULL)
{
break;
}
printf("Can't open the file.Try Again!");
}
printf("Input the special string:");
scanf("%s",spestr);

printf("%d times of %s in %s.",StrCount(fp,spestr),spestr,filename);

fclose(fp);
free(filename);
free(filename);

return 0;
}

int StrCount(FILE *file,char *str)
{
int count=0;
char ch;
int p=0;;

while((ch=fgetc(file))!=EOF)
{
// 當前讀入的字元匹配 str 相應位置的字元
if(ch == str[p])
{
// 匹配下一個字元
p++;
// 如果已經匹配成功
if(str[p] == '\0')
{
count++;
// 從頭開始重新匹配
p = 0;
}
}
// // 當前讀入的字元不匹配 str 相應位置的字元
else
{
// 匹配失敗,文件指針回溯
fseek(file,(p>0?-(p-1):0),SEEK_CUR);
// 從頭開始重新匹配
p = 0 ;
}
}
return count;
}