整数値の文字列があります。例 20 200 2000 21 1
最初の単語 (この場合は 20) を削除したい。これを行う方法はありますか?
私は次のようなものを使用することを考えました...
sscanf(str, "/*somehow put first word here*/ %s", str);
整数値の文字列があります。例 20 200 2000 21 1
最初の単語 (この場合は 20) を削除したい。これを行う方法はありますか?
私は次のようなものを使用することを考えました...
sscanf(str, "/*somehow put first word here*/ %s", str);
どうですか
char *newStr = str;
while (*newStr != 0 && *(newStr++) != ' ') {}
をそのまま使用できますstrchr()
。これは最初のスペースの後の部分文字列に設定さstr
れます。スペースがない場合はそのままにしておきます。
char *tmp = strchr(str, ' ');
if(tmp != NULL)
str = tmp + 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 へのリンク)。
最も簡単でエレガントな方法は、そのようなことをすることであることがわかりました
int garbageInt;
sscanf(str, "%d %[^\n]\n",&garbageInt,str);