字符串处理
给出一字符串char *string = "Front%20matter%20front.htm" ;
写一函数将其中的字串“%20”用空格符号代替;
----------------解决方案--------------------------------------------------------
自己写了一个,那位再写个,交流下了
int GetNewString(char *new, const char *temp){
int i=0;
int j=0;
while(*(temp + i) != 0)
{
if(*(temp + i) != '%')
{
*(new + j) = *(temp + i);
}else{
*(new + j) = ' ';
i +=2;
}
i++;
j++;
}
*(new +j) = 0;
printf("new character = %s\n", new);
return 1;
}
----------------解决方案--------------------------------------------------------
你这个测试过了没?
我怎么觉得不太对呀,
两个指针是指向哪里的?
字符串的结尾是‘\0’不是0
还有那个if语句不知道你是什么意思,给个注释吧,
我也很菜,只能说这么多了
----------------解决方案--------------------------------------------------------
这样写应该比较好看了
//这样写应该比较好看了,程序是经过测试的,0就是结束符char * temp = "Front%20matter%20front.htm";;
char new[256]; //用来存放处理后的字符串
int i=0;
int j=0;
while(*(temp + i) != 0)
{
if(*(temp + i) != '%')
{
*(new + j) = *(temp + i);
}else{
*(new + j) = ' ';
i +=2;
}
i++;
j++;
}
*(new +j) = 0;
printf("new character = %s\n", new);
return 1;
----------------解决方案--------------------------------------------------------
#include <stdio.h>
#include <string.h>
#include <malloc.h>
int main(void)
{
char * temp = "Front%20matter%20front.htm";
char *new=(char *)malloc(strlen(temp)); //用来存放处理后的字符串
int i=0;
int j=0;
char *s0=temp;
int li=0;
int lj=0;
while((unsigned)i<=strlen(temp))
{
if(*(temp + i) == '%')
{
memcpy(new+lj,temp+li,i-li);
*(new+j)=' ';
j++;
lj=j;
i+=3;
li=i;
continue;
}
if(*(temp + i) == '\0')
{
memcpy(new+lj,temp+li,i-li);
*(new+j)='\0';
break;
}
i++;
j++;
}
printf("new character = %s\n", new);
return 0;
}
是不是很拖沓,
不过我也就这水平了,
不知道改成这样效率会不会高点?
----------------解决方案--------------------------------------------------------
我的想法
char *string = "Front%20matter%20front.htm" ;写一函数将其中的字串“%20”用空格符号代替;
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
char* replace(char* str )
{
for(;*str!='\0';str++)
{
if(*str='%'&&*(str+1)='2'&&*(str+2)='0')
{*(str++)=' ';*(str++)=' ';*str=' ';}
else continue;
}
return *str;
}
----------------解决方案--------------------------------------------------------
不好意思我刚发布的程序有些小问题,又改了一下,欢迎提出意见:
char* replace(char* s)
{
char* str=s;
for(;*str!='\0';str++)
{
if(*str=='%'&&*(str+1)=='2'&&*(str+2)=='0')
{*(str++)=' ';*(str++)=' ';*str=' ';}
else continue;
}
return str;
}
----------------解决方案--------------------------------------------------------
关于我的主题被锁之事,已到意见区投诉,请众位去看下帖子,发表下意见-----欢迎意见相同的,也欢迎意见不同的。
----------------解决方案--------------------------------------------------------
#include<stdio.h>
void main()
{ char a[]="Front%20matter%20front.htm";
int i=0;
while(a[i]!='\0')
{ if (a[i]=='%')
a[i]=' ';
if (a[i]=='2')
a[i]=' ';
if (a[i]=='0')
a[i]=' ';
i++;
}
i=0;
while(a[i]!='\0')
{printf("%c",a[i]);i++;
}
printf("\n");
}
已经运行过了 ,
明天再发用指针的 代码
现在要陪女朋友发短信去
----------------解决方案--------------------------------------------------------
[bo]以下是引用 [un]zhao8882407[/un] 在 2008-2-1 09:36 的发言:[/bo]
//这样写应该比较好看了,程序是经过测试的,0就是结束符
char * temp = "Front%20matter%20front.htm";;
char new[256]; //用来存放处理后的字符串
int i=0;
int j=0;
while(*(temp + i) != 0)
...
//这样写应该比较好看了,程序是经过测试的,0就是结束符
char * temp = "Front%20matter%20front.htm";;
char new[256]; //用来存放处理后的字符串
int i=0;
int j=0;
while(*(temp + i) != 0)
...
楼主写的这个不太好用啊,要是碰到%30,一样被你替换成了空格,岂不不美!
----------------解决方案--------------------------------------------------------