字符串與指針函數等技巧

字符串與指針的技巧

代碼都註釋掉了,想試哪段解除註釋就好了,都測試過,都沒問題,寫的過程中最大的問題就是申請指針,最好提前分配空間,否則程序容易意外終止!!希望大家也切忌,程序意外終止最好考慮一下指針未申請空間的問題。

// 指針與字符串
// 字符串的結束不同於數組,數組需要知道長度,字符串末尾結束永遠是'\0'

#include <stdio.h>
#include <string.h>
int copy_(char *src, char *tar);
int connect(char *s, char *t, char *q);
char * insert_char(char *s, char  *q, int n);
int match(char *a,char *b);
int ptr_string_sort(char *p[],int n);

////利用指針實現字符串複製
//copy_(char *src, char *tar)
//{
//    for(;*src != '\0';)
//    {
//        *tar = *src;
//        tar++;
//        src++;
//    }
//    *tar = '\0';  //添加字符串結束符
//}
//
////字符串連接
//connect(char *s,char *t, char *q)
//{
//    while(*s != '\0')
//    {
//        *q = *s;
//        s++;
//        q++;
//    }
//    *q++ = ' ';
//    while(*t != '\0')
//    {
//        *q = *t;
//        q++;
//        t++;
//    }
//    *q = '\0';
//}
//
////字符串插入
//char *insert_char(char *s, char  *q, int n)
//{
//    char *init_str,*init_s;
//    char *str = (char *)malloc(sizeof(char) * 100);
//    init_str = str;
//    init_s = s;
//    while(*s != '\0')
//    {
//        if(s  == init_s + n-1)
//        {
//            while(*q != '\0')
//                *str++ = *q++;
//            *str++ = *s;
//        }
//        else
//        {
//        *str++ = *s;
//        }
//        s++;
//
//    }
//    *str = '\0';
//    return init_str;
//
//}
//
////字符串匹配
//int match(char *a, char *b)
//{
//    int i = 0,flag = 0;
//    char *a_fst,*a_tmp,*b_tmp;
//    a_fst = a;  a_tmp = a;   b_tmp = b;
//    while(*a++ != '\0')
//    {
//        flag = 1;
//        while(*b_tmp != '\0')
//        {
//            if(*a_tmp != *b_tmp)
//            {
//                flag = 0;
//                break;
//            }
//            a_tmp++;
//            b_tmp++;
//        }
//        if(flag)
//            return i;
//        i++; //從下一個字符比較
//        a_tmp = a_fst+i;    b_tmp = b;  //設置循環初始條件
//    }
//    return -1;
//}
//
////字符串排序
//int ptr_string_sort(char *p[],int n)
//{
//    char *temp;
//    for(int i=0 ; i< n; i++)
//    {
//        for(int j = i + 1 ; j< n; j++)
//            if(strcmp(*(p + i) , *(p + j)) > 0)
//        {
//            temp = p[i];
//            p[i] = p[j];
//            p[j] = temp;
//        }
//    }
//}


int main(void)
{

//    //指針實現字符串複製的代碼
//    char *str = "Hello World!";
//    char *p = (char *)malloc(sizeof(char)); //這裏一定要給指針分配內存,否則會進程終止錯誤
//    copy_(str, p);
//    printf("%s\n\n\n",p);
//
//
//    //字符串連接的代碼
//    char *str1,*str2,*str3 = (char *)malloc(sizeof(char));
//    str1 = "hello";
//    str2 = "world";
//    connect(str1, str2, str3);
//    printf("%s + %s = %s",str1, str2, str3);
//
//
//    //指針實現字符串插入
//    int pos;
//    char *str_,*src_,*tar_;
//    src_ = "hello world!";
//    tar_ = "every ";
//    printf("\nplease input the position you want to insert:\n");
//    scanf("%d", &pos);
//    str_ = insert_char(src_,tar_,pos);
//    printf("%s",str_);
//
//
//    //指針實現簡單字符串匹配
//    char *src = "hello world forever!";
//    char *tar = "world";
//    int number;
//    number = match(src, tar);
//    printf("the start position is : %d", number);
//    printf("\nthe first char is : %c",*(src+number));
//
//    //使用指針的指針輸出字符串
//    char *strings[] = { "c language",
//                        "Basic",
//                        "world wide",
//                        "olympic",
//                        "great wall"};
//    char **p_ = strings;       //這裏定義單個指針會出現錯誤,指向指針的指針,由於strings定義的就是一個指針數組
//    for(int i=0; i<5; i++)
//        printf("%s\n",*(p_+i));
//
//
//    //使用指針的指針輸出月份號
//    char *month[] = {
//         "January",
//         "February",
//         "March",
//         "April",
//         "May",
//         "June",
//         "Junly",
//         "August",
//         "September",
//         "October",
//         "November",
//         "December"
//    };
//    char **p__ = month;
//    int num;
//    printf("please input a month you want to know: ");
//    scanf("%d", &num);
//    printf("\nThe %dth month is : %s",num, (*(p__ + num)+1)); //輸出函數,若輸出的是字符串,則應輸入的是首地址,檢測到'\0'自動結束
//
//
//    //使用指向指針的指針對字符串進行排序
//    char *strings_[] = { "c language",
//                        "basic",
//                        "world wide",
//                        "olympic",
//                        "great wall"};
//
//    char **p___ = strings_;
//    ptr_string_sort(strings_,5);
//    for(int i=0; i< 5; i++)
//        printf("%s \n", *(p___ + i));
    return 0;
}


————————————編程之路,你我相伴————————————
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章