我需要编写一个名为doubleString的函数,它接收一个字符串,然后将文本中的所有数字乘以2.该函数应该返回一个指向接收到的字符串的开头的指针,以便于链接.

示例:如果输入字符串为

这个句子包含数字4、数字8和数字88

调用该函数后,字符串应为:

这个句子包含数字8、数字16和数字176

不允许使用string.hstdlib.h库中的函数以及stdio.h库中的sprintfsscanf函数.不允许在函数中或全局创建助手字符串.

我已经编写了代码,但它只有在输入没有单词的字符串时才起作用,只有数字才起作用.在try 时,我也遇到了一些问题,比如程序对445以上的数字不起作用,或者每次得到不同的输出.这是我当前的代码:

#include <stdio.h>

void inputString(char array[], int size) {
    char character = getchar();
    if (character == '\n') character = getchar();
    int i = 0;
    while (i < size - 1 && character != '\n') {
        array[i] = character;
        i++;
        character = getchar();
    }
    array[i] = '\0';
}

char *doubleString(char *text) {
    int carry = 0;
    int length = 0;

    while (text[length] != '\0') {
        length++;
    }

    for (int i = length - 1; i >= 0; i--) {
        if (text[i] >= '0' && text[i] <= '9') {
            int digit = ((int)text[i]) * 2 + carry;
            carry = digit / 10;
            text[i] = (digit % 10) + '0';
        }
    }

    while (carry > 0) {
        for (int i = length; i > 0; i--) {
            text[i] = text[i - 1];
        }

        text[0] = (carry % 10) + '0';

        carry /= 10;
        length++;
    }

    return text;
}

int main() {
    char text[1000];
    int size = sizeof(text);
    printf("Enter text: ");
    inputString(text, size);

    char *result = doubleString(text);

    printf("'%s'", result);

    return 0;
}

该程序在这种情况下不起作用:

输入:

'U ovoj recenici se nalazi broj 4, broj 8 i broj 88'
   

预期输出:

'U ovoj recenici se nalazi broj 8, broj 16 i broj 176'
        

我的输出:

'U ovoj recenici se nalazi broj 9, broj 7 i broj 76'

但在这件事上很管用:

输入:

"1 i 1 jesu 2"

预期输出:

'2 i 2 jesu 4'

我的输出:

'2 i 2 jesu 4'

推荐答案

@anatolyg(当前被删除)很好地标识了代码不能被simply就地替换,因为像"99"这样的子字符串将被更长的"198"替换.

找到字符串末端后,沿字符串向下传递2次.第一个只是确定所需的额外偏移量,而第二个使用类似的代码,从新的末端开始并执行复制.

代码不使用int索引,因为字符串可能长于INT_MAX.

((int)text[i]) * 2 + carry is amiss.
Better as (text[i] - '0') * 2 + carry; @Weijun Zhou .

OP的while (carry > 0) {只能很好地处理一个前导数字子串. 可能有很多人.

为了删除不需要的前导零,当找到不需要的前导零时,查找字符串结尾的循环也会缩短字符串.

/*
 * Copy in-place string 'src' to `src` with each sub-string of digits doubled in value.
 */
char* double_str(char *src);

#define IS_DIGIT(ch) ((ch) >= '0' && (ch) <= '9')

char* double_str(char *src) {
  char *s0 = src;
  char *d0 = src;

  // Remove unneeded leading zeroes and find end.
  int is_numeric = 0;
  while (*s0) {
    while (!is_numeric && s0[0] == '0' && IS_DIGIT(s0[1])) {
      s0++;
    }
    is_numeric = IS_DIGIT(*s0);
    *d0++ = *s0++;
  }
  *d0 = '\0';

  char *end = d0;
  char *new_end = end;

  // Make 2 passes.
  // 1st to find the new end of the string.
  // 2nd to copy the string.
  for (int i = 0; i < 2; i++) {
    int carry = 0;
    char *s = end;
    char *d = new_end;
    if (i > 0) {
      *d = '\0';
    }
    while (s > src) {
      s--;
      if (IS_DIGIT(*s)) {
        int new_digit = (*s - '0')*2 + carry;
        if (i > 0) {
          d--;
          *d = (char)(new_digit % 10 + '0');
        }
        carry = new_digit/10;
        // Do we have a non-zero carry and the prior character is not a digit?
        if (carry && (s == src || !IS_DIGIT(s[-1]))) {
          if (i > 0) {
            d--;
            *d = '1';
          }
          new_end++;
        }
      } else {
        if (i > 0) {
          d--;
          *d = *s;
        }
        carry = 0;
      }
    }
  }
  return src;
}

测试代码

#include <stdio.h>

int main(void) {
  char src_text[1000] = "5x2x5x9x15x99x500000000000000000000000000x";
  //printf("Enter text: ");
  //inputString(text, size);
  printf("'%s'", double_str(src_text));
  return 0;
}

结果是

'10x4x10x18x30x198x1000000000000000000000000000x'

C++相关问答推荐

ARM上的Modulo Sim Aarch 64(NEON)

intellisense不工作,甚至已经下载了c/c++扩展

为什么getchar()挂起了,尽管poll()返回了一个好的值?""

如何创建由符号组成的垂直结果图形?

GLIBC:如何告诉可执行文件链接到特定版本的GLIBC

C lang:当我try 将3个或更多元素写入数组时,出现总线错误

如何在C中打印包含扫描字符和整数的语句?

如何按顺序将所有CSV文件数据读入 struct 数组?

为 struct 中的数组动态分配内存时出错

如何确保我将使用C标准库函数的函数版本,如&getc";,而不是类似函数的宏版本?

我可以创建适用于不同endian的 colored颜色 struct 吗?

不带Malloc的链表

使用C++中的字符串初始化 struct 时,从‘char*’初始化‘char’使指针变为整数,而不进行强制转换

有没有办法减少C语言中线程的堆大小?

如何组合两个宏来初始化C语言中的字符串数组?

Linux/C:带有子进程的进程在添加waitid后都挂起

我编写这段代码是为了判断一个数字是质数、阿姆斯特朗还是完全数,但由于某种原因,当我使用大数时,它不会打印出来

在C中打印指针本身

当另一个指向 const 的指针观察到数据时,通过指针更改数据是否安全?

`void foo(int a[static 0]);` 有效吗?