2

整数値の文字列があります。例 20 200 2000 21 1

最初の単語 (この場合は 20) を削除したい。これを行う方法はありますか?

私は次のようなものを使用することを考えました...

sscanf(str, "/*somehow put first word here*/ %s", str);
4

4 に答える 4

5

どうですか

char *newStr = str;
while (*newStr != 0 && *(newStr++) != ' ') {}
于 2012-10-23T17:04:53.233 に答える
4

をそのまま使用できますstrchr()。これは最初のスペースの後の部分文字列に設定さstrれます。スペースがない場合はそのままにしておきます。

char *tmp = strchr(str, ' ');
if(tmp != NULL)
    str = tmp + 1;
于 2012-10-23T17:10:32.153 に答える
1

次のように、すべての文字を最初のスペースまでスキップしてから、スペース自体をスキップできます。

char *orig = "20 200 2000 21 1";
char *res;
// Skip to first space
for (res = orig ; *res && *res != ' ' ; res++)
    ;
// If we found a space, skip it too:
if (*res) res++;

このコード フラグメントは出力します200 2000 21 1( ideone へのリンク)。

于 2012-10-23T17:07:55.313 に答える
0

最も簡単でエレガントな方法は、そのようなことをすることであることがわかりました

   int garbageInt;
   sscanf(str, "%d %[^\n]\n",&garbageInt,str);
于 2012-10-23T20:18:35.523 に答える