📄 str.c
字号:
/****************************************************************************
FUNCTION: StrLpad
PURPOSE: Pad with spaces on left
StrRpad(dest,"123", 6) dest = " 123"
****************************************************************************/
#ifdef C_ANSI
VOID StrLpad(CHAR * dest, CHAR * source, INT i)
#else
VOID StrLpad(dest,source,i)
CHAR * dest, * source;
int i;
#endif
{
int c,q,m;
c = strlen(source);
if (c < i)
{
q = i-c;
StrSpaces(dest,q);
for (m = 0; m < q; m++)
*(dest+m+q) = source[m];
*(dest+i+1) = '\0';
}
}
/****************************************************************************
FUNCTION: StrMid
PURPOSE: Like basic mid$() extract from middle of string
StrMid(dest,"123",2,2) dest = "23"
****************************************************************************/
#ifdef C_ANSI
VOID StrMid(CHAR * dest, CHAR * source, INT beg, INT len)
#else
VOID StrMid(dest,source,beg,len) /* like Mid$() */
CHAR * dest,* source;
INT beg,len;
#endif
{
int i,k;
k = MIN(len,strlen(source));
for (i = 0; i < k; i++)
* dest++ = source[beg+i];
* dest = '\0';
}
#if 0
/****************************************************************************
FUNCTION: StrCopy
PURPOSE: string copy
****************************************************************************/
#ifdef C_ANSI
VOID StrCopy(CHAR * dest, CHAR * source)
#else
VOID StrCopy(dest,source)
CHAR * dest,* source;
#endif
{
int i,k;
k = strlen(source);
for (i = 0; i < k; i++)
* dest++ = source[i];
* dest = '\0';
}
/****************************************************************************
FUNCTION: StrCat
PURPOSE: string cat
****************************************************************************/
#ifdef C_ANSI
VOID StrCat(CHAR * dest, CHAR * source)
#else
VOID StrCat(dest,source)
CHAR * dest,* source;
#endif
{
int i,k,m;
k = strlen(dest);
m = strlen(source);
for (i = 0; i < m; i++)
dest[k+i] = source[i];
dest[k+m] = '\0';
}
#endif
/****************************************************************************
FUNCTION: StrInChar
PURPOSE: Find a character within a string
StrInChar("123", '3') return = [2]
****************************************************************************/
#ifdef C_ANSI
INT StrInChar(CHAR * source, CHAR find_char)
#else
INT StrInChar(source,find_char) /* Instr$() */
CHAR * source;
CHAR find_char;
#endif
{
int i = 0;
while ((source[i] != find_char) && (source[i] != '\0'))
i++;
if (source[i] == find_char)
return(i);
else
return(-1);
}
/****************************************************************************
FUNCTION: StrPos
PURPOSE: Find one string within another, returns -1 if not in
StrPos("1234567", "456" ) return = [3]
****************************************************************************/
#ifdef C_ANSI
INT StrPos(CHAR * s, CHAR *t)
#else
INT StrPos(s,t) /* look for t in s */
CHAR *s, *t;
#endif
{
int i,j,k;
for (i = 0; s[i] != '\0'; i++)
{
for (j = i, k = 0; t[k] != '\0' && s[j]==t[k]; j++, k++);
if (t[k] == '\0')
return(i);
}
return(-1);
}
/****************************************************************************
FUNCTION: StrRep
PURPOSE: Replace characters within a string
StrRep(dest,"_1_2_3", '_', ' ') dest = " 1 2 3"
****************************************************************************/
#ifdef C_ANSI
VOID StrRep(CHAR * dest, CHAR * source, CHAR find_char, CHAR rep_char)
#else
VOID StrRep(dest,source,find_char,rep_char) /* replace chars */
CHAR * dest,* source;
CHAR find_char,rep_char;
#endif
{
int k,m,n;
m = strlen(source);
for (n = 0; n < m; n++)
*(dest+n) = source[n];
*(dest+m) = '\0';
while ((k = StrInChar(dest,find_char)) != -1)
*(dest+k) = rep_char;
}
/****************************************************************************
FUNCTION: StrRpt
PURPOSE: Repeat characters into a string
StrRpt(dest,"0", 9) dest = "000000000"
****************************************************************************/
#ifdef C_ANSI
VOID StrRpt(CHAR * dest, CHAR rpt_char, INT i)
#else
VOID StrRpt(dest,rpt_char,i) /* repeating digits */
CHAR * dest;
CHAR rpt_char;
INT i;
#endif
{
int q;
for (q = 0; q < i; q++)
* dest++ = rpt_char;
* dest++ = '\0';
}
#if 0
double fnround(x,m)
double x;
int m;
{
int k;
double f,g;
double fabs();
f = (fabs(x) + 0.00501);
k = (f * 100.0);
g = k / 100.0;
g = g * SGN(x);
return(g);
}
wait_key(command)
CHAR *command;
{
CHAR inky;
inky = getcnb();
printf("%c",inky);
if ( inky == '\00')
{
*command = getcnb();
return(1);
}
else
{
*command = inky;
return(0);
}
}
void print_tab_mid(fp,tab,str,beg,len)
FILE *fp;
int tab;
CHAR *str;
int beg,len;
{
static int print_head;
static CHAR temp_s[255];
static CHAR s_s;
int i,k,r,crlf = 0;
if (tab < 0)
{
crlf = 1;
tab = abs(tab);
}
k = MIN(len,strlen(str));
for (i = 0; i < k; i++)
temp_s[i] = str[beg+i];
temp_s[k] = '\0';
r = tab-print_head;
if (r > 1)
{
StrSpaces(s_s,r);
fprintf(fp,s_s); /* advance spaces from last position to new tab */
}
if (r > -1)
{
fprintf(fp,temp_s); /* print data */
print_head = (print_head + r + strlen(str));
/* increment print_head for next read */
}
if (r < 0)
{
fprintf("\n"); /* do newline if tab too close */
StrSpaces(s_s,tab);
fprintf(fp,temp_s);
print_head = tab + strlen(temp_s);
}
if (crlf)
{
fprintf(fp,"\n");
print_head = 1;
}
}
void print_tab(fp,tab,str)
FILE *fp;
int tab;
CHAR *str;
{
static int print_head;
static CHAR s_s;
int k,r,crlf = 0;
if (tab < 0)
{
crlf = 1;
tab = abs(tab);
}
k = strlen(str);
r = tab-print_head;
if (r > 1)
{
StrSpaces(s_s,r);
fprintf(fp,s_s); /* advance spaces from last position to new tab */
}
if (r > -1)
{
fprintf(fp,str); /* print data */
print_head = (print_head + r + strlen(str));
/* increment print_head for next read */
}
if (r < 0)
{
fprintf("\n"); /* do newline if tab too close */
StrSpaces(s_s,tab);
fprintf(fp,str);
print_head = tab + strlen(str);
}
if (crlf)
{
fprintf(fp,"\n");
print_head = 1;
}
}
void print_mid(source,beg,len)
CHAR * source;
int beg,len;
{
int i,j;
j = MIN(strlen(source),(beg+len));
for (i = beg; i < j; i++)
putchar(source[i]);
}
void print_midrtrm(source,beg,len)
CHAR * source;
int beg,len;
{
int i,j;
static CHAR temp_1[255],temp_2[255];
copy_s(temp_1,source);
rtrm(temp_2,temp_1);
j = MIN(strlen(temp_2),(beg+len));
for (i = beg; i < j; i++)
putchar(temp_2[i]);
}
CHAR mid_asn(dest,beg,len,source)
CHAR * dest;
int beg,len;
CHAR * source;
{
int i,k,p,x;
static CHAR temp_s[255];
k = MIN(len,strlen(source));
p = strlen(dest);
if (p == 0)
{
StrSpaces(dest,beg);
p = beg;
}
x = beg - p;
if ( x > 0 )
{
rpad(temp_s,dest,beg);
copy_s(dest,temp_s);
}
for (i = beg; i < beg+len; i++)
dest[i] = source[i-beg];
if (strlen(dest) <= (beg+len) )
dest[beg+len] = '\0';
}
CHAR mid_asn_mid(dest,beg1,len1,source,beg2,len2)
CHAR * dest;
int beg1,len1;
CHAR * source;
int beg2,len2;
{
int i,j,k,p,x;
static CHAR temp_s[255];
k = MIN(len1,strlen(source));
p = strlen(dest);
if (p == 0)
{
StrSpaces(dest,beg1);
p = beg1;
}
x = beg1 - p;
if ( x > 0 )
{
rpad(temp_s,dest,beg1);
copy_s(dest,temp_s);
}
for (i = beg1, j = beg2; i < beg1+len1; i++, j++)
dest[i] = source[j];
if (strlen(dest) <= (beg1+len1) )
dest[beg1+len1] = '\0';
}
#ifdef UNIX
int strstr(pcStr1,pcStr2)
char * pcStr1;
char * pcStr2;
{
int iLen1;
int iLen2;
int iNdx1;
int iNdx2;
int iRet = C_FALSE;
int iMatches;
iLen1 = strlen(pcStr1); /* main string */
iLen2 = strlen(pcStr2); /* string being looked for in main string */
for(iNdx1 = 0; iNdx1 < iLen1; iNdx1++)
{
iMatches = 0;
for(iNdx2 = 0; (iNdx2 < iLen2) && (iNdx1+iNdx2 < iLen1); iNdx2++)
if(pcStr1[iNdx1 + iNdx2] == pcStr2[iNdx2])
iMatches++;
if(iMatches == iLen2)
{
iRet = C_TRUE;
break;
}
}
return(iRet);
}
#endif
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -