谁会编C语言程序计算字符串中子串出现的次数和位置啊?多谢各位了,俺星期1就要交了。

来源:百度知道 编辑:UC知道 时间:2024/06/03 12:43:32
谁可以帮我写个C语言程序计算字符串中子串出现的次数和位置啊(如果不用到指针最好,实不用也只能用了)?多谢各位了,俺星期1就要交了。

题 目要求就是程序可以接受用户输入一串英文字母和一串短的密码字母,程序结果必须输出那个密码字母出现的位置和次数。
例如:用户输入的密码字母是GCTA, 而一串英文字母输入的是ATCGCCCGCTACAGCTAGCTAGCTAACGAT,程序必须找出前者在后者中出现的次数和位置。
谢谢各位,我自己写出来了。答案如下:
#include <stdio.h>
#include <string.h>

main()
{
int i=0,j=0,position=0;
int occur=0;
char code[50],text[100];

printf(" LAB2_A: A program of accepts a line of text and a secret code\n\n");

printf("please input your text: \n");
scanf("%s", text);

printf("please input your code: \n");
scanf("%s",code);

while(text[j]!='\0')
{
if ((text[j]==code[i])&&(code[i]!='\0'))

{
i++;
j++;
}

else

终于好了!下面是初稿,我在想想更通用的……
#include <stdio.h>
#include<string.h>
main()
{char s1[]="ATCGCCCGCTACAGCTAGCTAGCTAACGAT";
int flag[7]={0,0,0,0,0,0,0},position[7]={0,0,0,0,0,0,0};
int i=0,j=0;
for(i=0;i<strlen(s1)-3;i++)
if(s1[i]=='G'&&s1[i+1]=='C'&&s1[i+2]=='T'&&s1[i+3]=='A')
{flag[j]=1;position[j]=i+1;j++;}
printf("GCTA is found %d times in the string.\n",j);
for(i=0;i<j;i++)
{printf("The position is %d.\n",position[i]);}
}

楼主的答案灰常不错~

太长了。。。

学习学习 占座