C 中用另一个字符串替换一个子字符串

发布于 2024-12-01 22:38:47 字数 1128 浏览 0 评论 0原文

我正在编写一个代码来用它的值替换所有宏。 如果我的宏 MAX 值为 1000, 在代码中,它必须替换为 1000。(我假设一种情况,如果 MACROS 是一行中的第一个单词,那么在该行中 MACROS 将不会被我们替换,并且这种情况我们将得到不同的处理。

 //Code to replace MACROS BY THEIR VALUES 

 //line contains the actual one line of the code.  
 //line is initialized to contain as maximum number of charectos(say 100).

 //SrcStr is the macro  and destStr is its value. 

 //This block will be looped  for all lines.

   char* p; 
   p = strstr(line,srcStr);
   if(p != NULL)  //if the srcString is found
   {
      if(strlen(p) != strlen(line)) //special case   
      {
         if( isalnum(*(p-1)) == 0 && isalnum( *(p+strlen(srcStr)))==0 ) 
        // if the next char and prev char to our macro is not a alphabets or digits
             {
/*As answered by medo42 (below)*/     
     memmove(p+strlen(destStr), p+strlen(srcStr),strlen(p+strlen(srcStr)+1);
     memcpy(p,destStr,strlen(destStr));         
             }
           }
         else
         {/* handle differently*/}

       } 

因为我是第一次使用 memmovememcopy,我怀疑上面的代码是否稳定并正常工作

。 上面的代码对于所有输入情况都是稳定的吗?

I am writing a code to replace all MACROS with its value.
If my macro MAX has a value 1000,
And in the code, it must be replaced with 1000.(I am assuming a case that if the MACROS are the first word in a line, then in that line MACROS will not we replaced, and that case will we be handled differently.

 //Code to replace MACROS BY THEIR VALUES 

 //line contains the actual one line of the code.  
 //line is initialized to contain as maximum number of charectos(say 100).

 //SrcStr is the macro  and destStr is its value. 

 //This block will be looped  for all lines.

   char* p; 
   p = strstr(line,srcStr);
   if(p != NULL)  //if the srcString is found
   {
      if(strlen(p) != strlen(line)) //special case   
      {
         if( isalnum(*(p-1)) == 0 && isalnum( *(p+strlen(srcStr)))==0 ) 
        // if the next char and prev char to our macro is not a alphabets or digits
             {
/*As answered by medo42 (below)*/     
     memmove(p+strlen(destStr), p+strlen(srcStr),strlen(p+strlen(srcStr)+1);
     memcpy(p,destStr,strlen(destStr));         
             }
           }
         else
         {/* handle differently*/}

       } 

Since I'm using memmove and memcopy for the first time, I'm in doubt whether the above code is stable and works properly.

Is the above code correct?
And is the above code stable for all cases of inputs?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

宣告ˉ结束 2024-12-08 22:38:47

我至少看到三个问题:

  1. memmove 不应该使用 sizeof(p) ,它总是会被修复(比如说 4),它应该使用 strlen(line) - (p + strlen (p) - line)
  2. 您需要处理替换宏导致行长度超过 100 的情况
  3. 您需要处理宏标签被符号包围的情况。即,_MACRO_ 与 MACRO 不同。

I see at least three problems:

  1. memmove shouldn't use sizeof(p) which is always going to be fixed (say 4), it should use strlen(line) - (p + strlen(p) - line)
  2. You need to handle the case where replacing the macro increases the length of the line beyond 100
  3. You need to handle cases where the macro label is surrounded by symbols. i.e., _MACRO_ is not the same as MACRO.
第七度阳光i 2024-12-08 22:38:47

if(strlen(p) != strlen(line)) 为什么不在这里简单地使用 if(p != line) 呢?这应该是等效的,更容易理解并且更快(strlen 扫描整个字符串)。

isalnum(...) == 0 也许是个人喜好,但我会将该表达式写为 !isalnum(...) 因为这样更容易理解其含义方式。

memmove(p+(strlen(destStr)-strlen(srcStr)),p,sizeof(p)); 这对我来说看起来不对。它将根据指针大小移动许多字符,这是没有意义的,如果 srcStr 比 destStr 长,则移动的目的地可能是行缓冲区开始之前的位置。如果您想移动该行的其余部分以调整更改后的长度,请尝试以下操作:memmove(p+strlen(destStr), p+strlen(srcStr), strlen(p+strlen(srcStr)+1) ; +1 对于移动空终止符也很重要,当然,您需要确保行缓冲区实际上提供了足够的空间。

if(strlen(p) != strlen(line)) Why not simply use if(p != line) here? That should be equivalent, easier to understand and faster (strlen scans the entire string).

isalnum(...) == 0 Personal preference maybe, but I'd write that expression as !isalnum(...) since it's easier to understand the meaning this way.

memmove(p+(strlen(destStr)-strlen(srcStr)),p,sizeof(p)); This looks wrong to me. It will move a number of characters depending on your pointer size, which makes no sense, and if srcStr is longer than destStr, the destination of the move might be a position before the start of the line buffer. If you want to move the remainder of the line to adjust for the changed length, try this: memmove(p+strlen(destStr), p+strlen(srcStr), strlen(p+strlen(srcStr)+1); The +1 is important to move the null terminator as well. Of course, you need to ensure that the line buffer actually provides enough space.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文