我需要帮助从字符数组中删除空格和特殊字符。我一直遇到的问题是,擦除函数只适用于字符串数据类型,如果我没有弄错。该分配要求char数组不是一个字符串,所以我不能转换它或有一个字符串变量。我已经尝试过查找它,但所有类型都只是建议将其转换为字符串,或者以字符串开头,这是我无法做到的。我是编程新手,指针对我来说有点奇怪,所以如果答案很明显,我很抱歉。感谢提供给你们所有人的帮助。使用指针从字符数组[]中删除/删除字符
#include "stdafx.h"
#include <iostream>
#include <stdio.h>
#include <string>
#include <stdlib.h>
#include <ctype.h>
using namespace std;
int main()
{
const int SIZE = 80;
char str[SIZE];
char* strPtr;
int strlength;
int j = 0;
int front = 0, back; int flag = 1;
strPtr = str;
cout << "This program checks for palidromes." << endl;
cout << "Please enter a phrase." << endl;
cout << endl;
cin.getline(str, SIZE);
//Get the length of string str
strlength = strlen(str);
//Convert all the characters in the string str to uppercase
for (int i = 0; i < strlength; i++)
{
if (!isupper(str[i]))
str[i] = toupper(str[i]);
}
//Remove all the special characters except letters a - z
for (int i = 0; i < strlength; i++)
if (!isalpha(str[i])||(!isalnum(str[i])))
{
str.erase(str[i]); // need help here. Not sure how to go about it.
}
//str[j] = '\0';
return 0;
}
我其实喜欢在这里使用lambda。 +1 –