在我的程序中,我正在检查整个cstring,如果找到任何空格或标点符号,只需将空字符添加到该位置,但是complilor给出了一个错误:空字符常量。
请帮帮我,在我的循环中,我正在检查这个
if(ispunct(str1[start])) {
str1[start]=''; // << empty character constant.
}
if(isspace(str1[start])) {
str1[start]=''; // << empty character constant.
}
这是我的错误,请纠正我。
例如单词str,, ing,
输出应为string
。
答案 0 :(得分:0)
没有空角色。
如果您指的是空格,请将''
更改为' '
(其中有空格)。
如果您的意思是NUL
,请将其更改为'\0'
。
答案 1 :(得分:0)
编辑:由于OP已编辑问题,答案已不再相关。为了子孙后代而离开。
如果您想添加空字符,请使用'\ 0'。如果您想要使用不同的角色,请使用适当的角色。你什么都不能分配它。那毫无意义。这就像说
int myHexInt = 0x;
或
long long myIndeger = L;
编译器会出错。放入你想要的价值。在char情况下,这是一个从0到255的值。
答案 2 :(得分:0)
更新:
从编辑到OP的问题,显然他/她想修剪一串标点符号和空格字符。
如标记的可能重复中所述,一种方法是使用remove_copy_if
:
string test = "THisisa test;;';';';";
string temp, finalresult;
remove_copy_if(test.begin(), test.end(), std::back_inserter(temp), ptr_fun<int, int>(&ispunct));
remove_copy_if(temp.begin(), temp.end(), std::back_inserter(finalresult), ptr_fun<int, int>(&isspace));
ORIGINAL
检查你的问题,用空格替换空格是多余的,所以你真的需要找出如何用空格替换标点符号。您可以使用比较函数(通过将std::ispunct
包装)与STL中的std::replace_if
一起使用来执行此操作:
#include <string>
#include <algorithm>
#include <iostream>
#include <cctype>
using namespace std;
bool is_punct(const char& c) {
return ispunct(c);
}
int main() {
string test = "THisisa test;;';';';";
char test2[] = "THisisa test;;';';'; another";
size_t size = sizeof(test2)/sizeof(test2[0]);
replace_if(test.begin(), test.end(), is_punct, ' ');//for C++ strings
replace_if(&test2[0], &test2[size-1], is_punct, ' ');//for c-strings
cout << test << endl;
cout << test2 << endl;
}
输出:
THisisa test
THisisa test another
答案 3 :(得分:-1)
尝试此操作(正如您明确要求cstring
):
char str1[100] = "str,, ing";
if(ispunct(str1[start]) || isspace(str1[start])) {
strncpy(str1 + start, str1 + start + 1, strlen(str1) - start + 1);
}
好吧,用纯c语言做这件事,有更有效的解决方案(详情请看@MichaelPlotke的答案)。
但是,当您明确要求c++时,我建议采用以下解决方案:
注意您可以使用standard c++ algorithms作为'普通'c风格的字符数组。您只需将要删除的谓词条件放入一个小助手仿函数中,并将其与std::remove_if()
算法一起使用:
struct is_char_category_in_question {
bool operator()(const char& c) const;
};
后来就像使用它一样:
#include <string>
#include <algorithm>
#include <iostream>
#include <cctype>
#include <cstring>
// Best chance to have the predicate elided to be inlined, when writing
// the functor like this:
struct is_char_category_in_question {
bool operator()(const char& c) const {
return std::ispunct(c) || std::isspace(c);
}
};
int main() {
static char str1[100] = "str,, ing";
size_t size = strlen(str1);
// Using std::remove_if() is likely to provide the best balance from perfor-
// mance and code size efficiency you can expect from your compiler
// implementation.
std::remove_if(&str1[0], &str1[size + 1], is_char_category_in_question());
// Regarding specification of the range definitions end of the above state-
// ment, note we have to add 1 to the strlen() calculated size, to catch the
// closing `\0` character of the c-style string being copied correctly and
// terminate the result as well!
std::cout << str1 << endl; // Prints: string
}
请参阅此可编辑和工作样本here。
答案 4 :(得分:-1)
由于我不喜欢接受的答案,这是我的:
#include <stdio.h>
#include <string.h>
#include <cctype>
int main() {
char str[100] = "str,, ing";
int bad = 0;
int cur = 0;
while (str[cur] != '\0') {
if (bad < cur && !ispunct(str[cur]) && !isspace(str[cur])) {
str[bad] = str[cur];
}
if (ispunct(str[cur]) || isspace(str[cur])) {
cur++;
}
else {
cur++;
bad++;
}
}
str[bad] = '\0';
fprintf(stdout, "cur = %d; bad = %d; str = %s\n", cur, bad, str);
return 0;
}
哪个输出cur = 18; bad = 14; str = string
这样做的好处是更高效,更易读,嗯,好吧,我喜欢的风格更好(参见冗长辩论/解释的评论)。