【问题标题】:How to get rid of first element in a string in c如何摆脱c中字符串中的第一个元素
【发布时间】:2021-11-20 21:34:03
【问题描述】:

我正在学习 c 并且在这个问题上遇到困难。 我正在编写一个获取包含正整数的字符串的函数。

该函数将该整数减去 1 并将获得的值放入字符串中。

但我现在的问题是

例如当输入为“1000”时,我希望输出为“999”。不是“0999”。 输入“100”,我希望输出为“99”,而不是“099”。所以我将在这个函数的末尾去掉第一个 0。 如何摆脱字符串的第一个元素??

感谢任何反馈!非常感谢。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void str_subtract_one(char* num) {
  int len = 0;
  int i ;
  for (i=0; num[i]!='\0';i++){
    len++;


  }
  int j;
for (j = len;j !=0;j--){
  if (len != '0'){
    if (num[j-1]=='9'){
      num[j-1] = '8';
      break;
    }  
    else if (num[j-1]=='8'){
      num[j-1] = '7';
      break;
    }  
    else if (num[j-1]=='7'){
      num[j-1] = '6';
      break;
    }
    else if (num[j-1]=='6'){
      num[j-1] = '5';
      break;
    } 
    else if (num[j-1]=='5'){
      num[j-1] = '4';
      break;
    }
    else if (num[j-1]=='4'){
      num[j-1] = '3';  
      break;
    } 
    else if (num[j-1]=='3'){
      num[j-1] = '2';
      break;
    }  
    else if (num[j-1]=='2'){
      num[j-1] = '1';
      break;
    } 
    else if (num[j-1]=='1'){
      num[j-1] = '0';
      break;
    }
    else if (num[j-1]=='0'){
      num[j-1] = '9';
     
    }

  }
  
}    
  if (num[0] =='0')
  //I dont know how to get rid of the first 0 here.

   
 
}

int main()  {
  
  
  char nums[] = "10000";
  
  
  str_subtract_one(nums);

  printf("\nQ5\nstr_subtract_one function result; %s\n",nums);

  
  return 0;
}

【问题讨论】:

  • 与其一直使用字符串,不如将字符串转换为 int,对 int 进行数学运算,然后再转换回字符串。
  • 但也要注意那个大的if ... else 块是可怕的。 '0' 需要一个特殊情况,但对于任何和所有其他数字,num[j-1] -= 1 都可以完成这项工作。
  • 另外len != '0' 并不意味着您似乎认为它意味着什么。我想len != 0 不同,这就是你的想法。
  • 如果你用char nums[] = "0";调用它的预期结果是什么?
  • @TedLyngmo:我已经在my answer to OP's previous question 中解决了这个问题。不过,您再次提出这个问题仍然是合适的。

标签: arrays c string function


【解决方案1】:

请您尝试以下方法:

#include <stdio.h>

void str_subtract_one(char *num) {
    int len = 0;
    int i;
    for (i = 0; num[i] != '\0'; i++) {
        len++;                          // determine the number of digits
    }

    for (i = len - 1; i >= 0; i--) {    // upward from the last digit
        if (num[i] == '0') {            // if the digit is '0'
            num[i] = '9';               // then put '9' and continue to the upper digit
        } else {                        // if the digit > '0'
            num[i]--;                   // then subtract 1
            break;                      // and exit the loop
        }
    }

    if (num[0] == '0' && len > 1) {     // if the highest digit is '0' and has lower digit
        for (i = 0; i < len; i++) {     // then shift the array including the terminating '\0'
            num[i] = num[i + 1];
        }
    }
}

int main()  {
    char nums[] = "10000";
    str_subtract_one(nums);
    printf("\nQ5\nstr_subtract_one function result; %s\n",nums);
    return 0;
}

【讨论】:

    【解决方案2】:

    对现有代码的更改最少的解决方案:

    char* str_subtract_one(char* num) {
      // as mentioned, this can be far more concise
      // ...
    
      // I dont know how to get rid of the first 0 here.
    
      // Advance the pointer past a leading zero.
      // If there's ever a case where there can be more than one leading '0',
      // you'll need to handle that here in a loop instead
      if (num[0] =='0')
      {
        num++;
      }
    
      return num;
    }
     
    int main()  {
      char nums[] = "10000";
      char* newNum = str_subtract_one(nums);
    
      // prints 9999
      printf("\nQ5\nstr_subtract_one function result; %s\n",newNum);
    
      return 0;
    }
    

    example

    【讨论】:

    • 不是最好的方法。
    • @0___________ 在什么条件下?比将每个字符左移一个更快。
    • 只是这个指针没有引用对象的开头。这是在大型项目中非常难以调试问题的常见来源。
    • @0___________ 为自己说话。想想我在我的职业生涯中只使用过一次memmove。我每次都会选择零拷贝。
    • 好吧,问题中的签名返回无效。这意味着您不能返回指针。您需要实际修改字符串。此外,如果您只是丢失了零字符,那就是内存泄漏。
    【解决方案3】:

    可能您不允许使用 stdlib 函数。

    您需要将所有字符向左移动一个字符:

        if (num[0] =='0')
        {
            for(size_t i = 1; i <= len; i++)
            {
                num[i - 1] = num[i];
            }
        }
    

    或者如果你被允许使用一些标准库函数:

        if (num[0] =='0')
        {
            memmove(num, num + 1, len);
        }
    

    https://godbolt.org/z/Prbzj6b9a

    【讨论】:

      【解决方案4】:

      要去掉字符串的第一个字符,只需在字符串指针上加 1。示例:

      puts("1234" + 1);
      

      打印:

      234
      

      请注意,此答案从字面上解决了该主题中的问题。

      【讨论】:

      • 根据this previous question from OP,似乎 OP 想要更改原始字符串,以便删除前导零。
      • @AndreasWenzel,这就是我添加注释的原因。该主题看起来定义明确,但与实际问题几乎没有关系。它应该类似于“如何删除航向零?”或类似的东西
      【解决方案5】:
      #include <stdio.h>
      
      void str_subtract_one(char* num) {    
          int number;
          sscanf(num, "%d", &number);
          number--;
          sprintf(num, "%d", number);
      }
      
      int main(void) {
        char num[5] = "1000";
        str_subtract_one(num);
        printf("%s\n", num);
        return 0;
      }
      

      【讨论】:

      • 我相信他是不允许使用这个抽象级函数的
      • OP 在this previous question 中表示该数字可能大于LLONG_MAX,因此他们需要将数字存储为字符串而不是整数类型。因此,这个答案可能对 OP 没有用。当然,这不是你的错,因为 OP 应该在问题中提到这个约束。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-18
      • 2012-11-19
      • 2020-03-30
      • 2021-04-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多