C语言 求大牛们帮忙写一个小程序

(点击图片放大。。)接口如下:intgetRes(constchar*str,intstrlen,constchar*substr,intsublen,int*count... (点击图片放大。。)
接口如下:
intgetRes(const char*str, int strlen, const char *substr, int sublen, int *count, char *buf2)
2)问有点问题,实际上就是把原串abcd换成dcba再把替换后得到的新的字符串传出来
展开
 我来答
百度网友05adf615fb
2014-10-22
知道答主
回答量:11
采纳率:0%
帮助的人:7.7万
展开全部
/* ************************************************************************
* Filename: test.c
* Description:
* Version: 1.0
* Created: 2014年10月22日 09时31分13秒
* Revision: none
* Compiler: gcc
* Author: YOUR NAME (),
* Company:
* ************************************************************************/

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

int getRes( const char *str,int strlen, //strlen没用上...
const char *substr,int sublen,
int *count,
char *buf2)
{
char *p[1000] = {NULL};
const char *q = str;
int i = 0,j = 0;

do {
p[i] = strstr(q,substr);
q = p[i++]+sublen;
} while(p[i-1] != NULL);
*count = --i;

strcpy(buf2,str);
for(;j < i;) {
memcpy(buf2+(p[j++]-str),"dcba",4);//偷个懒,直接上"dcba" ^0^
}

return *count;
}

int main(int argc, char *argv[])
{
char *str = "abcd11111abcd22222abcdqqqqqabcd11111abcd22222abcdqqqqqabcd11111abcd2222abcdqqqqq";
char *sub = "abcd";
char *buf = (char *)malloc(strlen(str)+1);// '\0'
int Count = 0;

getRes(str,strlen(str),sub,strlen(sub),&Count,buf);
printf("字串 \"%s\" 出现的次数为 %d 次.\n",sub,Count);
printf("替换之前:\n %s \n替换之后:\n %s .\n",str,buf);

free(buf); //屁股要擦干净..
buf = NULL;
return 0;
}
匿名用户
推荐于2016-10-05
展开全部
int getRes(const char*str, int strlen, const char *substr, int sublen, int *count, char *buf2)
{
int i,j,k;
char temp;
*count = 0;
for(i = 0,j = 0; i < strlen; )
{
*(buf2+i) = *(str+i);
if(*(str+i) == *(substr+j))
{
if(sublen-1 == j)
{
(*count)++;
j = 0;

for(k = sublen-1; k > sublen/2; k--)
{
temp = *(buf2-k);
*(buf2-k) = *(buf2+sublen-k-1);
*(buf2+sublen-k-1) = temp;
}

i++;
continue;
}
i++;
j++;
}
else
{
if(0 == j)
{
i++;
}
else
{
j = 0;
}
}

}

}
本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式