【问题标题】:How can I use sscanf with white space?如何将 sscanf 与空白一起使用?
【发布时间】:2020-08-21 04:24:40
【问题描述】:

我正在尝试将字符串拆分为几部分,但它没有正确执行此操作。这是我的代码。

int main(void){
    char point[2][2];
    char *try="Distance P1 P2 // Prints the distance between P1 and P2.";
    sscanf(try,"Distance %s %s",point[0], point[1]);
    printf("point[0]:%s point[1]: %s\n",point[0],point[1] ); 

}

期待这个

point[0]:P1 point[1]:P2 

输出

point[0]:P1P2 point[1]: P2 

【问题讨论】:

    标签: c string split scanf whitespace


    【解决方案1】:
    #include <stdio.h>
    
    int main(void){
        char point[2][3];
        char *try="Distance P1 P2 " ; // Prints the distance between P1 and P2;
        sscanf(try,"Distance %s %s",point[0], point[1]);
        printf("point[0]:%s point[1]: %s\n",point[0],point[1] );
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-20
      • 1970-01-01
      • 1970-01-01
      • 2014-07-23
      • 2017-08-31
      相关资源
      最近更新 更多