编程实现WORD字符替换功能。
提示:
设计两个函数:主函数main和字符替换函数replace。
主函数功能:输入原文text、输入查找串s、输入替换串t,然后调用替换函数replace,最后显示替换后的结果。
replace函数定义:void replace(char* text, char s, char t);
replace函数功能:查找原文中与串s匹配的字符串,然后用串t替换串s。
高级扩展项:实现WORD的交互式替换功能,即当查找到串s,就询问“继续查找F”或“替换R”。若按键’F’,则继续查找串s的下一个位置;若按键’R’,则用串t替换串s。求高手解答!指导一下!新手
你参考参考:
#include<stdio.h>
#include<string.h>
int replace(char*p_str)
{
int i,n=0;
for(i=0;*(p_str+i)!='\0';i++)
{
if(*(p_str+i)=='t')
{*(p_str+i)='e';
n++;}
if(*(p_str+i)=='T')
{*(p_str+i)='E';
n++;}
}
return n;
}
main()
{
char str[50];
char*p_str=str;
int n;
printf("input a string:\n");
gets(p_str);
n=replace(p_str);
printf("the replaced number:%d\n",n);
printf("the final string:");
puts(p_str);
}
写一个函数replace将用户输入的字符串中的字符t(T)都替换为e(E),并返回替换字符的个数。字符串在主函数中输入,替换后的字符串和替换字符的个数都在主函数中输出。要求使用指针方法来完成。