社区
C语言
帖子详情
如何最简单的编程去掉字符串中多余的 /
joyself
2005-02-19 08:58:56
譬如 /home/hello///test//end
将该字符串变为 /home/hello/test/end
即超过一个的'/'只保留一个
大家试试
...全文
292
15
打赏
收藏
如何最简单的编程去掉字符串中多余的 /
譬如 /home/hello///test//end 将该字符串变为 /home/hello/test/end 即超过一个的'/'只保留一个 大家试试
复制链接
扫一扫
分享
转发到动态
举报
AI
作业
写回复
配置赞助广告
用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;
}
python清除
字符串
中
间空格的实例讲解
在本篇文章
中
,我们将通过实例讲解如何使用Python去除
字符串
中
的
多余
空格,具体涉及到
字符串
内置函数和正则表达式的使用方法。 首先,我们来介绍一个
简单
但不够高效的方法:使用
字符串
的replace方法。replace方法是...
PHP
中
用正则表达式清除
字符串
的空白
=\s)'去除
字符串
中
多余
的空白字符,将它们压缩为一个。最后,再次使用preg_replace()函数和正则表达式'[\n\r\t]'替换掉
字符串
中
所有的换行符、回车符和制表符,把它们转换成一个普通的空格字符。 整合上述方法,...
C#删除最后一个结尾逗号的方法
这种方法在实际开发
中
非常有用,例如当你需要将数组或列表连接成一个由逗号分隔的
字符串
时,可以在连接完成后调用此方法来去除
多余
的逗号。例如,对于一个整数数组: ```csharp int[] numbers = { 1, 2, 3, 4, 5 };...
张志晨老师之《则表达式去空格》
除了去除
字符串
首尾的空格外,有时候还需要屏蔽掉输入
中
的空格和换行符。这可以通过修改正则表达式实现: ```javascript var str = " Hello \nWorld! "; str = str.replace(/[\s\n]/g, ""); console.log(str); // ...
简单
的用js实现过滤
多余
字符的正则表达式
本文将介绍如何使用JavaScript结合正则表达式来过滤掉
字符串
中
的
多余
字符,特别是去除
字符串
中
的重复字符。 首先,需要明确什么构成“
多余
字符”。在这个上下文
中
,我们可以认为“
多余
字符”是指在
字符串
中
出现一次...
C语言
70,024
社区成员
243,263
社区内容
发帖
与我相关
我的任务
C语言
C语言相关问题讨论
复制链接
扫一扫
分享
社区描述
C语言相关问题讨论
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章