Я пишу код, чтобы заменить все MACROS на его значение.
Если мой макрос MAX имеет значение 1000,
А в коде его нужно заменить на 1000. (Я предполагаю случай, когда MACROS - это первое слово в строке, тогда в этой строке MACROS не будет заменен, и в этом случае мы будем обрабатываться по-другому. 1001 *
//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*/}
}
Поскольку я впервые использую memmove
и memcopy
, я сомневаюсь, что приведенный выше код стабилен и работает правильно.
Код выше верен?
И является ли приведенный выше код стабильным для всех случаев ввода?