首页 > 试题广场 >

编写一个函数,其原型如下: int replace (ch

[问答题]
编写一个函数,其原型如下:
int replace (char * str, char c1, char c2);
该函数将字符串中所有的c1都替换为c2,并返回替换次数。
推荐
int replace (char * str, char c1, char c2)
{
     int count =  0;
     while (*str)       // while not at end of string
     {
           if (*str == c1)
           {
              *str = c2;
              count++;
            }
            str++;          // advance to next character
       }
       return count;
}

发表于 2018-05-07 21:28:31 回复(0)