2016-11-19 4 views
-4

単語に文字の配列、あなたが特定の区切り文字に基づいて個々の単語に、この「のchar *テキスト」のような文字列を破ると、フォームに保存するにはどうすればよい私はこの問題に少しの助けを必要とし

を破ります"char * text []" strtok関数や "iostream"以外のライブラリを使用しません。

通常の状況では、char配列とstrtok関数の代わりに文字列を使用しますが、この状況では、私は単に許可されません。

おかげで、

更新: 私は擬似コード

words split(line, delims) 
{ 
    nb_words = cound_words(line); 
    words = allocate_words(nb_words + 1); // words is a array of pointer 
    i = 0 
    j = 0 
    while true 
    { 
     while line[i] in delims // we transform every delims into a end string 
     { 
      line[i] = end_string 
      i++ 
     } 
     if line[i] not end_string 
     { 
      words[j] = line + i // we stock the address of line[i] 
      j++ 
      while line[i] not in delims and line[i] not end_string 
      { 
       i++ 
      } 
     } 
     else 
     { 
      words[j] = NULL // we end the array by NULL pointer 
      return words 
     } 
    } 
} 

が同様のループを使用しcount_wordここ

#include <iostream> 
#include <fstream> 
//#define MAX_CHARS_PER_LINE = 512; 
//#define MAX_TOKENS_PER_LINE = 5; 
using namespace std; 
char stringToken(char* input_string); 
int main(int argc, char* argv[]) 
{ 
    char input_string[512]; 
    ifstream infile; 
    infile.open(argv[1]); 
    while(!infile.eof()) 
    { 
     infile.getline(input_string, 512); 
     cout << "Main line: " << input_string << endl; 
     stringToken(input_string); 
    } 
    infile.close(); 
    return 0; 
} 
char stringToken(char* input_string) 
{ 
    //char* word; 
    //cout << "String token function: " << input_string << endl; 
    /*while(input_string >> word) 
    { 
     cout << word << endl; 
    }*/ 

    char *tempone; 
    char *temptwo[5]; 

    int ii=0, 
     jj=0; 
    while(input_string[ii] != '\0' && jj<5) 
    { 
     if((int)input_string[ii]!= 32 && (int)input_string[ii]!= 9 && (int)input_string[ii] != 44) 
     { 
      tempone[ii]=input_string[ii]; 
      //cout << "\n\nindiv char" << input_string[ii] << "\t\t" << (int)input_string[ii] << "\n\n"; 
     } 
     else 
     { 
      temptwo[jj]=tempone; 
      jj++; 
      //testing 
      cout << temptwo << endl; 
     } 
     ii++; 

    } 



    return 0; 
} 
+0

C++で何かを得るのは少し難しいです。つまり、たとえば 'strtok'を使用します。 – nbro

+0

私はそれが難しいことを知っているが、あなたは[検索](http://stackoverflow.com/questions/236129/split-a-string-in-c)することができます。または、あなたが試したことを示す。あなたは来て、ちょうど「私の仕事をしてください!」と言います。 – Stargateur

+0

@Stargateur私はあなたの批評を理解していますが、私はしばらく検索しており、見つけたものはすべて文字列やベクターライブラリの使用が必要です。 –

答えて

0

をしようとしているものが含まれています。私はそれを見つけることができます。このアルゴリズムの目的は、行を複数の単語に変換することです。だから、あなたが言葉を使うほど長い間生きていなければなりません。

+0

ありがとう! "nb_words"の種類は何ですか? –

+0

@VictorL nb_wordsは、行数の単語数を**カウントするためのものです。そして配列を割り当てるのに使うよりも。彼のタイプは[new operator](http://www.cplusplus.com/reference/new/operator%20new/)の議論と同じです。 – Stargateur

関連する問題