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;
}
方法二
在一个函数里面实现,但是要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;
}
相关文章推荐
- 删除特定位置前面的字符串c++代码实例及运行结果
- C++中对字符串进行插入、替换、删除操作
- 【字符串处理算法】删除特定的字符的算法设计及C代码实现
- 删除特定位置前面的字符串c++代码实例及运行结果
- C++学习笔记——erase删除字符串中特定字符 由leetcode383题想到的
- C++删除字符串中特定一个或多个字符
- 删除特定位置前面的字符串c++代码实例及运行结果
- 删除特定位置前面的字符串c++代码实例及运行结果
- 删除特定位置前面的字符串c++代码实例及运行结果
- 删除特定位置前面的字符串c++代码实例及运行结果
- C++学习记录11--字符串中删除特定元素
- c++字符串处理操作
- c++字符串的插入与删除操作
- 删除特定位置前面的字符串c++代码实例及运行结果
- 删除特定位置前面的字符串c++代码实例及运行结果
- C++删除字符串中特定的字符
- 删除特定位置前面的字符串c++代码实例及运行结果
- 删除特定位置前面的字符串c++代码实例及运行结果
- 【STL】C++使用STL处理替换字符串操作。
- C++中对字符串进行插入、替换、删除操作