c - 删除第一个有效字符 C 之前的空格

标签 c arrays char whitespace removing-whitespace

我基本上想删除数组中第一个有效字符之前的所有前导空格。

例如,如果我有类似 ' 1.6, 1.7' 的内容,我希望它是 '1.6, 1.7' 或者只是 '1.7 , 1.8',那么它将是 '1.7, 1.8'

这是我处理空白的方法,但它只显示空白所在的位置。我需要帮助删除它。

char **storeArray

void Students::removeSpace()
{
   int MAX_SIZE = 30;
   for(int i=0; i<3; i++)
   {
     for(int j=0; j<MAX_SIZE; j++)
     {
        if(isspace(storeArray[i][j]) && !(isspace(storeArray[i][j++])
        {
          // I NEED HELP HERE. I'M TRYING TO REMOVE ALL THE LEADING WHITESPACE ONLY
        }
     }
   }
}

最佳答案

要删除多余的空格,请遍历字符串:

void Remove_Leading_ExtraMiddle_Trailing_Whitespace(char *s, int size) {
  char *end = &s[size];
  char *t = s;
  // skip leading
  while (isspace(*s))
    s++;

  // middle
  for (;;) {
    while (!isspace(*s) && *s != '\0') {
      *t++ = *s++;
    }
    if (*s == '\0')
      break;
    *t = *s++;
    while (isspace(*s))
      s++;
    if (*s == '\0') {
      break;
    }
    t++;
  }

  // end
  while (t < end) {
    *t++ = '\0';
  }
}

void removeSpace() {
  int MAX_SIZE = 30;
  char storeArray[4][MAX_SIZE];
  for (int i = 0; i < 3; i++) {
    Remove_Leading_ExtraMiddle_Trailing_Whitespace(storeArray[i], MAX_SIZE);
  }
}

关于c - 删除第一个有效字符 C 之前的空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27433809/

相关文章:

c - 遍历数组并将元素移动到数组末尾

arrays - GSON 阵列,错误消息 : Expected a string but was BEGIN_ARRAY

Python:将日期从字符串转换为数字

java - 为什么单个Java char变量可以表示超过65535个字符?

c - *char 值在函数上改变

c - 当 malloc 返回时,8 字节对齐是什么意思?

使用 sprintf 和 printf 时的 C 线程安全区域设置/编码

c - 需要帮助进行自然对数 (ln) 的数学计算

c# - 嵌套 for 循环的动态数量,用于列出对象的唯一组合

javascript - 将提示中的输入存储在数组中并显示该数组