免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
1234下一页
最近访问板块 发新帖
查看: 11232 | 回复: 35
打印 上一主题 下一主题

[C] How to del a character from a string? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-04-08 10:53 |只看该作者 |倒序浏览
For example , char str[50] ="I like XH,but she doesn't like me";

                        char del       =  'e';

After   executing the code, the result str should   not contain character 'e',

that is to say, str should be modifyed to"I lik XH,but sh dosn't lik m"

I write a solution,but  it  requires  temporary  memory.

Any suggestion  to improve it?

Thx

论坛徽章:
0
2 [报告]
发表于 2008-04-08 10:55 |只看该作者
BTW,how to type Chinese in ubuntu?

I've installed the Chinese version,but I cann't type Chinese .

论坛徽章:
0
3 [报告]
发表于 2008-04-08 10:57 |只看该作者
//My code :

#include <stdio.h>
#include <string.h>

void DeleteChar(char* str,char* ch){

char    str_temp[50];

str_temp[0] = '\0';

char*   substr =  str;

char*   delpos = NULL;

    while(   (delpos=strstr(substr,ch))!=NULL  ){

            *delpos = '\0';

           strcat(str_temp,substr);

            substr  =  delpos+1;

    }

          strcat(str_temp,substr);

          strncpy(str,str_temp,50);

}


int main()
{

char str[50]="I like XH,but she doesn't like me";

char  *str_del = "e";

DeleteChar(str,str_del);

printf("%s\n",str);

return 1;
}

论坛徽章:
0
4 [报告]
发表于 2008-04-08 10:59 |只看该作者
c = 'e';
cp = strchr(str, c);
if(cp)
    strcpy(cp, cp+1);

论坛徽章:
0
5 [报告]
发表于 2008-04-08 11:08 |只看该作者
原帖由 思一克 于 2008-4-8 10:59 发表
c = 'e';
cp = strchr(str, c);
if(cp)
    strcpy(cp, cp+1);

好方法!

论坛徽章:
0
6 [报告]
发表于 2008-04-08 11:19 |只看该作者

回复 #4 思一克 的帖子

I am sorry ,but the strcpy function parameter ,dest and src string ,are  not allowed to overlap.

论坛徽章:
0
7 [报告]
发表于 2008-04-08 11:20 |只看该作者
原帖由 思一克 于 2008-4-8 10:59 发表
c = 'e';
cp = strchr(str, c);
if(cp)
    strcpy(cp, cp+1);


I am sorry ,but the strcpy function parameter ,dest and src string ,are  not allowed to overlap.

论坛徽章:
0
8 [报告]
发表于 2008-04-08 11:35 |只看该作者
原帖由 wilbur8415 于 2008-4-8 11:20 发表


I am sorry ,but the strcpy function parameter ,dest and src string ,are  not allowed to overlap.

use memmove to instead.

论坛徽章:
26
处女座
日期:2016-04-18 14:00:4515-16赛季CBA联赛之深圳
日期:2020-06-02 10:10:5015-16赛季CBA联赛之广夏
日期:2019-07-23 16:59:452016科比退役纪念章
日期:2019-06-26 16:59:1315-16赛季CBA联赛之天津
日期:2019-05-28 14:25:1915-16赛季CBA联赛之青岛
日期:2019-05-16 10:14:082016科比退役纪念章
日期:2019-01-11 14:44:062016科比退役纪念章
日期:2018-07-18 16:17:4015-16赛季CBA联赛之上海
日期:2017-08-22 18:18:5515-16赛季CBA联赛之江苏
日期:2017-08-04 17:00:4715-16赛季CBA联赛之佛山
日期:2017-02-20 18:21:1315-16赛季CBA联赛之天津
日期:2016-12-12 10:44:23
9 [报告]
发表于 2008-04-08 12:04 |只看该作者
shit!

Can you speak chinese ?

论坛徽章:
0
10 [报告]
发表于 2008-04-08 12:13 |只看该作者
能不能overlap, 你自己不会实验一下吗?

那么简单的程序.


原帖由 放驴娃 于 2008-4-8 11:35 发表

use memmove to instead.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP