【发布时间】:2016-08-08 01:04:09
【问题描述】:
我需要帮助从 char 数组中删除空格和特殊字符。我一直遇到的问题是,如果我没记错的话,擦除函数只适用于字符串数据类型。赋值需要一个 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;
}
【问题讨论】:
标签: c++ arrays pointers visual-c++ char