c: string

 

/**
 * *****************************************************************************
 * @file        duSortType.h
 * @brief       
 * @author       ()
 * @date        2023-10-18
 * @copyright   geovindu
 * *****************************************************************************
 */

#include<ctype.h>
#include<stdlib.h>
#include<string.h>

/**
 * @brief   整数从大到小排序    
 * @param a
 * @param b
 * @return
 */
int IntCmpDesc(const void *a,const void *b);

/**
 * @brief     整数排序从小到大  
 * @param a
 * @param b
 * @return
 */
int IntCmpAsc(const void *a,const void *b);



/**
 * @brief     排序从小到大
 * @param a
 * @param b
 * @return
 */
int strCmpAsc(const void *srca,const void *srcb);

/**
 * @brief     排序从大到小
 * @param a
 * @param b
 * @return
 */
int strCmpDsc(const void *srca,const void *srcb);

  

/**
 * *****************************************************************************
 * @file        duSortType.c
 * @brief       
 * @author       ()
 * @date        2023-10-18
 * @copyright   geovindu
 * *****************************************************************************
 */

#include"include/duSortType.h"
#include<ctype.h>
#include<stdlib.h>
#include<string.h>


/**
 * @brief   整数从大到小排序    
 * @param a
 * @param b
 * @return
 */
int IntCmpDesc(const void *a,const void *b)
{
	return *(int*)b-*(int*)a;
}
/**
 * @brief     整数排序从小到大  
 * @param a
 * @param b
 * @return
 */
int IntCmpAsc(const void *a,const void *b)
{
	return *(int*)a-*(int*)b;
}


/**
 * @brief     排序从小到大
 * @param a
 * @param b
 * @return
 */
int strCmpAsc(const void *a,const void *b)
{
	return *(char*)a-*(char*)b;
}

/**
 * @brief     排序从大到小
 * @param a
 * @param b
 * @return
 */
int strCmpDsc(const void *a,const void *b)
{
	return *(char*)b-*(char*)a;
}

  

/*****************************************************************//**
 * \file   Dustring.h
 * \brief 
 * IDE: VSCODE   c11 
 *
 * int isdigit(int c)   如果c是一个数字,返回true,否则返同 false
 * int isalpha(int c)  如果c是一个字母,返回true,否则返回 false
 * int isalnum(int c)  如果 c 是一个字母或数字,返回 true,否则返同 false
 * int isxdigit(int c)  如果c是一个十六进制字符,返回 true,否则返回 false
 * int islower(int c)  如果c是一个小写字母,返回 true,否则返回 false
 * int isupper(int c)  如果c是一个大写字母,返回true,否则返回 false
 * int isspace(int c)   如果c是一个空白符,返回 true,否则返回 false 空白符包括,"n,空格,W,V进纸符(),垂直制表符(“v)
 * int iscntrl(int c)  如果c是一个控制符,返回 true ,否则返回 false
 * int ispunct(int c)   如果c是一个除空格、数字和字母外的可打印字符,返回 true,否则返回 false
 * int isprint(int c)  如果c 是一个可打印符 (包括空格),返回 true,否则返回 false
 * int isgraph(int c)   如果c 是除空格之外的可打印字符,返回 true,否则返回 false
 
 * 站在巨人的肩膀上 Standing on the Shoulders of Giants
 * \author geovindu,Geovin Du 涂聚文
 * \date   2023-10-15
***********************************************************************/
 
#ifndef DUSTRING_H
#define DUSTRING_H
 
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>

 
/**
 * @brief 从左截取字符串
 *
 * @param dst 要选择的字会串存储的数组(预设长度)
 * @param src 源字符串
 * @param n 取几位
 * @return char* 返回要截取的字符串
 */
char *strLeft(char *dst,char *src,int n);
 
 
/**
 * @brief 从右截取字符串
 *
 * @param dst 要选择的字会串存储的数组(预设长度)
 * @param src 源字符串
 * @param n 取几位
 * @return char* 返回要截取的字符串
 */
char *strRight(char *dst,char *src,int n);
 
 
/**
 * @brief 截取字符串
 *
 * @param dst 要选择的字会串存储的数组(预设长度)
 * @param src 源字符串
 * @param start 开始位置
 * @param len 取几位
 * @return char*  返回要截取的字符串
 */
char *strSubstring(char *dst,char *src,int start,int len);
 
/**
 * @brief 字符串替换
 *
 * @param search
 * @param replace
 * @param subject
 * @param bufsize
 * @return char*
 */
char *strReplace(const char *search, const char *replace, char *subject, int bufsize);
 
/**
 * @brief  删除两端空格
 *
 * @param scr
 * @return int
 */
int trim(char *scr);
 
 
 
/**
 * @brief 字符字替换
 *
 * @param src 原字符串
 * @param oldstr  要替的旧字串
 * @param newstr  要替成的新字串
 * @return char* 返回替好的字符串
 */
char *duReplace(char *src,char *oldstr,char *newstr);
 
 
/**
 * @brief 排序OK      
 * @param 原字符串数组
 * @param 排序后的字符串数组
 * @returns 返回值 不OK null 
 */
char* duSortAsc(char* src,char* newchar,int lenght);


/**
 * @brief 排序OK 
 * @param 原字符串数组
 * @param  排序后的字符串数组
 * @returns 返回值 OK
 */
char* duSortAsc2(char* src,char* newchar[],int lenght);

 /**
 * @brief       
 * @param  dst
 * @param str
 * @param spl
 * 示例  
 *   char str[] = "what is your name?";
 *   char dst[10][80];
 *   int cnt = split(dst, str, " ");
 *   for (int i = 0; i < cnt; i++)
 *       puts(dst[i]);
 * 
 */
int strSplit(char dst[][80], char* str, const char* spl);


/**
 * @brief   排序从大到小     
 * @param src 原字符串数组
 * @param newchar 排序后的字符串数组
 * @param lenght 字符串数组长度  
 * @return
 * 
 */
char* duStrCmpSortAsc(char* src,char* newchar[],int lenght);

/**
 * @brief   排序从大到小     
 * @param src 原字符串数组
 * @param newchar 排序后的字符串数组
 * @param lenght 字符串数组长度  
 * @return
 * 
 */
char* duStrCmpSortDesc(char* src,char* newchar[],int lenght);


/**
 * @brief   排序从小到大
 * @param src 原字符串数组
 * @param newint 排序后的字符串数组
 * @param lenght 字符串数组长度  
 * @return
 * 
 */
int* duIntCmpSortAsc(int* src,int* newint[],int lenght);



/**
 * @brief   排序从大到小    
 * @param src 原字符串数组
 * @param newint 排序后的字符串数组
 * @param lenght 字符串数组长度  
 * @return
 * 
 */
int* duIntCmpSortDesc(int* src,int* newint[],int lenght);




#endif

  

 

/*****************************************************************//**
 * \file   Dustring.c
 * \brief  字符串操作
 * IDE: VSCODE   c11 
 *站在巨人的肩膀上 Standing on the Shoulders of Giants
 *
 * \author geovindu,Geovin Du 涂聚文
 * \date   2023-10-15
***********************************************************************/
 
#include "include/Dustring.h"
#include<ctype.h>
#include<stdlib.h>
#include<string.h>
#include "include/duSortType.h"

/**
 * @brief 从左截取字符串
 *
 * @param dst 要选择的字会串存储的数组(预设长度)
 * @param src 源字符串
 * @param n 取几位
 * @return char* 返回要截取的字符串
 */
char *strLeft(char *dst,char *src,int n)
{
     
    char *p=src;
    char *q=dst;
    int len=strlen(src);
    //isupper()
    //islower()
 
    if(n>len) n=len;
    while(n--) *(q++)=*(p++);
    *(q++)='\0';
    return dst;
}
 
/**
 * @brief 从右截取字符串
 *
 * @param dst 要选择的字会串存储的数组(预设长度)
 * @param src 源字符串
 * @param n 取几位
 * @return char* 返回要截取的字符串
 */
char *strRight(char *dst,char *src,int n)
{
    char *p=src;
    char *q=dst;
    int len=strlen(src);
    if(n>len) n=len;
    p+=(len-n);
    while(*(q++)=*(p++));
    return dst;
}
 
/**
 * @brief 截取字符串
 *
 * @param dst 要选择的字会串存储的数组(预设长度)
 * @param src 源字符串
 * @param start 开始位置
 * @param len 取几位
 * @return char*  返回要截取的字符串
 */
char *strSubstring(char *dst,char *src,int start,int len)
{ 
    char *p=dst; 
    char *q=src; 
    int length=strlen(src);
    if(start>=length||start<0)
        return NULL; 
    if(len>length)
        len=length-start;
    q+=start;
    while(len--) 
    {  
        *(p++)=*(q++);
    } 
    *(p++)='\0'; 
    return dst;
}
 
 
/**
 * @brief 字符串替换
 *
 * @param search
 * @param replace
 * @param subject
 * @param bufsize
 * @return char*
 */
char *strReplace(const char *search, const char *replace, char *subject, int bufsize)
{
    char *p;
    int i, j = 0, n = 0;
    int len, slen, rlen, tlen;
     
    // check arguments
    if (subject == NULL)
    {
        return NULL;
    }
    tlen = strlen(subject);
    if (bufsize == -1)
    {
        // make sure subject is writable
        p = malloc(tlen + 1);
        if (p == NULL)
        {
            return NULL;
        }
        memcpy(p, subject, tlen + 1);
        subject = p;
    }
    if (search == NULL || search[0] == '\0')
    {
        return subject;
    }
    slen = strlen(search);
    if (replace == NULL)
    {
        replace = "";
    }
    rlen = strlen(replace);
  
    // find out how many replacements are needed
    for (i = 0; subject[i] != '\0'; i++)
    {
        if (subject[i] != search[j])
        {
            // character mismatch
            j = 0;
        }
        if (subject[i] == search[j])
        {
            j++;
            if (j == slen)
            {
                // pattern match
                subject[i + 1 - j] = '\0'; // front
                subject[i] = '\0'; // rear
                j = 0;
                n++;
            }
        }
    }
    if (n == 0)
    {
        return subject;
    }
  
    // calculate the length of the new string and check buffer size
    len = tlen + n * (rlen - slen);
    if (bufsize == -1)
    {
        if (len > tlen)
        {
            // increase buffer size
            p = realloc(subject, len + 1);
            if (p == NULL)
            {
                free(subject);
                return NULL;
            }
            subject = p;
        }
    }
    else if (bufsize < len + 1)
    {
        return NULL;
    }
  
    // replace substrings
    if (len <= tlen)
    {
        i = 0; // for read
        j = 0; // for write
        while (i < tlen)
        {
            if (subject[i] == '\0')
            {
                memcpy(subject + j, replace, rlen);
                i += slen;
                j += rlen;
            }
            else
            {
                subject[j] = subject[i];
                i++;
                j++;
            }
        }
    }
    else
    {
        i = tlen - 1;
        j = len - 1;
        while (i >= 0)
        {
            if (subject[i] == '\0')
            {
                memcpy(subject + j + 1 - rlen, replace, rlen);
                i -= slen;
                j -= rlen;
            }
            else
            {
                subject[j] = subject[i];
                i--;
                j--;
            }
        }
    }
    subject[len] = '\0';
    return subject;
}
  
/**
 * @brief  删除两端空格
 *
 * @param scr
 * @return int
 */
int trim(char *scr)
{
    int i, j = -1, len = 0;
  
    for (i = 0; scr[i] != '\0'; i++)
    {
        if (j == -1 && scr[i] != ' ' && scr[i] != '\t')
        {
            j = 0;
        }
        if (j != -1)
        {
            scr[j] = scr[i];
            j++;
            if (scr[i] != ' ' && scr[i] != '\t')
            {
                len = j;
            }
        }
    }
    scr[len] = '\0';
    return len;
}
 
/**
 * @brief 字符字替换
 *
 * @param src 原字符串
 * @param oldstr  要替的旧字串
 * @param newstr  要替成的新字串
 * @return char* 返回替好的字符串
 */
char *duReplace(char *src,char *oldstr,char *newstr)
{
 
    char bstr[strlen(src)];//转换缓冲区
    memset(bstr,0,sizeof(bstr));
    for(int i = 0;i < strlen(src);i++){
            if(!strncmp(src+i,oldstr,strlen(oldstr))){//查找目标字符串
                strcat(bstr,newstr);
                i += strlen(oldstr) - 1;
            }
            else
            {
                strncat(bstr,src + i,1);//保存一字节进缓冲区
            }
    }
    strcpy(src,bstr);
    return src;
 
}

/**
 * @brief 排序OK      从大到小
 * @param src 原字符串数组
 * @param newchar 排序后的字符串数组
 * @param lenght 字符串数组长度  
 * @returns 返回值 不OK null 
 */
char* duSortDesc(char* src,char* newchar,int lenght)
{
    char bstr[strlen(src)];//转换缓冲区  需要考虑,否则重复用,会出现问题
    memset(bstr,0,sizeof(bstr));
    int lentf=strlen(src);
    //printf("%d\n",lentf);
    //char* newstr[lenght];
    int digits[10] = {0}; // 存储数字出现的次数
    for (int i = 0; src[i] != '\0'; i++) {
        if (isdigit(src[i])) {
            digits[src[i] - '0']++;
        }
    }
    for (int i = 9; i >= 0; i--) {
        for (int j = 0; j < digits[i]; j++) {
            char digitChar = i + '0';
            
            //*(newchar+j)=digitChar;
            //*(newchar+i)=digitChar;
            //printf("%c",digitChar);
            //newchar[j]=digitChar;
            strncat(newchar, &digitChar, 1);
            //strncat(newstr, &digitChar, 1);
        }
    }

    return newchar;  //newstr 返回这个值有点不准确

}

/**
 * @brief 排序OK  从大到小
 * @param  src 原字符串数组
 * @param  newchar 排序后的字符串数组
 * @param lenght 字符串数组长度
 * @returns 返回值 OK
 */
char* duSortDesc2(char* src,char* newchar[],int lenght)
{
    char bstr[strlen(src)];//转换缓冲区  需要考虑,否则重复用,会出现问题
    memset(bstr,0,sizeof(bstr));
    int lentf=strlen(src);
    //const lstr=lenght;
    //printf("%d\n",lentf);
    //char* newstr[lentf];
    int digits[10] = {0}; // 存储数字出现的次数
    for (int i = 0; src[i] != '\0'; i++) {
        if (isdigit(src[i])) {
            digits[src[i] - '0']++;
        }
    }
    for (int i = 9; i >= 0; i--) {
        for (int j = 0; j < digits[i]; j++) {
            char digitChar = i + '0';
            
            //*(newchar+j)=digitChar;
            //*(newchar+i)=digitChar;
            //printf("%c",digitChar);
            //newchar[j]=digitChar;
            strncat(newchar, &digitChar, 1);
            //strncat(newstr, &digitChar, 1);
        }
    }

    return newchar;

}

/**
 * @brief       
 * @param  dst
 * @param str
 * @param spl
 * 示例  
 *   char str[] = "what is your name?";
 *   char dst[10][80];
 *   int cnt = split(dst, str, " ");
 *   for (int i = 0; i < cnt; i++)
 *       puts(dst[i]);
 * 
 */
int strSplit(char dst[][80], char* str, const char* spl)
{
    int n = 0;
    char *result = NULL;
    result = strtok(str, spl);
    while( result != NULL )
    {
        strcpy(dst[n++], result);
        result = strtok(NULL, spl);
    }
    return n;
}



/**
 * @brief   排序从小到大
 * @param src 原字符串数组
 * @param newint 排序后的字符串数组
 * @param lenght 字符串数组长度  
 * @return
 * 
 */
int* duIntCmpSortAsc(int* src,int* newint[],int lenght)
{
    int bstr[sizeof(src)/sizeof(src[0])];//转换缓冲区  需要考虑,否则重复用,会出现问题
    memset(bstr,0,sizeof(bstr));
    int lentf=strlen(src);
    int* newstr[lentf];
	qsort(src,strlen(src),sizeof(src[0]),IntCmpAsc);
	for(int i=0;i<lenght;i++)
    {
        printf("%d ",src[i]);
        strncat(newint,src[i], 1);
        strncat(newstr, src[i], 1);
    }		
    return newint;
}

/**
 * @brief   排序从大到小    
 * @param src 原字符串数组
 * @param newint 排序后的字符串数组
 * @param lenght 字符串数组长度  
 * @return
 * 
 */
int* duIntCmpSortDesc(int* src,int* newint[],int lenght)
{
    int bstr[sizeof(src)/sizeof(src[0])];//转换缓冲区  需要考虑,否则重复用,会出现问题
    memset(bstr,0,sizeof(bstr));
    int lentf=strlen(src);
    int* newstr[lentf];
	qsort(src,strlen(src),sizeof(src[0]),IntCmpDesc);
	for(int i=0;i<lenght;i++)
    {
        printf("%d ",src[i]);
        strncat(newint,src[i], 1);
        strncat(newstr, src[i], 1);
    }		
    return newint;
}




/**
 * @brief   排序从大到小     
 * @param src 原字符串数组
 * @param newchar 排序后的字符串数组
 * @param lenght 字符串数组长度  
 * @return
 * 
 */
char* duStrCmpSortAsc(char* src,char* newchar[],int lenght)
{
    char bstr[strlen(src)];//转换缓冲区  需要考虑,否则重复用,会出现问题
    memset(bstr,0,sizeof(bstr));
    int lentf=strlen(src);
    char* newstr[lentf];
	qsort(src,strlen(src),sizeof(src[0]),strCmpAsc);
	for(int i=0;i<lenght;i++)
    {
        printf("%c ",src[i]);
        strncat(newchar,src[i], 1);
        strncat(newstr, src[i], 1);
    }		
    return newchar;
}

/**
 * @brief   排序从大到小     
 * @param src 原字符串数组
 * @param newchar 排序后的字符串数组
 * @param lenght 字符串数组长度  
 * @return
 * 
 */
char* duStrCmpSortDesc(char* src,char* newchar[],int lenght)
{
    //char bstr[strlen(src)];//转换缓冲区  需要考虑,否则重复用,会出现问题
    //memset(bstr,0,sizeof(bstr));
    const int lentf=strlen(src);
    printf("原始字符串:%s \n",src);
    //int firstn=sizeof(src[0]);
    char* newstr[lenght];
	qsort(src,strlen(src),sizeof(src[0]),strCmpDsc);

	for(int i=0;i<strlen(src);i++)
    {
        //printf("%c ",src[i]);
        //strncat(newchar,src[i], 1);
        if (isdigit(src[i])){
            char va=src[i] ;
            printf("%c ",va);
            strncat(newstr, &va, 1);
            strncat(newchar, &va,1);
        }
        else
        {
            char va=src[i];
            strncat(newstr, &va, 1);
            strncat(newchar, &va,1);

        }
        //newchar[i]=src[i];        
        //newstr[i]=src[i];
    }		
    return newchar;
}

 

    printf("hello c world \n");
    printf("你好,中国\n");
    //char a[10]="1092367145"; //变量名重复赋值,这个编译出问题

    char bbdu[10]="1092367145";

    /*
    char* c[10]={'\0'};
	qsort(a,strlen(a),sizeof(a[0]),cmp);
	for(int i=0;i<=10;i++)
    {
        char du=a[i];
        printf("%c ",du);
        strncat(c, &du, 1);
        //c[i]=du;
    }
	printf("\nc=:%s length=:%d\n",c,strlen(c));
    */
    char* charqs[10]={'\0'};//NULL; //
    char* reqs;    
    
    
    printf("\n 原串: %s",bbdu);
    reqs=duStrCmpSortDesc(bbdu,charqs,10);   
    printf("\nchar:%s lenght=:%d\n",charqs,strlen(charqs));
    printf("\nchar:%s\n",reqs);



    //分割字符串   https://githubmota.github.io/2017/12/29/2017-12-29-Linux-C-Split/
    //如何找到数字字符串的首位数和末位数索引
    char ssss[] = "GXZXLeaag%^*** 1092367145 &*@654123HUYqianrushi";  
    char delim[] = " ,!";  

    char *token;  
    for(token = strtok(ssss, delim); token != NULL; token = strtok(NULL, delim)) {  
        printf(token);  
        printf("\n\f");  
    }  
    printf("\n"); 
     




    char *dustr = "GXZXLeaag%^*** 1092367145 &*@654123HUYqianrushi"; //这个不可以用替换函数,需要索引长度大于自身的宽度
    char *substr = "109236714533";
    char *substr2 = "654123";
    char geovindu[100] = "GXZXLeaag%^*** 1092367145 &*@654123HUYqianrushi"; //必须索引值大,否则不可以替换
    char *newdig[10];
    char *digg;
    char *sdu;
    char *newdig2[6];
    



    char newstrd[10]={0};//初始化赋值
    char *ddu[10]={'\0'};; //初始化赋值
    int digitsdu[10] = {0}; // 存储数字出现的次数
    for (int i = 0; substr[i] != '\0'; i++) {
        if (isdigit(substr[i])) {
            digitsdu[substr[i] - '0']++;
        }
    }
    for (int i = 9; i >= 0; i--) {
        for (int j = 0; j < digitsdu[i]; j++) {
            char digitChar = i + '0';
            strncat(newstrd, &digitChar, 1);
            //printf("%c",digitChar);
            strncat(ddu, &digitChar, 1);
        }
    }

    char newstrd2[6]={0}; //初始化赋值
    char *ddu2[6]={'\0'};//初始化赋值
    int digitsdu2[10] = {0}; // 存储数字出现的次数
    for (int i = 0; substr2[i] != '\0'; i++) {
        if (isdigit(substr2[i])) {
            digitsdu2[substr2[i] - '0']++;
        }
    }
    for (int i = 9; i >= 0; i--) {
        for (int j = 0; j < digitsdu2[i]; j++) {
            char digitChar = i + '0';
            strncat(newstrd2, &digitChar, 1);
            //*(newchar+j)=digitChar;
            //*(newchar+i)=digitChar;
            //printf("%c",digitChar);
            //ddu2[i]=digitChar;
            strncat(ddu2, &digitChar, 1);
        }
    }

    printf("\n1: %s\n",newstrd);
    printf("2: %s\n",ddu);
    printf("3: %s\n",newstrd2);
    printf("4: %s\n",ddu2);




    char *newddd=newstrd;
    char *fff;
    char *kk;

    char dustr11[10] = "1092367145";
    char dustr22[6] = "654123";
    digg=duStrCmpSortDesc(dustr11,newdig,10);
    kk=duStrCmpSortDesc(dustr22,newdig2,6);
    char* Olddustr11 = "1092367145";
    char* Olddustr22 = "654123";
    fff=duReplace(geovindu,Olddustr11,ddu); //ddu
    printf("fff1:%s\n",fff);
    fff=duReplace(geovindu,Olddustr22,ddu2);  
    printf("fff2:%s\n",fff);
    printf("\nnew:%s\n",digg);
    printf("newdig:%s\n",newdig);
    printf("newdig2:%s\n",newdig2);
    printf("kk:%s\n",kk);
    printf("fff3:%s\n",fff);



    //查找索引
    char *dus =strstr(dustr,substr); //
    char *dus2=strstr(dustr,substr2);//
    if(dus==NULL)
		printf("can't find %s in %s\n",substr,dustr);
	else
        printf("%s include %s;show the string from start found address:%s\n", dustr,substr,dus);
     //起始索引   
     int index=dus - dustr;
     int index2=dus2-dustr;
     //结束索引
     int endindex=index+strlen(substr);
     int endindex2=index2+strlen(substr2);
     printf("1092367145 start index:%d,end:%d.\n",index,endindex);
     printf("654123 start index:%d,end:%d.\n",index2,endindex2);   

    char ch[100] = "GXZXLeaag%^*** 1092367145 &*@654123HUYqianrushi";
    char duresult[100]; // 存储处理后的结果

    char gx[]="GXZX";
   
    char *dup;
    char *gxdup;
    char newstr[100];
    char *des;
    dup=fff; //fff
    char gxz[]="高训中心";
    //strcat(newstr, "高训中心");    
    strncat(newstr,&gxz,8);  
    //printf("%s/n",gxdup);
    int gxlen=strlen("高训中心");
    //*(dup+14)='d';
    int l=sizeof(ch)/sizeof(ch[0]);
    //倒序显示
    //for(int i=l;i>=0;i--)
    //{
        // printf("%c\n",dup[i]); //
        
    // }
    //顺序显示
    for(int i=4;i<l;i++)
    {
    
        if(dup[i]>='A' && dup[i]<='Z')
        {
            printf("%c\n",dup[i]); 
            char sd=dup[i];
            strncat(newstr,&sd,1);  

        }
        else if(dup[i]>='a' && dup[i]<='z')
        {
            printf("%c\n",toupper(dup[i]));
            *(dup+i)=toupper(dup[i]);
            char sx=toupper(dup[i]);
             strncat(newstr,&sx,1);               
        }
        else if(isdigit(dup[i]))
        {
            printf("數字:%c\n",dup[i]);
            char sszi=dup[i];
            strncat(newstr,&sszi,1);   
        }
        else
        {
    
            //des=newstr;
            //des=dup[i];
            //strcat(des[1],dup[i]);  //strcpy(des, dup[i]);
            //memset(des, '\0', sizeof(des));
            //strcat(&des,&dup[i]);
            //*(des+i) = '*';
            //*(des+i+1)=dup[i];
            //printf("%c\n",dup[i]);
            strncat(newstr, &ch[i], 1);
            strncat(newstr, &ch[i], 1);
        }
        //
        
    }
    gxdup=duReplace(ch,"GXZX","高训中心");
    printf("gx=%s\n",gxdup);
    printf("newstr=  %s\n",newstr);
    // (1) 将GXZX前四个字符串中的大写字母转换成“高训中心”
    for (int i = 0; i < 4; i++) {
        if (isupper(ch[i])) {
            strcat(duresult, "高训中心");
        } else {
            // (2) 将字符串中其余的小写字母转换成大写字母
            char uppercaseChar = toupper(ch[i]);
            strncat(duresult, &uppercaseChar, 1);
        }
    }

    // (3) 数字降序排序
    int digits[10] = {0}; // 存储数字出现的次数
    for (int i = 0; ch[i] != '\0'; i++) {
        if (isdigit(ch[i])) {
            digits[ch[i] - '0']++;
        }
    }
    for (int i = 9; i >= 0; i--) {
        for (int j = 0; j < digits[i]; j++) {
            char digitChar = i + '0';
            strncat(duresult, &digitChar, 1);
        }
    }

    // (4) 特殊符号加倍输出
    for (int i = 4; ch[i] != '\0'; i++) {
        if (!isalpha(ch[i]) && !isdigit(ch[i])) {
            strncat(duresult, &ch[i], 1);
            strncat(duresult, &ch[i], 1);
        }
    }

    printf("处理后的字符串:%s\n", duresult);

  

 

 

 

 

 

    printf("hello world, c \n");
    printf("你好,中国\n");
    char ch[100] = {"GXZXLeaag^*** 1092367145 &*@654123HUYqianrushi" };
    int lennum=sizeof(ch)/sizeof(ch[0]);
    char *des[4];//={0};
    char *gx[0]={{"高训中心"}};
    
    int slen=strlen(ch);
    char *rdes[slen-4];
    printf("%s\n",strRight(rdes,ch,slen-4));
  
    printf("%s\n",strLeft(des,ch,4));
    printf(des);
    if(des=="GXZX")
    {
        printf("%s",gx[0]);
    }
       

  

    printf("hello world, c \n");
    printf("你好,中国\n");
    char ch[100] = {"GXZXLeaag^*** 1092367145 &*@654123HUYqianrushi" };
    int lennum=sizeof(ch)/sizeof(ch[0]);
    char *des[8];//={0};
    char *gx[0]={{"高训中心"}};
    char *newch[100];
    printf("%s\n",duReplace(ch,"GXZX","高训中心"));
    
    int slen=strlen(ch);
    char *rdes[slen-8];
    printf("%s\n",strRight(rdes,ch,slen-8));
  
    printf("%s\n",strLeft(des,ch,8));
    printf(des);
    if(des=="GXZX")
    {
        printf("%s",gx[0]);
    }

  

 

    printf("hello world, c \n");
    printf("你好,中国\n");

    char ch[] = {"GXZXLeaag^*** 1092367145 &*@654123HUYqianrushi" };
    char *dup;
    char newstr[50];
    char *des;
    dup=ch;
    *(dup+14)='d';
    int l=sizeof(ch)/sizeof(ch[0]);
    //倒序显示
    //for(int i=l;i>=0;i--)
    //{
       // printf("%c\n",dup[i]); //
      
   // }
    //顺序显示
    for(int i=0;i<l;i++)
    {

        if(dup[i]>='A' && dup[i]<='Z')
        {
            printf("%c\n",dup[i]);    
        }
        else if(dup[i]>='a' && dup[i]<='z')
        {
            printf("%c\n",toupper(dup[i]));
            *(dup+i)=toupper(dup[i]);
        }
        else if(isdigit(dup[i]))
        {
            printf("數字:%c\n",dup[i]);
        }
        else
        {
 
            des=newstr;
            //des=dup[i];
            //strcat(des[1],dup[i]);  //strcpy(des, dup[i]);
             //memset(des, '\0', sizeof(des));
            //strcat(&des,&dup[i]);
            *(des+i) = '*';
            *(des+i+1)=dup[i];
            printf("%c\n",dup[i]); 
            
        }
         //
        
    }
     printf("%s\n",dup); 
    //printf("%s",dup);
    //*(p+10)="G";
    char str1[14] = "涂聚文";
   char str2[14] = "google";
   char str3[14];
   int  len ;
 
   /* 复制 str1 到 str3 */
   strcpy(str3, str1);
   printf("strcpy( str3, str1) :  %s\n", str3 );
 
   /* 连接 str1 和 str2 */
   strcat( str1, str2);
   printf("strcat( str1, str2):   %s\n", str1 );
 
   /* 连接后,str1 的总长度 */
   len = strlen(str1);
   printf("strlen(str1) :  %d\n", len );

  

 

 printf("hello world, c \n");
    printf("你好,中国\n");



    char *dustr = "GXZXLeaag%^*** 1092367145 &*@654123HUYqianrushi"; //这个不可以用替换函数,需要索引长度大于自身的宽试
    char *substr = "1092367145";
    char *substr2 = "654123";
    char geovindu[100] = "GXZXLeaag%^*** 1092367145 &*@654123HUYqianrushi"; //必须索引值大,否则不可以替换
    char *newdig[10];
    char *digg;
    char *sdu;
    char *newdig2[6];




    char newstrd[10];
    char *ddu[10];
    int digitsdu[10] = {0}; // 存储数字出现的次数
    for (int i = 0; substr[i] != '\0'; i++) {
        if (isdigit(substr[i])) {
            digitsdu[substr[i] - '0']++;
        }
    }
    for (int i = 9; i >= 0; i--) {
        for (int j = 0; j < digitsdu[i]; j++) {
            char digitChar = i + '0';
            strncat(newstrd, &digitChar, 1);
            printf("%c",digitChar);
            strncat(ddu, &digitChar, 1);
        }
    }

    char newstrd2[10];
    char *ddu2[10];
    int digitsdu2[10] = {0}; // 存储数字出现的次数
    for (int i = 0; substr2[i] != '\0'; i++) {
        if (isdigit(substr2[i])) {
            digitsdu2[substr2[i] - '0']++;
        }
    }
    for (int i = 9; i >= 0; i--) {
        for (int j = 0; j < digitsdu2[i]; j++) {
            char digitChar = i + '0';
            strncat(newstrd2, &digitChar, 1);
            //*(newchar+j)=digitChar;
            //*(newchar+i)=digitChar;
            printf("%c",digitChar);
            //ddu2[i]=digitChar;
            strncat(ddu2, &digitChar, 1);
        }
    }

    printf("\n1: %s\n",newstrd);
    printf("2: %s\n",ddu);
    printf("3: %s\n",newstrd2);
    printf("4: %s\n",ddu2);




    char *newddd=newstrd;
    char *fff;
    char *kk;
    digg=duSortAsc(substr,newdig,10);
    kk=duSortAsc2(substr2,newdig2,6);

    fff=duReplace(geovindu,substr,ddu);
    printf("fff:%s\n",fff);
    fff=duReplace(fff,substr2,kk);  

    printf("\nnew:%s\n",digg);
    printf("newdig:%s\n",newdig);
    printf("newdig2:%s\n",newdig2);
    printf("kk:%s\n",kk);
    printf("fff:%s\n",fff);



    //查找索引
    char *dus =strstr(dustr,substr); //
    char *dus2=strstr(dustr,substr2);//
    if(dus==NULL)
		printf("can't find %s in %s\n",substr,dustr);
	else
        printf("%s include %s;show the string from start found address:%s\n", dustr,substr,dus);
     //起始索引   
     int index=dus - dustr;
     int index2=dus2-dustr;
     //结束索引
     int endindex=index+strlen(substr);
     int endindex2=index2+strlen(substr2);
     printf("1092367145 start index:%d,end:%d.\n",index,endindex);
     printf("654123 start index:%d,end:%d.\n",index2,endindex2);   

    char ch[100] = "GXZXLeaag%^*** 1092367145 &*@654123HUYqianrushi";
    char duresult[100]; // 存储处理后的结果

    char gx[]="GXZX";
   
    char *dup;
    char *gxdup;
    char newstr[100];
    char *des;
    dup=fff;
    char gxz[]="高训中心";
    //strcat(newstr, "高训中心");    
    strncat(newstr,&gxz,8);  
    //printf("%s/n",gxdup);
    int gxlen=strlen("高训中心");
    //*(dup+14)='d';
    int l=sizeof(ch)/sizeof(ch[0]);
    //倒序显示
    //for(int i=l;i>=0;i--)
    //{
        // printf("%c\n",dup[i]); //
        
    // }
    //顺序显示
    for(int i=4;i<l;i++)
    {
    
        if(dup[i]>='A' && dup[i]<='Z')
        {
            printf("%c\n",dup[i]); 
            char sd=dup[i];
            strncat(newstr,&sd,1);  

        }
        else if(dup[i]>='a' && dup[i]<='z')
        {
            printf("%c\n",toupper(dup[i]));
            *(dup+i)=toupper(dup[i]);
            char sx=toupper(dup[i]);
             strncat(newstr,&sx,1);               
        }
        else if(isdigit(dup[i]))
        {
            printf("數字:%c\n",dup[i]);
            char sszi=dup[i];
            strncat(newstr,&sszi,1);   
        }
        else
        {
    
            //des=newstr;
            //des=dup[i];
            //strcat(des[1],dup[i]);  //strcpy(des, dup[i]);
            //memset(des, '\0', sizeof(des));
            //strcat(&des,&dup[i]);
            //*(des+i) = '*';
            //*(des+i+1)=dup[i];
            //printf("%c\n",dup[i]);
            strncat(newstr, &ch[i], 1);
            strncat(newstr, &ch[i], 1);
        }
        //
        
    }
    gxdup=duReplace(ch,"GXZX","高训中心");
    printf("gx=%s\n",gxdup);
    printf("newstr=  %s\n",newstr);
    // (1) 将GXZX前四个字符串中的大写字母转换成“高训中心”
    for (int i = 0; i < 4; i++) {
        if (isupper(ch[i])) {
            strcat(duresult, "高训中心");
        } else {
            // (2) 将字符串中其余的小写字母转换成大写字母
            char uppercaseChar = toupper(ch[i]);
            strncat(duresult, &uppercaseChar, 1);
        }
    }

    // (3) 数字降序排序
    int digits[10] = {0}; // 存储数字出现的次数
    for (int i = 0; ch[i] != '\0'; i++) {
        if (isdigit(ch[i])) {
            digits[ch[i] - '0']++;
        }
    }
    for (int i = 9; i >= 0; i--) {
        for (int j = 0; j < digits[i]; j++) {
            char digitChar = i + '0';
            strncat(duresult, &digitChar, 1);
        }
    }

    // (4) 特殊符号加倍输出
    for (int i = 4; ch[i] != '\0'; i++) {
        if (!isalpha(ch[i]) && !isdigit(ch[i])) {
            strncat(duresult, &ch[i], 1);
            strncat(duresult, &ch[i], 1);
        }
    }

    printf("处理后的字符串:%s\n", duresult);

  

 

 

 

    //分割字符串   https://githubmota.github.io/2017/12/29/2017-12-29-Linux-C-Split/
    char ssss[] = "hello, world! welcome to china!";  
    char delim[] = " ,!";  

    char *token;  
    for(token = strtok(ssss, delim); token != NULL; token = strtok(NULL, delim)) {  
        printf(token);  
        printf("+");  
    }  
    printf("\n");  

  

 

https://github.com/huawenyu/Design-Patterns-in-C
https://swedishembedded.com/design-patterns/
https://blog.csdn.net/ZCShouCSDN/article/details/80217199
https://www.sciencedirect.com/book/9781856177078/design-patterns-for-embedded-systems-in-c
https://www.oreilly.com/library/view/design-patterns-for/9781856177078/
https://github.com/topics/design-patterns?l=c

安装插件“Doxygen Documentation Generator”,用来生成注释。
安装插件”C/C++ Snippets”,用来生成文件头、代码块分割线等。或 KoroFileHeader
C/C++ Snippets插件设置

 

posted @ 2023-10-16 00:22  ®Geovin Du Dream Park™  阅读(11)  评论(0编辑  收藏  举报