空白と句読点を残しておきたいので、区切り記号を使用できないと思います。私はあなたの問題の解決策を持っており、コードからアイデアを得ることができると思います。
#include <iostream>
#include <cstring>
using namespace std;
#define MAX_ITEM_LENGTH 20
#define MAX_ITEM_COUNT 200
#define MAX_STRING_LENGTH 1000
char delimeters[] = {' ', ',', '.', '-'};
bool equals(char* str1, char* str2, int length1, int length2){
if(length1 != length2)
return false;
for(int i = 0; i < length1; i++)
if(toupper(str1[i]) == toupper(str2[i]))
return true;
return false;
}
int parse(char* str, char*** result){
int index = 0;
int totalCount = 0;
for(; totalCount < MAX_ITEM_COUNT && str[index] != '\0' ; totalCount++){
for (int resultIndex = 0 ; resultIndex < MAX_ITEM_LENGTH; resultIndex++){
if (resultIndex > 0 && strchr(delimeters,str[index])){
break;
}
(*result)[totalCount][resultIndex] = str[index];
index++;
if(strchr(delimeters, str[index-1]))
break;
}
}
return totalCount;
}
int removeDuplicates(char** items, int itemsLength, char*** result){
char* lastItem = new char[MAX_ITEM_LENGTH];
int index = 0;
for(int i = 0 ; i < itemsLength ; i++){
if(equals(items[i], lastItem, strlen(items[i]), strlen(lastItem))){
index--;
continue;
}
strcpy((*result)[index++], items[i]);
if(!strchr(delimeters, items[i][0])){
strcpy(lastItem, items[i]);
}
}
return index;
}
int main() {
char str[MAX_STRING_LENGTH] = "One one one of the most famous titles in the short story genre is a must-read. The story is about a young couple and how they meet the challenge of buying each other a Christmas gifts when they don't have enough money money money.";
char** items;
char** result;
items = new char*[MAX_ITEM_COUNT];
result = new char*[MAX_ITEM_COUNT];
for(int i = 0; i < MAX_ITEM_COUNT; i++){
items[i] = new char[MAX_ITEM_LENGTH];
result[i] = new char[MAX_ITEM_LENGTH];
}
int itemsLength = parse(str, &items);
int resultLength = removeDuplicates(items, itemsLength, &result);
for(int i = 0; i < resultLength; i++)
cout<<result[i];
return 0;
}
なぜCタイプとC関数だけを使用したいのですか?単語区切りと['strstr'](http://www.cplusplus.com/reference/cstring/strpbrk/)を見つけるには[' strpbrk'] strstr /)。 – Garf365