中去掉一个字符的所有出现在C字符串 - 例如需要 [英] remove all occurences of a character in C string - Example needed

查看:114
本文介绍了中去掉一个字符的所有出现在C字符串 - 例如需要的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

InputString:我不舒服,我们需要去看医生这将需要多长时间?

OutputString:我不舒服,我们需要去看医生它需要多长时间

该字符串需要清理炭的所有出现的。我能想到以下approacg的


  1. 使用,和strchr()函数查找第一次出现

  2. 将所有字符由一次位置离开的字符串中。

重复步骤1和2,直到和strchr()返回NULL指针。

我觉得这是解决这个问题非常低效的方式。我需要知道,如果有其他的方法来实现这一目标?伪code或实际code都将是AP preciated。


解决方案

 为(S = D = str中; * D = * S;!D + =(* S + =' ));

InputString: "I am unwell" "We need to go to the doctor" "How long will it take?".

OutputString: I am unwell We need to go to the doctor How long will it take?

The string needs to cleaned of all occurrences of the char " . I can think of the following approacg

  1. Use, strchr() function finding first occurrence of "
  2. Move all characters in the string left by once position.

Repeat steps 1 and 2 , until strchr() returns a NULL pointer.

I feel this is very inefficient way to approach this problem. I need to know , if there are other methods to achieve this? Pseudo code or actual code will both be appreciated.

解决方案

for (s=d=str;*d=*s;d+=(*s++!='"'));

这篇关于中去掉一个字符的所有出现在C字符串 - 例如需要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆