Go to string processing function (not implemented with library functions)

Source: Internet
Author: User

//////////////////////////////////////////////////////////////////////////////////
Calculate the length of a string
int strlen1 (const char *STR)
{
int len=0;
while (*str++!= ')
len++;
return Len; /* This method is more efficient than the above (self-increment two) when the string length is longer
**const Char *temp=str;//Save the first address of SRC
**while (*src++!= ');//self-increment once
**return (src-temp-1);//Returns the difference between the tail pointer and the head pointer, that is, the length
*/
}
////////////////////////////////////////////////////////////////////////////////
Removes all specified characters from a string
Two pointers are defined, the head pointer is used for recording, and p is used for string traversal;
Char *del_char (char *str,char c)
{
Char *head=null;
Char *p=null;
if (str==null)
{
return NULL;
}
HEAD=P=STR;
while (*P)
{
if (*p!=c)
{
*str++=*p;
}
p++;
}
*str= ' + ';
return head;
}
//////////////////////////////////////////////////////////////////////////////
Deletes a specified length of character from the specified position of the string
First check the legitimacy of the POS and Len, then find the POS location, delete len characters
Char *delete_string (char *str,int pos,int len)
{
Char *p=str+pos-1;//character pointing to pos position
int strlen=strlen1 (str);//Custom method
int Str_len=strlen (str);//Character length library function if ((pos<1) | | (P-STR) >strlen)//check POS is not greater than 1, or POS exceeds the string length
{
return str;
}
if ((P+LEN-STR) >strlen)//len is greater than the number of characters remaining after Pos, simply assign '% ' to POS location
{
*p= ' + ';
return str;
}
while (*p && * (P+len))//len is less than or equal to the number of characters remaining after Pos delete len characters
{
*p=* (P+len);
p++;
}
*p= ' + ';
return str;
}
///////////////////////////////////////////////////////////////////////////////
The implementation of searching STRSTR library function of strings in string
const char *STRSTR1 (const char *src,const char *sub)
{
const char *BP;
const char *SP;
if (Src==null | | sub==null)//Check the validity of SRC and sub
{
return SRC;
}
while (*SRC)
{
BP=SRC;
Sp=sub;
Do
{//Traverse sub String
if (!*SP)//If the sub string end position is reached, exit
return SRC;
} while (*bp++==*sp++);
Src+=1;
} return NULL;
}
/////////////////////////////////////////////////////////////////////////////
The STRCHR () function returns a pointer to the first occurrence of CH in STR and returns NULL when no CH is found in Str.
Char *strchr1 (char *src, Char ch)
{while ((*src!=ch) && (*SRC))
src++; return (SRC);}
////////////////////////////////////////////////////////////////////////////////
CHRCNT () to calculate the number of occurrences of a character in a string.
int chrcnt1 (const char *src,char ch)
{
int count=0;
while (*SRC)
if (*src==ch)
count++;
return count;
}
/////////////////////////////////////////////////////////////////////////
The function returns a character position that points to any one of the first matched group in Str. NULL if none is returned.
Char *strpbk (char const *s,char const *ACCPET)
{
while (*s! = ')
{
while (*accpet! = ')
if (*accpet++ = = *s)
Return (char *) s;
++s;
}

return NULL;
}
////////////////////////////////////////////////////////////////////////////////
String copy
Char *strcpy1 (char *dest,const char *source)
{
Char *srcdest=dest;//Save the first address of the target string
/*
while (*source! = ')
*dest++ = *source++;
*dest = ' + '; Notation 1
*/
while ((*dest++=*source++)! = ' + ');
return srcdest;
}
/////////////////////////////////////////////////////////////////////////
String copy of the specified length
Char *strncpy1 (char *dest,const char *source,int len)
{
Char *srcdest=dest;//Save the first address of the target string

while (*source! = ' + ' && len--)
*dest++ = *source++;
*dest = ' + '; return srcdest;
}
////////////////////////////////////////////////////////////////////////////
Memory Copy implementation copy non-overlapping memory block void* memcpy1 (void* dst,const void* src,size_t count)
{
char* pbto = (char*) DST;
char* Pbfrom = (char*) src;
ASSERT (dst!= NULL && src! = NULL);//cannot exist null pointer
ASSERT (pbto >= pbfrom+count | | pbfrom >= pbto + count);//Prevent memory overlap (overlap)
while (count--> 0)
{
*pbto++ = *pbfrom++;
}
return DST;
}  //////////////////////////////////////////////////////////////////////////
Memmove implementation move a byte, src source data can not be preserved

void* memmove1 (void* dst,const void* src,size_t count)
{
char* pbto = (char*) DST;
char* Pbfrom = (char*) src;
ASSERT (DST! = NULL && src! = null);//cannot exist null pointer
if (DST <= src | | pbto >= PBFROM + count)//no overlap case, direct copy
{
while (count--> 0)
{
*pbto++ = *pbfrom++;
}
}
Else
{
pbto = pbto + count-1;//overlap case, copy from high address to low
Pbfrom = Pbfrom + count-1;
while (count--> 0)
{
*pbto--= *pbfrom--;
}
}
return DST;
}
/////////////////////////////////////////////////////////////////
Memset: Sets the first count bytes of the memory area indicated by buffer to character C
void * Memset (void* buffer, int c, int count)
{
char* pvto= (char*) buffer;
ASSERT (buffer! = NULL);
while (count-->0)
*pvto++= (char) C;
return buffer;
}
/////////////////////////////////////////////////////////////////////////////
Implementation of string comparison strcmp library function
int strcmp1 (const char *src,const char *DST)
{
int ret;
while (!) ( ret=* (unsigned char *) src-* (unsigned char *) DST) && *DST)//Cycle compare two characters for equality
{
++SRC;
++DST;
}
if (ret<0) ret=-1;
Else
if (ret>0) ret=1;

return (ret);
}
/////////////////////////////////////////////////////////////////////////
Connecting two strings implementation of STRCAT library function
Char *strcat1 (char *dest,const char *src)
{
char *temp = dest;
while (*dest! = ')
dest++;
while ((*dest++ = *src++)! = ' + ');

return temp;
}
////////////////////////////////////////////////////////////////////////////
Connect two specified number of strings implementation of STRNCAT library function
Char *strncat1 (char *dest, const char* src,int num)
{
char *temp = dest;
while (*dest! = ')
dest++;
while ((*dest++ = *src++)! = ' + ' && num--);
return temp;
}
////////////////////////////////////////////////////////////////////////////
The sorting and swapping of strings bubbles:
void Sort1 (char *src,int num)
{
int i,j;
int tmp=0;
for (i=0;i<num;i++)
{
for (j=i+1;j<num;j++)
{
Tmp=src[i];
SRC[I]=SRC[J];
src[j]=tmp;
}
}
}
//////////////////////////////////////////////////////////////////////////
Inverse of a string
Char *strrv1 (const char *STR)
{
int i;
int Len=strlen (str);
char c;
Char *tmp=new char[len+1]; strcpy (TMP,STR);
for (I=0;i<len/2;++i)
{
C=tmp[i];
TMP[I]=TMP[LEN-I-1];
Tmp[len-i-1]=c;
}
return TMP;
}
Pointer implementation
Char *strrv2 (const char *STR)
{
char c;
Char *tmp=new char[strlen (str) +1];
strcpy (TMP,STR); Char *ret=tmp;
Char *p=tmp+strlen (str)-1;
while (P&GT;TMP)
{
c=*tmp;
*tmp=*p;
*p=c;
--p;
++tmp;
}

return ret;
}
Recursive implementation
Char *strrv3 (char *str,int len)
{
if (len<=1)
return str;
Char t=*str;
*str=* (str+len-1);
* (str+len-1) =t; Return (Strrv3 (str+1,len-2)-1);}
///////////////////////////////////////////////////////////////////////
Reverses ABCDEFGH but does not change the internal structure of the word
void Revstr (char *src)
{
Char *start=src,*end=src,*ptr=src;
while (*ptr++! = ')
{
if (*ptr== ' | | | *ptr== ')
{
End=ptr-1;//end point to end of word
while (Start<end)
Swap (*start++,*end--);//reverse the letter of the word
start=end=ptr+1;//point to the beginning of the next word
}
} Start=src,end=ptr-2;//start points to the beginning, end points to the end
while (Start<end)
{
Swap (*start++,*end--);//reverse the entire string
}}
//////////////////////////////////////////////////////////////////////
Determine if it is a palindrome level
int Isrevstr (char *str)
{
int I,len;
int found=1;
if (str==null)
{
return-1;
}
Len=strlen (str);
for (i=0;i<len/2;i++)
{
if (* (str+i)!=* (str+len-i-1))
{
Found=0;
Break
}
}
return found;
}
/////////////////////////////////////////////////////////////////////////
String Loop Right Shift
void Loopmove (char *src,int N)
{
int i=0;
Char *tmp=null;
int strlen=0;
Char *head=src;

while (*src++)
Strlen=src-head-1;
N=n%strlen;
tmp= (char*) malloc (n);
for (i=strlen-1;i<n;i++)
{
Tmp[i]=head[strlen-n+i];
}
for (i=strlen-1;i>=n;i--)
{
HEAD[I]=HEAN[I-N];
}
for (i=0;i<n;i++)
{
Head[i]=tmp[i];
}
Free (TMP);
}
////////////////////////////////////////////////////////////////////////
String right (not implemented with library functions)
Char *loopmove (char *str, int num)
{
Char temp[100]= "";
int i = 0;
int len = 0;
Request length
for (i=0;str[i]!=0;i++)
len++;

Save last Num characters
for (i=0;i<num;i++)
{
Temp[i] = Str[len-num+i];
}

Move the first len-num character backward by num bytes
for (i=0;i<len-num;i++)
{
Str[len-1-i] = Str[len-1-i-num];
}
Put the saved post num characters at the front of str
for (i=0;i<num;i++)
{
Str[i] = Temp[i];
}
return str;
}z

Go to string processing function (not implemented with library functions)

Related Article

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.