如何最简单的编程去掉字符串中多余的 /

joyself 2005-02-19 08:58:56
譬如 /home/hello///test//end
将该字符串变为 /home/hello/test/end
即超过一个的'/'只保留一个

大家试试
...全文
272 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
yjh1982 2005-02-21
  • 打赏
  • 举报
回复
更正:应该是std::unique(str,str+strlen(str)+1);

std::unique的实现速度很快,只要一层循环
joyself 2005-02-21
  • 打赏
  • 举报
回复
再来加一个,荷荷

#include <stdio.h>
int main(int argc, char *argv[])
{
char *p;
char *dst;
char *q;

dst = p = argv[1];


for (; *p; p++)
{
for (q = p + 1; (*q == *p) && (*p == '/'); q++);
strcpy(p + 1, q);
}
return dst;
}
Leaveye 2005-02-21
  • 打赏
  • 举报
回复
sigh。看到的晚就是吃亏。
sutra(经典答案 /*短最美 小最优*/) 的实现就是我想到的最简单的方法。
yjh1982 2005-02-21
  • 打赏
  • 举报
回复
#include<algorithm>
char *str= "/home/hello///test//end";


std::unique(str,str+strlen(str));
knocker_k 2005-02-21
  • 打赏
  • 举报
回复
char *str= "/home/hello///test//end";
char *ptr0,*ptr1;

ptr0 = ptr1 = str+1;
while( *ptr0 )
{
if( *ptr0 == '/'&&*(ptr1-1) == '/' ) ptr0++;

else

*ptr1++ = *ptr0++;
}
*ptr1 = '\0';

puts(str);
sutra 2005-02-21
  • 打赏
  • 举报
回复
void fun( char *str, char ch )
{
char *p, c ;
p = str ;
while( c = *p++ = *str++ )
while( c == ch && *str == ch ) ++str ;
} ;
int main()
{
char str[] = "/home/hello///test//end";
fun( str, '/' ) ;
puts( str ) ;
return 0 ;
}
suyouxin 2005-02-21
  • 打赏
  • 举报
回复
#include "stdio.h"

void main()
{
char a[]="/home/hello///test//end";

char *p = a;
char *b = a;
while (*p) {
if (p[0] == '/' && p[1] == '/') {
p++;
continue;
}
*b++ = *p++;
}
*b = '\0';
printf("%s\r\n", a);
}

测试过了
ineedu_cn 2005-02-20
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <string.h>

main()
{
char str1[100];
char*str2;
char*str2=(char*)malloc(100);
scanf("%s",str2);
bool a=false; //当字符为'/'时true,其余为false
int i=0,j=0;

//读取字符首先判断是否为'/',否则复制到str1中
//是则判断前一个是否为'/',否则复制到str1中,是则读取下一个字符
while(str2[j]!='\0'){
if(str2[j]!='/'){
str1[i++]=str2[j++];
a=false;
continue;
}
if(!a){
str1[i++]=str2[j++];
a=true;
continue;
}
j++;
}
str1[i]='\0';
printf("%s",str1);
return;
}

以上代码是我凭流程图写的,由于手里没有编译器,还没有调试过。
ineedu_cn 2005-02-20
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <string.h>

main()
{
char str1[100];
char*str2;
char*str2=(char*)malloc(100);
scanf("%s",str2);
bool a=false; //当字符为'/'时true,其余为false
int i=0,j=0;

//读取字符首先判断是否为'/',否则复制到str1中
//是则判断前一个是否为'/',否则复制到str1中,是则读取下一个字符
while(str2[j]!='\0'){
if(str2[j]!='/'){
str1[i++]=str[j++];
a=false;
continue;
}
if(!a){
str1[i++]=str[j++];
a=true;
continue;
}
j++;
}
str1[i]='\0';
printf("%s",str1);
return;
}
tttx 2005-02-20
  • 打赏
  • 举报
回复
改一下。。。。。。。

char a[]="/home/hello///test//end";
char b[sizeof(a)];

char *p=b;
for (i=0;i<sizeof(a);)
{
*p++=a[i];
if(a[i]!='/')
{i++;
continue;
}
while(a[++i]=='/');
}
wuzhihong 2005-02-19
  • 打赏
  • 举报
回复
#include <iostream>

using namespace std;

int main()
{
char ch;
int slash_cnt = 0;
while(cin.get( ch ))
{
if(ch=='/')
slash_cnt++;
else
slash_cnt=0;
if(slash_cnt<=1)
{
cout.put( ch );
}
}
return 0;
}
tttx 2005-02-19
  • 打赏
  • 举报
回复
char a[]="/home/hello///test//end";
char b[sizeof(a)];

char *p=b;
for (i=0;i<sizeof(a);i++)
{
*p++=a[i];
do{}while(a[i++]=='/');
}
Dong 2005-02-19
  • 打赏
  • 举报
回复
的确使用C++做比较容易
Dong 2005-02-19
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <string.h>
int main()
{
char s[40]="/home/hello///test//end";
char *c;
while(c = strstr(s,"//"))
{
s[c-s]=0;
sprintf(s,"%s%s", s,++c);
}
printf("%s\n",s);
return 0;
}
nodummy 2005-02-19
  • 打赏
  • 举报
回复
比较简单的方法可以使用两层的循环,一个进行迭代,一个用于去除多余的/

另外的方法可以使用std里面的算法来进行这些工作,譬如unique之类的
#include <iostream>
#include <algorithm>
using namespace std;
main()
{
char ptr[]="/home/hello///test//end";
unique(ptr,ptr+sizeof(ptr));
cout<<ptr;
}

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧