华为面试遇到这个函数.该如何处理
华为面试遇到这个函数........
前段时间去面试,最后一轮让写一个函数,结果就是个函数,让我没能拿下这份工作.函数是要求实现一个替换的功能.原型是这样的:
void replace(char* text,char* source,char* des)
其中text就是原文,source是要求被替换的字符串,des是用来替换source的.不知道哪位大哥能帮帮小弟啊?
------解决方案--------------------
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//替换函数,注意保障空间的足够不在本函数中完成,由使用该函数的地方负责!
void replace(char* text,char* source,char* des)
{
int len = strlen(text);
char *tmp, *p, *q;
tmp = (char *)malloc(len*sizeof(char));
strcpy(tmp, text);
p=strstr(tmp, source);
q=tmp;
len=0;
while( p !=NULL)
{
strncpy(text+len, q, p-q);
len += p-q;
strcpy(text+len, des);
len += strlen(des);
q = p+strlen(source);
p=strstr(q, source);
}
strcat(text+len, q);
}
//测试主程序
int main()
{
char test[20]= "this is a test "; //注意确保 test 有足够的替换空间!!
char *s= "is ", *d= "are ";
puts(test);
replace(test, s, d);
puts(test);
system( "PAUSE ");
return 0;
}
------解决方案--------------------
我写了这个程序。不过有点乱。你将就看一下。
#include "stdafx.h "
#include <iostream>
using namespace std;
//其中text就是原文,source是要求被替换的字符串,des是用来替换source的
void replace(char* text,char* source,char* des)
{
int a=0,b=0,c=0;
char *p=new char[100];
strcpy(p,text);
while(*(text+a)!= '\0 ')//计算text、source、des的长度。
a++;
while(*(source+b)!= '\0 ')
b++;
while(*(des+c)!= '\0 ')
c++;
bool Flag=1;
int t=0,f=0;//记录字符偏移量
for (int i=0;i <a;i++)
{
Flag=1;
for(int j=0;j <b;j++)
{
if (*(text+i+j)!=*(source+j))//检验字符串是否相等
{
Flag=0;
break;
}
}
if (1==Flag)
{
for(int k=0;k <c;k++)
{*(text+i+k+t)=*(des+k);}
t=t+b;
f=f+c;
}
*(text+i+f)=*(p+i+t);
}
}
int main()
{
char a[100]={ "abcdefg "};
char b[100]={ "cd "};
char c[100]={ "kkkkkkkkkkkkkkkkk "};
replace(a,b,c);
cout < <a < <endl;
return 0;
}
前段时间去面试,最后一轮让写一个函数,结果就是个函数,让我没能拿下这份工作.函数是要求实现一个替换的功能.原型是这样的:
void replace(char* text,char* source,char* des)
其中text就是原文,source是要求被替换的字符串,des是用来替换source的.不知道哪位大哥能帮帮小弟啊?
------解决方案--------------------
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//替换函数,注意保障空间的足够不在本函数中完成,由使用该函数的地方负责!
void replace(char* text,char* source,char* des)
{
int len = strlen(text);
char *tmp, *p, *q;
tmp = (char *)malloc(len*sizeof(char));
strcpy(tmp, text);
p=strstr(tmp, source);
q=tmp;
len=0;
while( p !=NULL)
{
strncpy(text+len, q, p-q);
len += p-q;
strcpy(text+len, des);
len += strlen(des);
q = p+strlen(source);
p=strstr(q, source);
}
strcat(text+len, q);
}
//测试主程序
int main()
{
char test[20]= "this is a test "; //注意确保 test 有足够的替换空间!!
char *s= "is ", *d= "are ";
puts(test);
replace(test, s, d);
puts(test);
system( "PAUSE ");
return 0;
}
------解决方案--------------------
我写了这个程序。不过有点乱。你将就看一下。
#include "stdafx.h "
#include <iostream>
using namespace std;
//其中text就是原文,source是要求被替换的字符串,des是用来替换source的
void replace(char* text,char* source,char* des)
{
int a=0,b=0,c=0;
char *p=new char[100];
strcpy(p,text);
while(*(text+a)!= '\0 ')//计算text、source、des的长度。
a++;
while(*(source+b)!= '\0 ')
b++;
while(*(des+c)!= '\0 ')
c++;
bool Flag=1;
int t=0,f=0;//记录字符偏移量
for (int i=0;i <a;i++)
{
Flag=1;
for(int j=0;j <b;j++)
{
if (*(text+i+j)!=*(source+j))//检验字符串是否相等
{
Flag=0;
break;
}
}
if (1==Flag)
{
for(int k=0;k <c;k++)
{*(text+i+k+t)=*(des+k);}
t=t+b;
f=f+c;
}
*(text+i+f)=*(p+i+t);
}
}
int main()
{
char a[100]={ "abcdefg "};
char b[100]={ "cd "};
char c[100]={ "kkkkkkkkkkkkkkkkk "};
replace(a,b,c);
cout < <a < <endl;
return 0;
}