Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6384215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:44:55+00:00 2026-05-25T02:44:55+00:00

I am writing a code to replace all MACROS with its value. If my

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T02:44:55+00:00Added an answer on May 25, 2026 at 2:44 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been writing some code that creates a generic blog. Its features are
I have a treeview in my C# code. I want to replace all existing
I'm writing some code where all I have access to is a textureID to
When writing code do you consciously program defensively to ensure high program quality and
After writing code to populate textboxes from an object, such as: txtFirstName.Text = customer.FirstName;
While writing code in a file that would comprise of PHP, HTML, CSS &
While writing code, it is pretty common to request a page with an appended
When writing code in an Eclipse project, I'm usually quite messy and undisciplined in
When I am writing code in Python, I often need to remove items from
I frequently find myself writing code like this: List<int> list = new List<int> {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.