替换方法

来源:互联网 发布:泛雅网络教学平台 克隆 编辑:程序博客网 时间:2024/05/03 00:55

#include <iostream.h>
#include<string.h>
#define N 100
void th(char *p, char *pc, char *pt);
void main()
{
 char str[N], cstr[N],tstr[N];
 int s;
 cout << "请输入字符串:/n";
 cin.getline(str, N , '/n'); 
 cout << "请输入要替换的字符串:/n";
 cin.getline(cstr, N , '/n');
 cout << "请输入替换的字符串:/n";
 cin.getline(tstr, N , '/n');
 s=strlen(tstr);
 if(strlen(cstr) != strlen(tstr))
  cout<<"替换长度不匹配!"<<endl;
 else
     th(str,cstr,tstr);
}
void th(char *p, char *pc, char *pt)
{
 int i,j,g;  
 int num = 0; 
 char *ptrS;
 char *a,*c;
 a=p;
 g=0;
 while( *a != '/0' )
 {
   i = 0;
   ptrS = pc; 
   while( 1 )
  {
   if( *ptrS == *(a + i) )
    { ptrS++;  i++; j=g; }
   else break;
   if( *ptrS == '/0') 
    { num++; 
         cout<<"查找到"<<num<<"个在第"<<j+1<<"个位置"<<endl;
   c=pt;
   while(1)
   { 
    if(*c=='/0')
    {  break; }
    else
    {
     *(p+j)=*c;
     j++;
     c++;
    }
   }

         break; 
   }
   }
     a++;
   g++;
 }
 a=p;
 while(1)
 {
  cout<<*a;
  a++;
  if(*a=='/0') break;
 }
 cout<<endl;

}

原创粉丝点击