算法:只将 srcStr字符串中的 非标点字符 赋给destStr.
/*去掉字符串中的标点,字符指针操作*/
#include <iostream>
#include <ctype.h>
#include <assert.h>
using namespace std;
char *delPunct(char *destStr, const char *srcStr)
{
// assert(destStr != NULL && srcStr != NULL);
while (*srcStr) {
if (ispunct(*srcStr)) {
NULL;
} else {
*destStr++ = *srcStr;
}
srcStr++;
}
*destStr = '\0';
return destStr;
}
int main()
{
char srcStr[30] = " hello,world. string !";
char destStr[30];
delPunct(destStr, srcStr);
cout << destStr;
return 0;
}
/*去掉字符串中的标点,字符数组操作*/
#include <iostream>
#include <ctype.h>
#include <assert.h>
using namespace std;
int main()
{
char srcStr[30] = " hello,world. string !";
char destStr[30];
int j = 0;
for (int i=0;srcStr[i]!=0;i++) {
if (ispunct(srcStr[i])) {
NULL;
} else {
destStr[j] = srcStr[i];
j++;
}
}
destStr[j] = '\0';
cout << destStr;
return 0;
}