3

私は現在、次のコードを使用してテキスト ファイル内の各単語をスキャンし、それを変数に入れ、次の単語に移る前にいくつかの操作を行っています。これは正常に動作しますが、A-Z / a-z.たとえば"he5llo"入力された場合、出力を"hello". 変更できない場合fscanf、スキャンした変数に対してそれを行う方法はありますか? ありがとう。

while (fscanf(inputFile, "%s", x) == 1)
4

5 に答える 5

1

luser droog answer は機能しますが、私の意見では、必要以上に複雑です。

これを試すことができるあなたの簡単な例に従ってください:

while (fscanf(inputFile, "%[A-Za-z]", x) == 1) {   // read until find a non alpha character
   fscanf(inputFile, "%*[^A-Za-z]"))  // discard non alpha character and continue
}
于 2013-04-07T19:15:50.517 に答える
0

私は同様のプロジェクトに取り組んでいるので、あなたは大丈夫です!単語を別々の部分に分解します。

cin 各単語では空白は問題になりません。

 if( !isPunct(x) )

インデックスを 1 増やし、その新しい文字列を一時的な文字列ホルダーに追加します。配列のような文字列内の文字を選択できるため、これらの非アルファベット文字を見つけて新しい文字列を保存するのは簡単です。

 string x = "hell5o"     // loop through until you find a non-alpha & mark that pos
 for( i = 0; i <= pos-1; i++ )
                                    // store the different parts of the string
 string tempLeft = ...    // make loops up to and after the position of non-alpha character
 string tempRight = ... 
于 2013-04-07T17:03:28.737 に答える