输入一个字符串,计算字符串中子串出现的次数
2017-02-02 20:17
561 查看
#include <stdio.h>
#include <string.h>
int time(char *s1,char*s2);
int main()
{
char str[256];
char son[25];
int t;
printf("Please input a long strings:");
scanf("%s",str);
printf("Please input a son strings:");
scanf("%s",son);
t = time(str,son);
printf("The son strings total show %d times in long strings.\n",t);
return 0;
}
int time(char *s1,char*s2)
{
char *p1 = s1,*p2 = s2;
int count = 0,k,len;
len = strlen(s2);
while( (*p1) != '\0' )
{
k = 0;
while( (*p1) == (*p2) && (*p2) != '\0' ) //查找字符串中的子串
{
p1++;
p2++;
k++;
}
if( k == len )
count++;
p2 = s2;
p1++;
}
return count;
}
程序运行示例:
Please input a long strings:zxcvbnzxcvzxcvbnbvczxcvzxcvbn
Please input a son strings:zxc
The son strings total show 5 times in long strings.
#include <string.h>
int time(char *s1,char*s2);
int main()
{
char str[256];
char son[25];
int t;
printf("Please input a long strings:");
scanf("%s",str);
printf("Please input a son strings:");
scanf("%s",son);
t = time(str,son);
printf("The son strings total show %d times in long strings.\n",t);
return 0;
}
int time(char *s1,char*s2)
{
char *p1 = s1,*p2 = s2;
int count = 0,k,len;
len = strlen(s2);
while( (*p1) != '\0' )
{
k = 0;
while( (*p1) == (*p2) && (*p2) != '\0' ) //查找字符串中的子串
{
p1++;
p2++;
k++;
}
if( k == len )
count++;
p2 = s2;
p1++;
}
return count;
}
程序运行示例:
Please input a long strings:zxcvbnzxcvzxcvbnbvczxcvzxcvbn
Please input a son strings:zxc
The son strings total show 5 times in long strings.
相关文章推荐
- 4.3输入一个字符串,计算字符串中字串中子串出现的次数
- 输入一个字符串,计算字符串中子串出现的次数
- 输入一个字符串,计算字符串中子串出现的次数
- 输入一个字符串,计算字符串中子串出现的次数
- 输入一个字符串,计算字符串中子串出现的次数
- 输入一个字符串,计算字符串中子串出现的次数
- 输入一个字符串,计算该字符串中子串出现的次数
- 自己编写的一个求字符串中子串出现次数计算小程序字串长度为2
- 用Regex类计算一个字符串出现次数是最好方法【转载】
- String去除字符串两端空格、将一个字符串进行反转、计算字符串中出现的的次数、获取两个字符串中最大相同字串
- 输入一个字符串 打印出每个字符出现的次数
- 计算某字符串在另外一个字符串中出现的次数
- java 计算字符串中子串出现的次数
- sql 计算子字符串在父字符串中出现次数的一个非常简单的办法
- 计算字符串中子串出现的次数
- 给一个字符串,包含了空格等标点符号,计算出出现次数最多的字母和该字母出现的次数
- 判断一个字符串中子串出现的次数.
- 统计一个长度为2的子字符串在另一个字符串中出现的次数.例如:假定输入的字符串为“asd asasdfg asd as zx67 asd mklo”,子字符串为“as”,函数返回值为6。
- C# 在一个字符串中,找出出现次数最多的字母,并计算次数?
- vbs计算字符串在一个文本中出现次数的函数