공백 검사기를 만들어 공백이나 특수 문자를 무시합니다. 아래는 내 기능의 일부입니다. 그것이하는 일은 인자로 c 문자열을 취한 다음, orignal에서 공백과 특수 문자를 제거하기 위해 또 다른 c 문자열을 생성합니다. 두 번째 C 문자열을 출력 할 때 여전히 공백이나 특수 문자가 포함됩니다. 어떤 사람들은 왜 그렇게하고 있는지 설명 할 수 있습니까? 감사합니다C 문자열에서 공백과 특수 문자를 제거하므로
bool isPalindrome(char *line)
{
//variables
bool palindrome = true;
int length = strlen(line);
int count = 0;
//copy line to line2 with no spaces and no punctuation
char *line2 = new char[length + 1];
int count2 = 0;
for(int i = 0; i < length; i++)
{
if(line[i] != ' ' && ispunct(line[i]) == false)
{
line2[count2] = line[i];
count2 ++;
}
}
for(int i = 0; i < count2; i++)
cout << line[i];
line2[length] = '\0';
http://stackoverflow.com/questions/83439/remove-spaces-from -stdstring-in-c –