美文网首页
c语言字符串练习

c语言字符串练习

作者: 76577fe9b28b | 来源:发表于2017-02-24 10:22 被阅读55次
    1. 在一个字符串中查找另一个字符串出现的次数
    加入的头文件
    #ifndef struct_file_h
    #define struct_file_h
    
    #define OUT
    #define IN
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include<string.h>
    
    
    #endif
    
    头文件说明
    • #ifndef struct_file_h #endif这个宏是防止重复导入头文件
    • OUT, IN 是定义参数的作用是传入还是传出参数,
    int string_find_locals(IN const char *src, IN const char *find, OUT int *num){
       
        if (src == NULL || find == NULL || num == NULL) {
            
            fprintf(stderr, "string_find_locals err");
            return -1;
        }
      //如果传入参数在函数中有变化,则在函数中定义一个临时变量操作,尽量不要破坏传入进来的变量
        const char *src_in = src;
        int num_in = 0;
        while ((src_in = strstr(src_in, find)) != NULL) {
            
            num_in ++;
            src_in += strlen(find);
            if (src_in == NULL) {
                
                break;
            }
        }
       
        *num = num_in;
        
        return 0;
    }
    
    2. 去除字符串的前后空格
    //传入的字符串是可以改的
    int remove_space_string(char *src){
        
        if (src == NULL) {
            
            fprintf(stderr, "传入的参数为NULL");
            return -1;
        }
        
        char *src_in = src;
    
        int preIndex = 0;
        
        //isspace 在ctype.h头文件中
        while (isspace(*src_in) && *src_in != '\0') {
            
            preIndex ++;
            src_in++;
        }
        //说明是个空串
        if (preIndex == strlen(src)) {
            
               *src = '\0';
                return 0;
        }
        
        int lastIndex = strlen(src) - 1;
        src_in = src;
        
         while (isspace(*(src_in + lastIndex)) && lastIndex >= 0)
         {
            
              lastIndex --;
         }
        
            int i = 0;
            for (i = preIndex;  i <= lastIndex; i++) {
                
                *(src + i - preIndex) = *(src + i);
                
            }
            
            //字符串后面加上'\0'
            *(src + i - preIndex) = '\0';
        
        return 0;
    }
    
    3. 使用递归反转字符串
    //把rever中的字符串反转放到dst中
    int reversal_string(const char *rever,char *dst){
    
        if (rever == NULL || dst == NULL) {
            
            fprintf(stderr, "传入参数为NULL");
            return -1;
        }
        if (*(rever) != '\0') {
            
            reversal_string(rever + 1, dst);
        }
      /* char c = *rever;
        if (c != '\0') {
            char b[2] = {0};
            b[0] = c;
            strcat(dst, b);
        }
      */
        if (*rever) {
            *(dst +strlen(dst)) = *rever;
        }
        return 0;
    }
    
    
    4.替换字符串,如: "123tgywerwdtgy213",把tgy替换成yet。
       /*
        src:处理的字符串
        dst:处理后的字符串
        will_replace:要替换的字符串
        replace:替换的字符串
     */
    int string_replace(const char *src, char **dst, const char *will_replace, const char *replace){
    
        if (src == NULL || dst  == NULL || will_replace == NULL || replace == NULL) {
            
            return -1;
        }
        //在函数中开辟的内存,在调用完了之后要注意释放内存
        *dst = calloc(100, sizeof(char));
        
        const char *p = src;
        const char *q = src;
        while ( (p = strstr(p, will_replace)) != NULL) {
            
            strncat(*dst, q, p -q);
            strcat(*dst, replace);
            
            p  += strlen(will_replace);
            q = p;
            if (p == NULL) {
                break;
            }
        }
        
        strcat(*dst, q);
        return 0;
    }
    
    
    5. 把 char a[] = {"333","111","222"}; 与char b[][10] = {"444","000","aaaa"};合并,并把结果排序,放在char*p指向的数组中
    int string_cat(char **src1, int num1, char src2[][10], int num2, char ***p, int *num){
    
        if (src1 == NULL || num1 <=0 || src2 == NULL || num2 <=0 || p == NULL || num == NULL ) {
            
            return  -1;
        }
        
        char **p_in = calloc(num1 + num2, sizeof(char*));
        
        for (int i = 0; i < num1; i++) {
            
            // strlen(*(src1+ i) + 1)
            //这里申请的内存长度要加一,字符串后面有一个'\0'
            *(p_in + i) = calloc(strlen(*(src1+ i) + 1), sizeof(char));
            strcpy(*(p_in + i), *(src1+ i));
        }
        
        for (int i = 0; i < num2 ; i++) {
            
            //这里申请的内存长度要加一,字符串后面有一个'\0'
            *(p_in + num1 + i) = calloc(strlen(*(src2+ i) + 1), sizeof(char));
            
             strcpy(*(p_in + num1 + i), *(src2+ i));
        }
        
        sort_string(p_in, num1 + num2);
        
        *p = p_in;
        *num = num1 + num2;
        return 0;
    }
    
    

    字符串排序

    void sort_string(char **p, int num){
    
        char *q = NULL;
        for (int i = 0; i < num - 1; i++) {
            
            for (int j = i + 1; j < num; j++) {
                
                if (strcmp(*(p + i), *(p + j)) > 0) {
                    
                    q = *(p + i);
                    *(p + i) = *(p + j);
                    *(p + j)  = q;
                }
            }
        }
    }
    
    

    相关文章

      网友评论

          本文标题:c语言字符串练习

          本文链接:https://www.haomeiwen.com/subject/ekaiwttx.html