【问题标题】:How to Replace Leading or Trailing Blank Characters with "X"如何用“X”替换前导或尾随空白字符
【发布时间】:2021-03-31 21:37:49
【问题描述】:

寻找一种更有效的方法来替换前导和尾随空格 (' ') 并在每个空格的前面附加一个 'X'。它似乎可以用于尾随空格,但我想知道是否有更好/更简单的方法来解决这个问题。

例子:

传入字符串:'12345 ' 想要的结果'XXXXX12345'

删除了 5 个空格并将 5 个'X's 附加到前面。

例子:

传入字符串:' 12345' 想要的结果'XX12345'

去掉2个空格,在前面追加2个'X's。

void fixStr(char* str)
{
    int i = 0;
    int length = strlen(str);
    char strCopy[10];
    strcpy(strCpy, str);

    for(i = 0; i < length; i++)
    {
        if(strCopy[i] == ' ')
        {
            strCopy[i] = '\0';
            str[i] = '\0';

            break;
        }
    }
    for(i = 0; i < length - i + 2; i++)
    {
        str[i] = 'X';
        str[i + 1] = '\0';
    }
    strcat(str, strCopy);

}

【问题讨论】:

  • Passed in string: '12345 ' Desired result 'xxxxx12345' - 这里的逻辑到底是什么???
  • 顺便说一句,s_length 已使用但未在代码中的任何位置声明和初始化!
  • 请注意,strCopy 的大小应为 char strCopy[length + 1],以确保安全。
  • 您谈论 X 而您的示例使用 x 代替;我已将示例转换为使用大写字母,但您的问题确实应该是自洽的。注意这些细节是成功编程的先决条件。
  • Passed in string: '12345 ' Desired result 'XXXXX12345' 这里没有前导空格,只有尾随空格,为什么要在字符串中添加XXXXX?跨度>

标签: c string removing-whitespace prepend null-terminated


【解决方案1】:

我没有修复您的代码,但您可以将sprintfisspace 结合使用,类似于此。另外,请记住在字符串末尾为'\0 留出一个空格。使用这个想法,它应该可以帮助你:

#include <ctype.h>
#include <stdio.h>

int main()
{
    char buf[11];
    char *s = "Hello";
    int i;
    
    sprintf(buf, "%10s", s); /* right justifies in a column of 10 in buf */
    
    for(i = 0; i < 10; i++) {
        if(isspace(buf[i])) /* replace the spaces with an x (or whatever) */
            buf[i] = 'x';
    }
    
    printf("%s\n", buf);
    
    return 0;
}

【讨论】:

    【解决方案2】:

    工程师解决问题的方式:

    1. 定义需求。
    2. 了解您的工具。
    3. 使用尽可能简单、尽可能准确的工具来制定解决方案。

    在你的情况下:

    1. 需求:

      • 查找尾随空格的数量
      • 将字符串内容移到末尾
      • 将开头设置为“X”
    2. 工具:

      • 测量、迭代、比较和计数
      • 移动一块内存
      • 初始化一块内存
    3. 解决方案示例:

      #include <string.h> /* for strlen(), memmove () and memset() */
      
      void fix_str(char * s)
      {
        if ((NULL != s) && ('\0' != *s)) /* Ignore NULL and empty string! */
        {
          /* Store length and initialise counter: */
          size_t l = strlen(s), i = l;
      
          /* Count space(s): */
          for (; (0 != i) && (' ' == s[i-1]); --i); /* This for loop does not need a "body". */
      
          /* Calculate the complement: */
          size_t c = l - i;
      
          /* Move content to the end overwriting any trailing space(s) counted before hand: */
          memmove(s+c, s, i); /* Note that using memmove() instead of memmcpy() is essential 
                                 here as the source and destination memory overlap! */ 
      
          /* Initialise the new "free" characters at the beginning to 'X's:*/
          memset(s, 'X', c);
        }
      }
      

    【讨论】:

      【解决方案3】:

      实现这一点的一种方法是找出字符串的前导非空格位置和尾随非空格位置,然后将中间的内容(前导非空格,尾随非空格)移动到字符串的末尾,然后将开头的所有空白空间设置为'x'

      这样就可以得到预期的输出(函数如下)

      void fixStr(char* str)
      {
          int i = 0;
          int length = strlen(str);
          int leadindex = length; 
          int tailindex = 0;
          // First find the leading nonspace position
          for(i = 0; i < length; i++)
          {
              if(str[i] != ' ')
              {
                  leadindex = i;
                  break;
              }
          }
          
          // if not found nonspace then no change
          if( leadindex == length ) 
          {
              // all spaces, so no change required;
              return;
          }
          
          // Find the trailing nonspace position 
          for(i = length - 1; i >= 0 ; i--)
          {
              if(str[i] != ' ')
              {
                  tailindex = i;
                  break;
              }
          }
          
          // move the buffer (in place) to exclude trailing spaces
          memmove(str + (length - tailindex -1),str,(tailindex +1) );
          // set the 'x' to all empty spaces at leading ( you may use for loop to set this)
          memset(str, 'X', length - (tailindex - leadindex + 1) );
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-01-16
        • 2023-02-14
        • 2022-09-26
        • 2018-04-11
        • 2018-07-23
        • 1970-01-01
        • 2019-05-31
        • 1970-01-01
        相关资源
        最近更新 更多