C++ 将连续重复字符替换为其单一出现
我正在读取URL(即字符串)并搜索模式(连续出现的C++ 将连续重复字符替换为其单一出现,c++,c,C++,C,我正在读取URL(即字符串)并搜索模式(连续出现的/字符)。如果我找到一个匹配的模式,我想用一个/替换它,并复制其余的字符。例如:如果输入字符串为http://www.yahoo.com/,我需要通过删除额外的/来生成输出http:/www.yahoo.com/,因为该字符连续出现两次 节目如下: int main() { int i, j; bool found = false; unsigned char *str = "http://www.yahoo.com/";
/
字符)。如果我找到一个匹配的模式,我想用一个/
替换它,并复制其余的字符。例如:如果输入字符串为http://www.yahoo.com/
,我需要通过删除额外的/
来生成输出http:/www.yahoo.com/
,因为该字符连续出现两次
节目如下:
int main() {
int i, j;
bool found = false;
unsigned char *str = "http://www.yahoo.com/";
int len = strlen(str);
for (i = 0; i < len - 1; i++) {
if ((str[i] == '/') && (str[i + 1] == '/')) {
found = true;
break;
}
}
if (found) {
for (j = i + 1; j <= (len - i - 2); j++) {
str[j] = str[j + 1];
}
}
return 0;
}
intmain(){
int i,j;
bool-found=false;
无符号字符*str=”http://www.yahoo.com/";
int len=strlen(str);
对于(i=0;i 对于(j= i+1;j),您不可以更改字符串文字。它们在C和C++中是不可修改的。根据C标准(64.5字符串文字)
7未说明这些数组是否不同,前提是它们的
元素具有适当的值。如果程序尝试
修改这样一个数组,行为是未定义的。
使用标准C函数strstr
可以轻松完成任务
char s[] = "http://www.yahoo.com/";
puts(s);
char *p = strstr(s, "//");
if (p) memmove(p, p + 1, strlen(s) - (p - s));
puts(s);
代码段的输出如下所示
http://www.yahoo.com/
http:/www.yahoo.com/
至于你的程序,除了试图改变字符串文字外,这个循环是错误的
if (found) {
for(j = i + 1; j <= (len - i - 2); j++) {
str[j] = str[j + 1];
}
}
if(已找到){
对于(j= i+1;j),您不可以更改字符串文字。它们在C和C++中是不可修改的。根据C标准(64.5字符串文字)
7未说明这些数组是否不同,前提是它们的
元素具有适当的值。如果程序尝试
修改这样一个数组,行为是未定义的。
使用标准C函数strstr
可以轻松完成任务
char s[] = "http://www.yahoo.com/";
puts(s);
char *p = strstr(s, "//");
if (p) memmove(p, p + 1, strlen(s) - (p - s));
puts(s);
代码段的输出如下所示
http://www.yahoo.com/
http:/www.yahoo.com/
至于你的程序,除了试图改变字符串文字外,这个循环是错误的
if (found) {
for(j = i + 1; j <= (len - i - 2); j++) {
str[j] = str[j + 1];
}
}
if(已找到){
对于(j=i+1;j而言,您正在一个字符串文本上操作,该字符串是只读内存。当您尝试修改字符时,会出现错误
将字符串数据复制到可写内存中,然后可以对其进行修改
最简单的更改是这样做:
unsigned char *str = "http://www.yahoo.com/";
请改为:
char str[] = "http://www.yahoo.com/";
<>但是,对于C++,你应该使用一个<代码> STD::String ,然后你可以使用标准的搜索算法,比如:
#include <string>
int main() {
std::string str = "http://www.yahoo.com/";
std::string::size_type i = 0;
do {
i = str.find("//", i);
if (i == std::string::npos) break;
str.erase(i, 1);
}
while (!str.empty());
return 0;
}
#包括
int main(){
std::string str=”http://www.yahoo.com/";
std::string::size_type i=0;
做{
i=str.find(“/”,i);
如果(i==std::string::npos)中断;
str.erase(i,1);
}
而(!str.empty());
返回0;
}
或者:
#include <string>
#include <algorithm>
bool isBackslashPair(const char c1, const char c2) {
return ((c1 == '/') && (c2 == '/'));
}
int main() {
std::string str = "http://www.yahoo.com/";
std::string::iterator iter = str.begin();
do {
iter = std::adjacent_find(iter, str.end(), isBackslashPair);
if (iter == std::string::end()) break;
iter = str.erase(iter);
}
while (!str.empty());
return 0;
}
#包括
#包括
bool反斜杠对(常量字符c1,常量字符c2){
返回((c1=='/')和&(c2=='/'));
}
int main(){
std::string str=”http://www.yahoo.com/";
std::string::iterator iter=str.begin();
做{
iter=std::相邻查找(iter,str.end(),isBackslashPair);
如果(iter==std::string::end())中断;
iter=str.erase(iter);
}
而(!str.empty());
返回0;
}
您正在一个字符串文本上操作,该文本是只读内存。当您试图修改字符时,会出现错误
将字符串数据复制到可写内存中,然后可以对其进行修改
最简单的更改是这样做:
unsigned char *str = "http://www.yahoo.com/";
请改为:
char str[] = "http://www.yahoo.com/";
<>但是,对于C++,你应该使用一个<代码> STD::String ,然后你可以使用标准的搜索算法,比如:
#include <string>
int main() {
std::string str = "http://www.yahoo.com/";
std::string::size_type i = 0;
do {
i = str.find("//", i);
if (i == std::string::npos) break;
str.erase(i, 1);
}
while (!str.empty());
return 0;
}
#包括
int main(){
std::string str=”http://www.yahoo.com/";
std::string::size_type i=0;
做{
i=str.find(“/”,i);
如果(i==std::string::npos)中断;
str.erase(i,1);
}
而(!str.empty());
返回0;
}
或者:
#include <string>
#include <algorithm>
bool isBackslashPair(const char c1, const char c2) {
return ((c1 == '/') && (c2 == '/'));
}
int main() {
std::string str = "http://www.yahoo.com/";
std::string::iterator iter = str.begin();
do {
iter = std::adjacent_find(iter, str.end(), isBackslashPair);
if (iter == std::string::end()) break;
iter = str.erase(iter);
}
while (!str.empty());
return 0;
}
#包括
#包括
bool反斜杠对(常量字符c1,常量字符c2){
返回((c1=='/')和&(c2=='/'));
}
int main(){
std::string str=”http://www.yahoo.com/";
std::string::iterator iter=str.begin();
做{
iter=std::相邻查找(iter,str.end(),isBackslashPair);
如果(iter==std::string::end())中断;
iter=str.erase(iter);
}
而(!str.empty());
返回0;
}
@tinga您不能更改字符串文字。另外,标准函数strstr有助于快速查找字符串“/”这是一个奇怪的例子,将一个有效的URI转换为无效的URI。可能重复的@tinga您可能不会更改字符串文字。还有一个标准函数strstr可以帮助快速找到字符串“/”。这是一个奇怪的例子,将一个有效的URI转换为无效的URI。可能重复的