解析c中的char字符串以仅获取感叹号后的数字

Parse a char string in c to get just the numeric digits after the exclamation mark

如果我有类似 !23 的内容,我如何解析它以仅获取数字部分?

输入:!23 输出:23

输入:! 输出:错误

输入:!23eds3 输出:错误

这是我获取数字的方法。但是由于某种原因,strcpy 弄乱了我存储一些数据的数组,它删除了数组中的第一个元素。

if (args[0][0] == '!'){ 
  char str_index[1];
  strcpy(str_index, &args[0][1]);
  int index = atoi(str_index);
}

另外,我用 C 编写的程序不多,这只是我用 C 编写的第二个程序。

一些提示,但不会为您编写代码:

使用 strchr 等字符串库函数来查找“!”。然后使用 strtod 将字符串转换为数字。查看 strtod 的输出,看看它是否有错误或后面是否有字母。

或者您可能想要 strtol 代替整数。