您的位置:首页 > 编程语言 > C语言/C++

C和C++字符串删除等特定操作处理

2017-10-07 12:39 429 查看
1 C字符串删除特定字符,实际上利用了函数重新修改了特定地址上的字符串,而且是按照添加的方式修改的!

方法二

在一个函数里面实现,但是要new char新开辟一块地址;

int main()

 {

     char str[] = "we are stu!";

     char *zcl = new char;

     char *cxl = zcl;

     int len = strlen(str);

     for (int i = 0; i < len; i++)

     {

         if (str[i] == 'w' || str[i] == 'e')

         {

             zcl = zcl;

         }

         else

         {

             *zcl = str[i];

             zcl++;

         }

     }

     *zcl = '\0';

     cout << cxl << endl;

    

 

     system("pause");

     return 0;

 }

方法三,沿用之前地址上的字符串,但是不能再跟方法二一样用for循环了,因为循环的index已经发生了改变!

 int main()

 {

     char str[] = "we are stu!";

     char *zcl = str;

     char *cxl = zcl;

     int len = strlen(str);

     int i = 0;

     while (str[i]!='\0')

     {

         if (str[i] == 'w' || str[i] == 'e')

         {

             zcl = zcl;

             i++;

         }

         else

         {

             *zcl = str[i];

             zcl++;

             i++;

         }

     }

     *zcl = '\0';

     cout << cxl << endl;

    

 

     system("pause");

     return 0;

 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: