程序(部分):
#include <reg51.h>
#include "lcd.h"
#define LCD1602_WriteCom(c) LcdWriteCom(c)
#define LCD1602_PutChar(c) LcdWriteData(c)
#define LCD1602_Init() LcdInit()
void LCD1602_Goto(unsigned char x, unsigned char y)
{
LCD1602_WriteCom(0x80 + 0x40 * y + x);
}
void LCD1602_Puts(char* str)
{
for (; *str != '\0'; str++)
LCD1602_PutChar(*str);
}
unsigned char strlen(char *str)
{
unsigned char len = 0;
for (; *str != '\0'; str++, len++);
return len;
}
void insert(char *s1, char *s2)
{
unsigned char f = 0;
unsigned char len1 = strlen(s1);
unsigned char len2 = strlen(s2);
char *p1 = s1;
char *p2 = s1 + len1 - 1;
while (*p1 != '\0' && *p1 != *s2)
p1++;
if (*p1 != '\0')
{
for (; p2 > p1; p2--)
*(p2 + len2 - 1) = *p2;
f = 1;
}
while (*s2)
*p1++ = *s2++;
*(s1 + len1 + len2 - f) = '\0';
}
void main()
{
char s1[50] = "abcdef";
char *s2 = "d45";
LCD1602_Init();
LCD1602_Goto(0, 0);
LCD1602_Puts(s1);
LCD1602_Puts(",");
LCD1602_Puts(s2);
LCD1602_PutChar(':');
insert(s1, s2);
LCD1602_Goto(0, 1);
LCD1602_Puts(s1);
while (1);
}
运行效果: