删除重复字符并将其放在c++; 我试图在C++中添加标题,但没弄清楚。 这是我试过的 char *str=new char[100]; cout<<"enter the string"; cin.getline(str,100); int len=strlen(str); int i=0; //iterating through the string while(i<len-1) { int signal=0; char x; int j=i+1; while(j<len) { if(str[j]==str[i]) { x=str[i]; for(int k=j;k<len;k++) { str[k]=str[k+1]; } len--; signal=1; } else { j++; } } if(signal==1) { for(int l=i;l<len;l++) { str[l]=str[l+1]; } str[len-1]=x; } } cout<<str; char*str=新字符[100]; cout
使用std::string更容易:删除重复字符并将其放在c++; 我试图在C++中添加标题,但没弄清楚。 这是我试过的 char *str=new char[100]; cout<<"enter the string"; cin.getline(str,100); int len=strlen(str); int i=0; //iterating through the string while(i<len-1) { int signal=0; char x; int j=i+1; while(j<len) { if(str[j]==str[i]) { x=str[i]; for(int k=j;k<len;k++) { str[k]=str[k+1]; } len--; signal=1; } else { j++; } } if(signal==1) { for(int l=i;l<len;l++) { str[l]=str[l+1]; } str[len-1]=x; } } cout<<str; char*str=新字符[100]; cout,c++,string,C++,String,使用std::string更容易: std::string input("Darren"); std::string uniqueCharsString, repeatedCharsString; for (int i = 0; i < input.length(); ++i) { // If we are looking at the last character, we need to compare it with the p
std::string input("Darren");
std::string uniqueCharsString, repeatedCharsString;
for (int i = 0; i < input.length(); ++i)
{
// If we are looking at the last character, we need to compare it with the previous one
if (i == input.length() - 1)
{
// If the character is the same as the previous one, add it to the repeatedCharsString
if (input.at(i) == input.at(i-1))
repeatedCharsString.push_back(input.at(i));
// If the character is not the same as the previous one, add it to the uniqueCharsString
else
uniqueCharsString.push_back(input.at(i));
}
// If the character is the same as the following one, add it to the repeatedCharsString
else if (input.at(i) == input.at(i+1))
{
repeatedCharsString.push_back(input.at(i));
// Skip the next character since we already know it's the same as the previous one
i++;
}
// If the character is not the same as the following one, add it to the uniqueCharsString
else
uniqueCharsString.push_back(input.at(i));
}
std::string result(uniqueCharsString + repeatedCharsString);
std::字符串输入(“Darren”);
std::字符串uniqueCharsString,repeatedCharsString;
对于(int i=0;i
字符串比较区分大小写
希望这有帮助 重复字符是什么意思?具有相同值的两个连续字符?以前已出现在字符串中任何位置的字符?你需要保留它们吗?你需要保持这些字符串的相对位置吗?@chris它会对功能进行任何更改吗?@bhawin,它很好地集成到C字符串中,而且绝对更安全、更简单。@david例如,“darren”是输入字符串,而“daenr”应该是output@bhawin:路演怎么样?这会保持原样,还是转变成“拉德什沃”?花点时间完整地说明问题,并举例说明任何潜在用途。