Use the C language to implement string processing library functions and extend

Source: Internet
Author: User

1. Implement the basic C language library functions:

int Mystrlen (const char* str);//Gets the length of the string based on the first address of the incoming string; The return value is length

int Mystrlen (const char* STR)

{

if (str = = NULL)

return 0;

int length=0;

while (*str! = ')

{

length++;

str++;

}

return length;

}


void* mystrcpy (char* des, const char* source);//copy Source to des

void* mystrcpy (char* des, const char* source)
{
if (des = = NULL | | source = = NULL)
return NULL;
char *desbak = des;
while (((*des++) = (*source++))! = ' + ');
*des = ' + ';
return desbak;
}


void * MYSTRCAT (char* des, const char *source);//Add source to the end of Des

void * MYSTRCAT (char* des, const char *source)
{
if (Source = = NULL | | | des = = NULL)
return NULL;
if (Mystrlen (source) = = 0)
Return des;
Des = (char*) realloc (DES, Mystrlen (des) + mystrlen (source) + 1);

int i = Mystrlen (DES);
while (*source! = ')
{
* (des + i) = *source;
i++;
source++;
}
* (des + i) = ' + ';
Return des;
}


char* mystrrev (const char* str); String reversal

char* mystrrev (const char* STR)
{
int i = 0;
int len = Mystrlen (str);
char* res = (char*) calloc (len+1,sizeof (char));
* (res + len) = ' + ';
for (i = len-1; I >= 0; I--, str++)
{
* (res + i) = *str;
}
return res;
}


int str2int (const char* str);//convert integer string to integer; return value is result integer, failure returns-1

int str2int (const char* STR)
{
if (str = = NULL)
return-1;
char* strbak = str;
int res = 0;
while (*str! = ')
{
if (*str< ' 0 ' | | | *str> ' 9 ')
{
return-1;
}
str++;
}
str = Strbak;
while (*str! = ')
{
Res *= 10;
int temp = *str-48;
res + = temp;
str++;
}
return res;
}


char* int2str (int num);//Converts a positive integer to a string, returning the first address of the string

char* int2str (unsigned int num)
{
int numbak = num;
int n=0;//computes the number of int digits
while (num! = 0)
{
n++;
Num/= 10;
}
char* res = (char*) calloc (n+1,sizeof (char));
char* Resbak=res;
num = Numbak;
while (num! = 0)
{
*res = num% 10 + 48;
res++;
Num/= 10;
}
*res = ' + ';
Resbak = Mystrrev (Resbak);
return resbak;
}

2. According to the library functions written above, some extended functions are implemented:

typedef struct _MYSTRING//defines the string structure, including the string and its length

{

Char *p;

int length;

} myString;

int initstring (myString *string, char* str);//Initialize string

int initstring (myString *string, char* str)
{
if (str = = NULL)
return-1;
int length = Mystrlen (str);

String->p = (char*) calloc (length + 1,sizeof (char));
mystrcpy (STRING->P,STR);
string->length = length;
return 1;
}


void * Findchar (const myString *string, const char FINDCH);//Find a specific character and return to the first address

void * Findchar (const myString *string, char findch)
{
char* s = string->p;
while (*s! = ')
{
if (*s== findch)
return s;
s++;
}
return NULL;
}


void * findString (const myString *string,const char* FINDSTR);//Find String, return first address, cannot find return null

void * findString (const myString *string,const char* findStr)
{
if (NULL = = findStr)
return NULL;
if (Mystrlen (FINDSTR) > Mystrlen (string->p))
return NULL;

char* s = string->p;
CHAR*SP = NULL;

int i = 0,j=0;
for (i = 0; i < string->length-1; i++)
{
char* fs = FINDSTR;
if (* (s + i) = = * (FS))
{
SP = s + i;
for (j = 0; J < Mystrlen (FINDSTR)-1; j + +)
{
sp++;
fs++;
if (*sp! = *fs)
Break
}
if (J ==mystrlen (FINDSTR)-1)
return s + i;
}
}
return NULL;
}

int Addchar (myString *string, Char addch, char* POS),//pos null is added after string, otherwise a character is added at the specified position; Return 1 means the operation failed

int Addchar (myString *string, Char addch, char* POS)//pos to null means to add after string
{
if (pos = = NULL)
{
String->p = (char *) realloc (string->p, string->length + 2);
Mystrcat (String->p, &ADDCH);
String->length + = 1;
return 1;
}

if (pos<string->p | | pos>string->p + string->length)
return-1;

int i = string->length, addlength = pos-string->p;
String->p = (char *) realloc (string->p,string->length+2);
Char *s = string->p;
* (s + i + 1) = ' + ';

for (i = string->length; i > addlength; i--)
{
* (s + i) = * (s+i-1);
}
* (s + i) = ADDCH;
String->length + = 1;
return 1;
}


int addstring (myString *string, const char* ADDSTR, char* pos),//pos null is added after string, otherwise a string is added at the specified position; Return 1 means the operation failed

int addstring (myString *string, const char* ADDSTR, char* POS)
{
if (pos = = NULL)
{
Mystrcat (String->p, ADDSTR);
String->length + = Mystrlen (ADDSTR);
return 1;
}

if (pos<string->p | | pos>string->p + string->length| | Addstr==null)
return-1;

String->length + = Mystrlen (ADDSTR);

int i = String->length+mystrlen (addstr), addlength = pos-string->p;
String->p = (char *) realloc (string->p, String->length +mystrlen (ADDSTR) +1);
Char *s = string->p;
* (s + i) = ' + ';

for (i = String->length+mystrlen (addstr)-1; i > addlength; i--)
{
* (s + i) = * (S + i-mystrlen (ADDSTR));
}
while (*addstr! = ')
{
* (s + i) = *addstr;
i++;
addstr++;
}

return 1;
}

void Deletechar (myString *string,char deletech,int tag),//tag is 1, all specified characters are deleted, otherwise, only the first

void Deletechar (myString *string, char deletech,int tag)
{
char * pos = (char*) Findchar (string, Deletech);
if (pos = = NULL)
return;

while (*pos! = ')
{
*pos = * (pos+ 1);
pos++;
}
String->length-= 1;
if (tag = = 1)
{
while (Findchar (string, Deletech)! = NULL)
Deletechar (string, deletech,1);
}
}


void DeleteString (myString *string,const char *deletestr,int tag),//tag is 1, all specified characters are deleted; otherwise, only the first one is deleted;

void DeleteString (myString *string, const char *deletestr,int tag)
{
char * pos = (char*) findString (string, deletestr);
if (pos = = NULL)
Return

while (* (Pos+mystrlen (DELETESTR)-1)! = ' + ')
{
*pos = * (pos + mystrlen (DELETESTR));
pos++;
}
String->length-= Mystrlen (DELETESTR);
if (tag ==1)
{
while (FindString (string, deletestr)! = NULL)
DeleteString (string, deletestr,1);
}
}

void Updatechar (myString *string,char oldch,char newch);//Update one character

void Updatechar (myString *string, Char Oldch, char newch)
{
char* POS;
if (pos = Findchar (String, oldch) = = = NULL)
Return
*pos = Newch;
while (Findchar (string, OLDCH)! = NULL)
Updatechar (STRING,OLDCH,NEWCH);
}


void Updatestring (myString *string, char* oldstr, const char* NEWSTR);//Update string

void Updatestring (myString *string, char* oldstr, const char* NEWSTR)
{
if (newstr = = NULL)
Return
char* POS;
if (pos = findString (String, oldstr) = = = NULL)
Return
DeleteString (string,oldstr,0);
AddString (String,newstr,pos);
while (FindString (string, oldstr)! = NULL)
{
Updatestring (STRING,OLDSTR,NEWSTR);
}
}

Use the C language to implement string processing library functions and extend

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.