c - 关于这个问题的快速问题,为什么它不在字符串上打印出第二个值(转换后的第二个值)?

标签 c string integer scanf

快速提问,我在这里做错了什么。这段代码的目的是将输入变成一个字符串,输入是“12 34”,在“12”和“32”之间有一个空格,并转换和打印来自整数变量的两个单独的数字,称为数字。为什么第二次调用函数 copyTemp 不产生值 34?。我有一个 index_counter 变量,它跟踪字符串索引,它意味​​着跳过“空格”字符?我做错了什么?

谢谢。

#include <stdio.h>
#include <string.h>
int index_counter = 0;
int number;
void copyTemp(char *expr,char *temp);

int main(){
 char exprstn[80]; //as global?
 char tempstr[80];

 gets(exprstn);
 copyTemp(exprstn,tempstr);
 printf("Expression: %s\n",exprstn);
 printf("Temporary: %s\n",tempstr);
 printf("number is: %d\n",number);
 copyTemp(exprstn,tempstr);      //second call produces same output shouldnt it now produce 34 in the variable number?
 printf("Expression: %s\n",exprstn);
 printf("Temporary: %s\n",tempstr);
 printf("number is: %d\n",number);

 return 0;
}
void copyTemp(char *expr,char *temp){
 int i;
 for(i = index_counter; expr[i] != '\0'; i++){
  if (expr[i] == '0'){
   temp[i] = expr[i];
  }
  if (expr[i] == '1'){
   temp[i] = expr[i];
  }
  if (expr[i] == '2'){
   temp[i] = expr[i];
  }
  if (expr[i] == '3'){
   temp[i] = expr[i];
  }
  if (expr[i] == '4'){
   temp[i] = expr[i];
  }
  if (expr[i] == '5'){
   temp[i] = expr[i];
  }
  if (expr[i] == '6'){
   temp[i] = expr[i];
  }
  if (expr[i] == '7'){
   temp[i] = expr[i];
  }
  if (expr[i] == '8'){
   temp[i] = expr[i];
  }
  if (expr[i] == '9'){
   temp[i] = expr[i];
  }
  if (expr[i] == ' '){ 
   temp[i] = '\0';
   sscanf(temp,"%d",&number); 
   index_counter = i+1; //skips?
  }
 }
 // is this included here? temp[i] = '\0'; 
}

最佳答案

你的程序有几个问题:

  • 您正在使用相同的索引 exprtemp 数组。这适用于 第一次,因为两者都是 0 开始但当你想 处理 2nd 号码,你需要 将索引重置为 temp 数组 回到 0。显然这不可能 使用单个索引完成。你会 必须使用两个索引,ij
  • 当您完成 处理第二个数字( 34"12 34") 你会到达终点 字符串,因此 sscanf 从不 第二次运行(在 一般最后一次)。所以 在 for 循环之后你需要另一个 sscanf 提取最后一个数字。一旦你从字符串中提取数字并递增 i,你也应该从函数返回。
  • 您应该避免使用 gets() 并使用 fgets() 而不是因为安全 原因。
  • 您可以结合多项测试 将数字放入单个测试中 显示:

像这样。

void copyTemp(char *expr,char *temp){
    int i;
    int j = 0;
    for(i = index_counter; expr[i] != '\0'; i++){

        if (expr[i] >= '0' && expr[i]<='9'){
            temp[j++] = expr[i]; // copy the digit into temp..increment j.
        }    
        else if (expr[i] == ' '){ // space found..time to extract number.
            temp[j] = '\0'; // terminate the temp.
            sscanf(temp,"%d",&number); // extract.
            index_counter = i+1; // skip the space.
                    return; // done converting...return..must not continue.
        }
    }
    // have reached the end of the input string..and still need to extract a 
    // the last number from temp string.
    temp[j] = '\0';
    sscanf(temp,"%d",&number);
}

经过这些更改后,它按预期工作:

$ gcc b.c 2> /dev/null && ./a.out
12 34
Expression: 12 34
Temporary: 12
number is: 12
Expression: 12 34
Temporary: 34
number is: 34

您的方法非常脆弱...如果用户在输入数字之间提供多个空格...您的程序将失败。

关于c - 关于这个问题的快速问题,为什么它不在字符串上打印出第二个值(转换后的第二个值)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2400254/

相关文章:

c - 帮助理解 x86 内联汇编中的 DIV 指令

c++ - std::string 比较(检查字符串是否以另一个字符串开头)

algorithm - 位与int/float随机数生成器的关系

java - 为什么我的代码不从命令行获取输入?

c - 从文件中反转字符串,将字符串中的字符替换为另一个字符串并打印

java - 将 32 位整数中的所有位取反而产生错误输出的代码是什么?

c - 如何手动执行此操作。如何获取对 Print_Array 的第三次、第五次和第七次调用的输出?

catopen() 在某些情况下失败时不会设置 errno

c++ - 在C/C++中将变量名定义为__00000001有什么好处

c# - 自动调整固定大小标签中的文本以避免文本跳到第二行