C language with string manipulation functions

Source: Internet
Author: User

Header file: my_string.h#ifndef __my_string__#define __my_string__/* hexadecimal numeric string to integer */int ch_to_hex (const  char* val_in,int size);/* Determines whether the given string is a hexadecimal number */int is_hex (const char* val,int  size);/* Determines whether the given string is a numeric *//*r_v:1 (number)  0 (non-numeric) */int is_num (const char* val); */* character to integer number ( */int ch_to_num (Const char* val) for whether it is a pure number);/* The English letter, uppercase and lowercase */void to_upper ( char*  buf, int size);/* Capitalize the English letter, lowercase */void to_lower ( char* buf, int size) /* * Remove the space to the left of the string *//*r_v:-1  conversion failed   converted byte length */int left_trim (char* buf,int size);/* Remove the space to the right of the string *//*r_v:-1  conversion failed   converted byte length */int right_trim (char* buf,int size);/* Remove spaces on both sides of the string *//*r_v:-1   conversion failed   converted byte length */int trim (char* buf, int size);/* reduces the number of contiguous spaces inside a string to 1 spaces */void  Reduce_space (char* buf, int size); Int split (const char* buf_in,int size_in, Char* buf[],int sizE , char separator); Void split_free (char* buf[],int size); #endif 

Source code: MY_STRING.C

#include <string.h>

#include <malloc.h>

#include <memory.h>

/* Determine if the given string is a hexadecimal number */

int Is_hex (const char* val,int size)

{

int status = 1;

char* buf_tmp = malloc (size+1);

memset (buf_tmp,0,size+1);

memcpy (buf_tmp,val,size);

To_upper (buf_tmp,size);

for (int index= 0; index < size; index++)

{

/* hit X to jump over * *

if (index = = 1 && buf_tmp[index]==0x58) continue;

if ((buf_tmp[index]>0x39 && buf_tmp[index] <0x41) | | (unsigned char) buf_tmp[index] <0x30 | | (unsigned char) buf_tmp[index] > 0x46)

{

Status = 0;

Break

}

}

Free (buf_tmp);

return status;

}

/* Determine if the given string is a number */

/*r_v:1 (digital) 0 (not digital) */

int Is_num (const char* val)

{

int status = 1;

int index = 0;

while (Val[index])

{

if (val[index]>0x39 | | val[index] <0x30)

{

/* Non-digital */

Status = 0;

Break

}

index++;

}

return status;

}

/* Hexadecimal numeric string to integer */

int Ch_to_hex (const char* val_in,int size)

{

int result = 0;

int bit_pos = 0;

char* val = malloc (size+1);

memset (val,0,size+1);

memcpy (val,val_in,size);

To_upper (val,size);

for (int index = size-1;index>=0;index--)

{

/* hit X to jump out */

if (index = = 1 && val[index]==0x58) break;

if (val[index]>= 0x30 && Val[index] <=0x39)

{

result = ((Val[index]-0x30) << (bit_pos++ * 4)) | Result

}

Else

{

result = ((Val[index]-0x41 + ten) << (bit_pos++ * 4)) | Result

}

}

Free (val);

return result;

}

/* character to integer number (no judgment on whether it is a pure number) */

int Ch_to_num (const char* val)

{

int index = 0;

/* flag is negative */

int i_negative = 0;

/* Determine if the number in the string has a minus sign */

if (val[index] = = '-')

{

i_negative = 1;

index++;

}

int result = val[index++]-0x30;

while (Val[index])

{

/* Can enter the loop, indicating more than one count */

result = result * 10;

Result + = (val[index++]-0x30);

}

if (i_negative = = 1) result = 0-result;

return result;

}

/* Capitalize the English letter lowercase * *

void To_upper (char* buf, int size)

{

if (!buf | | size <= 0) return;

for (int index = 0; index < size; index++)

{

if (Buf[index] >= 0x61 && Buf[index] <= 0x7A)

{

Buf[index] = Buf[index]-0x20;

}

}

}

/* Capitalize the English letter, lowercase */

void To_lower (char* buf, int size)

{

if (!buf | | size <= 0) return;

for (int index = 0; index < size; index++)

{

if (Buf[index] >= 0x41 && Buf[index] <= 0x5A)

{

Buf[index] = Buf[index] + 0x20;

}

}

}

/* Remove the space to the left of the string */

/*r_v:-1 conversion failed converted byte length */

int Left_trim (char* buf,int size)

{

if (!buf | | size <= 0) return-1;

char* buf_tmp = malloc (size + 1);

memset (buf_tmp,0,size + 1);

int index = 0;

for (index = 0; index < size; index++)

{

if (Buf[index]! = 0x20) break;

}

memcpy (Buf_tmp,buf+index,size-index);

strcpy (BUF,BUF_TMP);

Free (buf_tmp);

return size-index;

}

/* Remove the space to the right of the string */

/*r_v:-1 conversion failed converted byte length */

int Right_trim (char* buf,int size)

{

if (!buf | | size <= 0) return-1;

char* buf_tmp = malloc (size + 1);

memset (buf_tmp,0,size + 1);


int index = 0;

for (index = size-1;index >= 0; index--)

{

if (Buf[index]! = 0x20) break;

}

memcpy (buf_tmp,buf,index+1);

strcpy (BUF,BUF_TMP);

Free (buf_tmp);

return index+1;

}

/* Remove spaces on both sides of the string */

/*r_v:-1 conversion failed converted byte length */

int trim (char* buf, int size)

{

int result = 0;

if ((result= Left_trim (buf,size)) = =-1) return-1;

if ((result= Right_trim (buf,result)) = =-1) return-1;

return result;

}

/* Reduce the number of consecutive spaces inside a string to 1 spaces */

void Reduce_space (char* buf, int size)

{

int index = 0;

int index_tmp = 0;

int flag = 0;

char* buf_tmp = malloc (size+1);

memset (buf_tmp,0,size+1);

for (index =0;index<size;index++)

{

if (Buf[index]! = 0x20)

{

if (flag = = 1)

{

buf_tmp[index_tmp++] = 0x20;

Flag = 0;

}

buf_tmp[index_tmp++] = Buf[index];

}

Else

{

flag = 1;

}

}

memset (buf,0,size);

memcpy (BUF,BUF_TMP,INDEX_TMP);

Free (buf_tmp);

Return

}

int split (const char* buf_in,int size_in,char* buf[],int size, char separator)

{

int index = 0;

int count = 0;

int flag = 0;

for (index = 0;index < size_in; index++)

{

if (buf_in[index] = = separator)

{

if (count = = size) return count;

Buf[count] = malloc (Index-flag + 1);

memset (buf[count],0,index-flag+1);

memcpy (Buf[count++],&buf_in[flag],index-flag);

Flag = index + 1;

}

}

Buf[count] = malloc (Index-flag + 1);

memset (buf[count],0,index-flag+1);

memcpy (Buf[count++],&buf_in[flag],index-flag);

return count;

}

void Split_free (char* buf[],int size)

{

for (int index = 0; index < size; index++)

{

Free (Buf[index]);

}

}


C language with string manipulation 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.