各位帮个忙!!!

tohy 2003-05-31 02:53:42
帮忙用c语言写个程序:
输入一段文字,在文字中查找某个单词w,且用一个单词s代替该词,输出替代的次数。
要求:一,编写查找函数"search()"
二,编写替换函数"replace()"

谢谢了!
...全文
47 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
蝎子i软件 2003-06-02
  • 打赏
  • 举报
回复
参考一下吧。
--------------------
#include <stdio.h>
#include <string.h>

#define STRLENMAX 80

int searchANDreplace( char *str, char *w, char *s ) { /*return the first position*/
unsigned int istr, iw, is, temp, temp2;
unsigned int times=0;
int s_w;
char tc;

if( strlen(str)<1 ) { /* str 为空 */
printf(" str is empty! ");
return times;
}
if( strlen(w)<1 ) { /* w 为空(注:“空”不是”空格“) */
printf(" w is empty! ");
return times;
}

for( istr=0; istr<strlen(str); ++istr ) {
if( str[istr]!=w[0] )
continue;
else {
temp = istr;
++temp;
for( iw=1; iw<strlen(w); ++iw ) {
if( temp>=strlen(str) || str[temp]!=w[iw] )
break;
else
++temp;
}
if( iw==strlen(w) ) { /* w found --> replace */
s_w = strlen(s)-strlen(w);
if( s_w>0 ) { /* str后移 */
if( strlen(str)+s_w>=STRLENMAX ) { /* 超长 */
printf(" NO enough memory! ");
return times;
}
str[strlen(str)+s_w] = '\0'; /*字符串结束符*/
for( temp2=strlen(str)-1; temp2>=istr+iw; --temp2 )
str[temp2+s_w] = str[temp2];
} else if( s_w<0 ) { /* str前移 */
for( temp2=istr+strlen(s); temp2<strlen(str); ++temp2 )
str[temp2] = str[temp2-s_w];
}
is = 0;
while( is<strlen(s) ) {
str[istr] = s[is];
++str;
++is;
}
++times;
}
}
}

return times;
}

void main() {
char str[STRLENMAX] = "Hello, may name is Scorpion! Nice to meet you here! Nice day, isn't it?";
char *w = "Nice";
char *s = "Fantastic";

printf("Replace times: %d.\n", searchANDreplace( str, w, s ) );
printf("%s", str);
}
tohy 2003-06-02
  • 打赏
  • 举报
回复
谢谢楼上那位!找到后,怎样才能替换呢?
cdocument 2003-06-02
  • 打赏
  • 举报
回复
调试过了,以下程序可以运行:不过有几个地方请高手指误,
bool search(const char* text,const char* w)
{
if ((strlen(text)<strlen(w))||(strlen(w)==0))
return 0;
char* cmp=new char[strlen(text)];
strcpy(cmp,text); //如果不要这句,strlen(cmp)=strlen(text)+4
char* temp=new char[strlen(w)];
strcpy(temp,w); //事实上这句没有意义,如果不要这句,strlen(temp)=strlen(w)+4为什么??
while (*cmp)
{
if (strxfrm(temp,cmp,strlen(w))<strlen(w))
break;
if(strcmp(temp,w)==0)
return 1;
cmp++;
}
return 0;
}
cdocument 2003-06-02
  • 打赏
  • 举报
回复
bool search(const char* text,const char* w)
{
if (strlen(text)<strlen(w))||(strlen(w)==0)
return 0;
char* cmp=new char[strlen(text)];
strcpy(cmp,text);
char* temp=new char[strlen(w)];
while (*cmp)
{
if (strxfrm(temp,cmp,strlen(w))<strlen(w))
break;
if(strcmp(temp,w)==0)
return true;
cmp++;
}
return 0;
}


















messagebox 2003-06-02
  • 打赏
  • 举报
回复
上面写的都没有考虑存储
skytears66 2003-06-01
  • 打赏
  • 举报
回复
《数据结构(清华)》里有现成的算法呀 字符串匹配
tohy 2003-06-01
  • 打赏
  • 举报
回复
顶!
zalyer 2003-05-31
  • 打赏
  • 举报
回复
哈哈。简单问题。
pzytony 2003-05-31
  • 打赏
  • 举报
回复

加油
tohy 2003-05-31
  • 打赏
  • 举报
回复
楼上那位没有把题目看清楚,w和s都是代表单词,而不是'w','s'!!
flmttm 2003-05-31
  • 打赏
  • 举报
回复
sorry更正:
假设你的文章放到数组article[MAX]了
int search(char article[])
{
char *p;
int i;
static count=0;
p=article;
for(i=0;i<MAX&&*p!=NULL;i++)
{
if(*p++=='w')replace(p); //更改此处
count++;
}
return count;
}

void replace(char *p)
{
*p='s';
}
flmttm 2003-05-31
  • 打赏
  • 举报
回复
假设你的文章放到数组article[MAX]了
int search(char article[])
{
char *p;
int i;
static count=0;
p=article;
for(i=0;i<MAX&&*p!=NULL;i++)
{
if(*p=='w')replace(p);
count++;
}
return count;
}

void replace(char *p)
{
*p='s';
}
tohy 2003-05-31
  • 打赏
  • 举报
回复
难道就没有人知道吗?
tohy 2003-05-31
  • 打赏
  • 举报
回复
查找那个函数就没有实现查找的功能呀!!那只不过是比较了两个串是否相等!!
snipersu 2003-05-31
  • 打赏
  • 举报
回复
void Replace(char* w,const char* s)
{
strcpy(w,s);
}
bool Search(const char* w,const char* s)
{
if(strcmp(w,s)==0)
return true;
return false;
}
tohy 2003-05-31
  • 打赏
  • 举报
回复
帮帮忙!!

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧