C/C++ string processing correlation

Keywords: Programming C

1. C Standard String Function

  1. int strcmp(char * str1, char * str2): comparison function
  2. char * strcpy(char * str1, char * str2): copy function
  3. char * strcat(char * str1, char *str2): Splicing function
  4. int strlen(char * str): length function
  5. char * strstr(char * str1, char * str2): Matching function

In functions like char * strcpy(char * str1, char * str2), the return value is a pointer to str1. The purpose of this is to facilitate the expansion of strcpy, increase its flexibility and support chain expression. For example, the following expression

printf("%s\n",strcpy(dest,src));

1.0 does not use library function to realize the function strcmp

int mystrcmp(char * str1, char * str2)
{
    if (str1 == NULL || str2 == NULL) exit(0);
    while (*str1 != '\0' && *str2 != '\0' && *str1 == *str2)
    {
        str1++;
        str2++;
    }
    if (*str1 == '\0' && *str2 != '\0') return -1;
    else if (*str1 != '\0' && *str2 == '\0') return 1;
    else if (*str1 > *str2) return 1;
    else if (*str1 < *str2) return -1;
    else return 0;
}

1.1 Implement the function strcpy without using library functions

char * mystrcpy(char * dest, char * src)
{
    if (src == NULL || *src == '\0') exit(0);
    while (*src != '\0')
    {
        *dest++ = *src++;
    }
    *dest = '\0';
    return dest;

1.2 Implementing the function strstr without using library functions

char * mystrstr(char * src, char * sub)
{
    if (src == NULL || sub == NULL || *src == '\0' || *sub == '\0')
        exit(0);
    int i = 0, j = 0;
    while (i < strlen(src) && j < strlen(sub))
    {
        if (src[i] == sub[j])
            i++, j++;
        else
        {
            i = i - j + 1;
            j = 0;
        }
    }
    if (j >= strlen(sub)) return src +(i - strlen(sub));
    else return NULL;
}

The difference between 1.3 memcpy and strcpy

1.3.0 memcpy is a memory copy function in C language, which provides general memory copy function, not only for strings. Mainly used for the overall copy of memory blocks, copy data without type restrictions. Its parameters and return values are void * type, which can pass any type of pointer, and its application is more extensive.

void * memcpy(void * dest, const void * src, size_t count)

1.3.1 strcpy is a function designed for copying strings. It copies not only the contents of strings, but also the end identifier'\ 0'. Only legitimate strings can use this function.

char * strcpy(char * str1, char * str2)

1.4 Programming to Realize Right Shift of String Circulation

void loopMoveStr(char * str, int n)
{
    if (str == NULL || n <= 0) exit(0);

    int len = strlen(str);

    if (n > len) n = n % len;

    char  * temp = (char *)malloc(sizeof(char) * n);
    int i;
    for (i = 0; i < n; i++)                                     //Parts that need to be moved to the right are stored in the cache
    {
        temp[i] = str[len - n + i];        
    }

    for (i = 0; i < len - n; i++)                            //Move the remaining len - n strings right in turn
    {
        str[len - 1 - i] = str[len - n - 1 - i];
    }

    for (i = 0; i < n;i++)                                    //Fill the buffer characters in front
    {
        str[i] = temp[i];
    }
    free(temp);
}

1.5 Delete a specified length of substring from the specified position of the string

void delSubStr(char * str, int pos, int len)
{
    if (str == NULL) exit(0);
    int str_len = strlen(str);
    if (pos + len - 1 > str_len || len < 0 || pos < 0) exit(0);

    int i = pos - 1;
    int j = i + len;
    while (str[j] != '\0')
    {
        str[i] = str[j];
        i++;
        j++;
    }
    str[i] = '\0';
}

1.6 Find the maximum number of consecutive occurrences of 0 and 1 in a 0/1 string

void getMaxCount(char * str, int * max0, int * max1)
{
    if (str == NULL) exit(0);
    int len = strlen(str);

    int temp_max0 = 0, temp_max1 = 0;
    int i;
    for (i = 0; i < len;i++)
    {
        if (str[i] == '0')
        {
            if (str[i - 1] == '1' && i != 0)    //If it's the turning point of string 1-0
            {
                if (temp_max1 > *max1)        //Determine whether you need to modify the value of max1
                    *max1 = temp_max1;
                temp_max1 = 0;                    //Zero Temporary Variables
            }
            temp_max0++;                        //Increase 1, record 0 times
        }
        if (str[i] == '1')
        {
            if (str[i - 1] == '0' && i != 0)
            {
                if (temp_max0 > *max0)
                    *max0 = temp_max0;
                temp_max0 = 0;
            }
            temp_max1++;
        }
    }
    if (temp_max1 > *max1)
        *max1 = temp_max1;
    if (temp_max0 > *max0)
        *max0 = temp_max0;
}

1.7 Find the largest common substring in two strings

char * getCommonString(char * str1, char * str2)
{
    char * longer_str, *shorter_str, *sub_str;
    int i, j;

    if (str1 == NULL || str2 == NULL)
        return NULL;

    if (strlen(str1) <= strlen(str2))                        //The pointer longer_str points to a longer string
    {                                                                    //The pointer shorter_str points to a shorter string
        longer_str = str2;
        shorter_str = str1;
    }
    else
    {
        longer_str = str1;
        shorter_str = str2;
    }

    if (strstr(longer_str, shorter_str) != NULL)    //If it's shorter, it's a substring.
        return shorter_str;

    sub_str = (char *)malloc(strlen(shorter_str));    //Application Memory Storage String

    for (i = strlen(shorter_str) - 1; i > 0; i--)            //Outer loop control substring length
    {
        for (j = 0;j < strlen(shorter_str) - i;j++)        //Substring of length bit i obtained by inner loop
        {
            strncpy(sub_str, &shorter_str[j], i);            //Copy substring, length i
            sub_str[i] = '\0';                                        //Add End Mark
            if (strstr(longer_str, sub_str) != NULL)        //Find in a longer string
                return sub_str;
        }
    }
    free(sub_str);                                                    //Not found. Release space.
    return NULL;
}

1.8 String Content Rearrangement

//String content rearrangement
void parseString(char * str)
{
    int len = strlen(str);
    char * temp = (char *)malloc(len);
    
    int a_count = 0, d_count = 0;
    int index1 = 0, index2 = 0, index3 = 0;

    int i;
    for (i = 0; i < len; i++) //Number of letters and numbers in a statistical string
    {
        if ((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z'))
            a_count++;
        else if (str[i] >= '0' && str[i] <= '9')
            d_count++;
    }

    for (i = 0; i < len; i++)        //Rearrange its contents through a circular scan
    {
        if ((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z'))
            temp[index1++] = str[i];        //Save alphabetic characters
        else if (str[i] >= '0' && str[i] <= '9')
            temp[a_count + (index2++)] = str[i];    //Save numeric characters
        else
            temp[a_count + d_count + (index3++)] = str[i];        //Save other characters
    }

    for (i = 0; i < len; i++)        //Copy Temporary Zone Characters to Source Character Array
        str[i] = temp[i];

    free(temp);
}

 

Posted by neroag on Mon, 12 Aug 2019 05:02:59 -0700