微软笔试题3
来源:互联网 发布:清除上网痕迹软件 编辑:程序博客网 时间:2024/04/29 22:51
把字符串S中所有子串A替换成子串B,使用自定义的strstr
#include<iostream>
using namespace std;
char* My_strstr(char *str1, const char *str2 )
{
assert(str1 != NULL && str2 != NULL);
char *p = str1;
const char *q = str2;
while(*str1 != '/0' && *str2 != '/0')
{
if(*str1 == *str2)
{
++str1;
++str2;
}
else
{
str1 = ++p;
str2 = q;
}
}
if(*str2 == '/0')
{
return p;
}
return NULL;
}
void str_A_to_B(char *str,const char *subA,const char *subB)
{
assert(str != NULL && subA != NULL && subB != NULL);
int lenA = strlen(subA);
int lenB = strlen(subB);
char *p = str;
while(p != NULL)
{
p =My_strstr(str,subA);
if(p != NULL)
{
if(lenA == lenB)
{
strncpy(p,subB,lenA);
}
else if(lenA > lenB)
{
char *ptr = p + lenA;
char *ps = p +lenB;
while(*ptr != '/0')
{
*ps++ = *ptr++;
}
strncpy(p,subB,lenB);
}
else
{
int newlen = strlen(str);
char *ptr = &str[newlen-1];
char *ps = p + lenA;
int sp = lenB-lenA;
while(ptr >= ps)
{
*(ptr+sp) = *ptr;
--ptr;
}
strncpy(p,subB,lenB);
}
}
else
{
break;
}
p = My_strstr(str+lenA,subB);
}
}
int main()
{
char str[200];
memset(str,0,200);
strcpy(str,"pqrstaaaaajkjk");
char ch = 'a';
cout<<str<<endl;
char *subA = "aa";
char *subB = "QQQQQ";
str_A_to_B(str,subA,subB);
cout<<str<<endl;
system("pause");
return 0;
}
- 微软笔试题3
- 2015.4.3微软在线笔试第二题
- 微软笔试题3 Demo Day
- 微软笔试题3:Demo Day
- [转载] 微软笔试题
- 微软笔试题(zz)
- 微软笔试题4
- 微软笔试题一
- 微软笔试题
- 微软笔试题 汇总
- 2011微软笔试题
- 微软笔试题
- 微软笔试题
- 2011微软笔试题
- 微软笔试题
- 微软笔试题
- 微软笔试题:
- 微软PM笔试题
- SQL SERVER tips
- 找工作
- Castle研究-第一个Castle程序
- 关于权限管理的系统的构思
- 14点开始做,现在时间16:23。出错了,头好晕~~~
- 微软笔试题3
- 图片缓存BUG导致Ajax无效的解决
- FCKeditor與asp.net 7/24
- 看《北大牛人唐翔》有感以自勉
- Enum的toString方法
- Microsoft® Office Access 2003 Inside Out
- 高手的思路[ZT]
- Learn Windows XP In Pictures
- Ida Pro v5.0.0.879 Advanced Full