开发者

filtering a string in c

开发者 https://www.devze.com 2023-02-17 19:45 出处:网络
Given the input ~Zw~~\"iE^L I should get !w~\"iE^L inste开发者_如何学运维ad I am getting Zw~\"iE^L

Given the input ~Zw~~"iE^L I should get !w~"iE^L inste开发者_如何学运维ad I am getting Zw~"iE^L

So it catch the ~~ just fine and replace it with ~ but I want to replace ~Z with !

Any ideas?

  for (j = 0; j < dataCharCount; j++, k++)
  {
    if (inputData[j] == '~' && inputData[j + 1] == '~')
    {
      filteredInputData[k] = '~';
      j++;
    }
    else if (inputData[j] == '~' && inputData[j + 1] == 'Z')
    {
      filteredInputData[k] = '!';
      j++;

    }
    filteredInputData[k] = inputData[j];
  }


} else {
    filteredInputData[k] = inputData[j];
}

Without the else you're overwriting filteredInputData[k] after the if statements.


there is an else missing before the last ine of the function. you surely do not want to copy the input to the output (filteredInputData[k] = inputData[j];) after having performed the replacement.


The last statement in your loop overwrites the ~ or ! you wrote via your if statment body. The reason it looks like it works for the ~~ is that it's overwriting the ~ with another ~. In the ~Z case, you're overwriting your ! with the Z. Step through your code with a debugger - you'll see what's happening immediately.

0

精彩评论

暂无评论...
验证码 换一张
取 消