2016-11-29 5 views
0

文字列が重複する文字列がありますが、文字列ライブラリではなくcstringライブラリのみを使用して削除します。文字配列内の単語を選択し、それを1つずつ比較します。

*****サンプルテキスト短編小説のジャンルで最も有名なタイトルの******

つ1 1は必読です。物語は若いカップルと彼らがお金のお金を十分に持っていないときに彼らはお互いにクリスマスプレゼントを買うという挑戦をどのように満たしているのですか?短編小説のジャンルで最も有名なタイトルの******

一つ

*****編集したテキストは必読です。この話は、若いカップルと、お金が、お金がと不足しているときに、お互いにクリスマスプレゼントを買うという挑戦をどのようにして満たしているかです。

私は、テキストを大文字に変換し、その文字列を大文字に変換しました。

char str[100]; 

しかし、どのように単語を取得して1つずつ比較できますか?その中には句読点も含まれています。 「お金お金」のように。それも重複しています。

+1

なぜCタイプとC関数だけを使用したいのですか?単語区切りと['strstr'](http://www.cplusplus.com/reference/cstring/strpbrk/)を見つけるには[' strpbrk'] strstr /)。 – Garf365

答えて

2

空白と句読点を残しておきたいので、区切り記号を使用できないと思います。私はあなたの問題の解決策を持っており、コードからアイデアを得ることができると思います。

#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; 
} 
+0

ありがとう、あなたはコードに慣れていません。 parse()で、なぜ "if(strchr(delimeters、str [index-1]))break;"適用する必要がありますか? – Harry2046

+0

str [index-1](たとえば '、'文字)がdelimeter文字列(この場合はtrue)に存在する場合、strchrはゼロ以外の数値を返します。 「配列」のような文字列がある場合は、「」で止める必要があり、単語には含めないでください。このコントロールでは、{array、 "、"} {array、}}ではなく "array"を解析します。 – cokceken

+0

もう一度ありがとうございます。しかし、私はいくつかの有線のものを見つけました。あなたは時には(* result)[resultCount] [resultIndex] = str [index];を使用します。 strcpy((* result)[index ++]、items [i])を使用することもあります。 ? – Harry2046

関連する問題