当前位置:首页 » 编程语言 » 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;
}