1. String substitution
Syntax: replace (char Str[],char Key[],char swap[]); |
Parameters: |
Str[]: |
Replace operation on this source string |
Key[]: |
The string to be replaced, cannot be an empty string |
Swap[]: |
The replacement string, which can be an empty string, is an empty string representing the deletion of key[in the source character] |
return value: |
Null |
Attention: |
|
|
Default str[] length less than 1000, if no, reset the TMP size setting |
|
Need string.h |
SOURCE program: |
|
|
void replace (char Str[],char Key[],char swap[])
{
int l1,l2,l3,i,j,flag;
Char tmp[1000];
L1=strlen (str);
L2=strlen (key);
L3=strlen (swap);
for (i=0;i<=l1-l2;i++)
{
flag=1;
for (j=0;j<l2;j++)
if (Str[i+j]!=key[j])
{
flag=0;break;
}
if (flag)
{
strcpy (TMP,STR);
strcpy (&tmp[i],swap);
strcpy (&tmp[i+l3],&str[i+l2]);
strcpy (str,tmp);
i+=l3-1;
L1=strlen (str);}}}
|
2. String Lookup
Syntax: Result=strfind (char Str[],char key[]); |
Parameters: |
Str[]: |
Find operations on this source string |
Key[]: |
The string to be searched for cannot be empty |
return value: |
If the lookup succeeds, returns the position where key first appears in STR, otherwise returns-1 |
Attention: |
|
|
Need string.h |
SOURCE program: |
|
|
int Strfind (char str[],char key[])
{
int l1,l2,i,j,flag;
L1=strlen (str);
L2=strlen (key);
for (i=0;i<=l1-l2;i++)
{
flag=1;
for (j=0;j<l2;j++)
if (Str[i+j]!=key[j]) {flag=0;break;}
if (flag) return i;
}
return-1;
} |
3. String interception
Syntax: Mid (char str[],int start,int Len,char strback[]) |
Parameters: |
Str[]: |
The target string for the operation |
Start |
Starts with the beginning of the first string and intercepts the Len-length characters |
Len: |
Starts with the beginning of the first string and intercepts the Len-length characters |
Strback[]: |
truncated to the character |
return value: |
0: Out of string length, intercept failed; 1: Intercept Successful |
Attention: |
|
|
Need string.h |
SOURCE program: |
|
|
int mid (char str[],int start,int len,char strback[])
{
int l,i,k=0;
L=strlen (str);
if (start+len>l) return 0;
for (i=start;i<start+len;i++)
strback[k++]=str[i];
strback[k]= ' + ';
return 1;
} |
4. String reversal function
Char *strrev (char string[])//string inversion function
{
char *start=string;
char *left=string;
char ch;
while (*string + +);
string-=2;
while (left<string)
{
ch=*left;
*left++=*string;
*string--=ch;
}
return (start);