DAY 8

作业:

1> 实现对数组的排序
2> 实现单词的逆置,在自定义函数中输出逆置后的字符串
3> 自定义函数实现字符串比较【使用非函数】
4> 自定义函数实现字符串拷贝【使用非函数】在自定义函数中输出拷贝后的字符串
5> 自定义函数实现字符串连接【使用非函数】在自定义函数中输出连接后的字符串
 

#include 
#include 
#include 
// 数组的排序,input函数循环输入,Bubble冒泡排序,Output输出
int Input();
void Bubble(int num);
void Output(int num);

void my_strrev(char str[], int start, int end);

int my_strcmp(char s1[], char s2[]);
void my_strcpy(char dest[], char src[]);
void my_strcat(char dest[], char src[]);

int arr[50] = {0}; // 作业1

int main(int argc, const char *argv[]){
    printf("=====作业1=====\n");
    int num = Input();
    Bubble(num);
    Output(num);

    printf("=====作业2=====\n");
    char str[50] = "";
    printf("Please input your string: ");
    getchar();
    gets(str);
    int len = 0;
    while(str[len]){len++;}
    len--;
    my_strrev(str, 0, len);

    {
    printf("=====作业3=====\n");
    char s1[50] = "";
    char s2[50] = "";
    printf("Please input your string 1 : ");
    gets(s1);
    printf("Please input your string 2 : ");
    gets(s2);
    printf("return : %d\n", my_strcmp(s1, s2));
    }

    {
    printf("=====作业4=====\n");
    char s1[50] = "";
    char s2[50] = "";
    printf("Please input your string 1 : ");
    gets(s1);
    printf("Please input your string 2 : ");
    gets(s2);
    my_strcpy(s1, s2);
    }

    {
    printf("=====作业5=====\n");
    char s1[50] = "";
    char s2[50] = "";
    printf("Please input your string 1 : ");
    gets(s1);
    printf("Please input your string 2 : ");
    gets(s2);
    my_strcat(s1, s2);
    }
    return 0;
}

// 
int Input(){
    int num = 0;
    printf("Please set your num: ");
    scanf("%d", &num);
    for(int i = 0; i < num; i++){
        printf("input arr[%d] : ", i);
        scanf("%d", &arr[i]);
    }
    return num;
}
void Bubble(int num){
    // num
    // 0~num-1
    for(int i = 0; i < num-1; i++){
        int count = 0;
        for(int j = 0; j < num-1-i; j++){
            if(arr[j] > arr[j+1]){
                int temp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = temp;
                count++;
            }
        }
        if(count == 0){
            break;
        }
        // Output(num);
    }
}
void Output(int num){
    for(int i = 0; i < num; i++){
        printf("output arr[%d] : %d\n", i, arr[i]);
    }
}

void my_strrev(char str[], int start, int end){
    int head = start;
    int tail = end;
    while(head < tail){
        char temp = str[head];
        str[head] = str[tail];
        str[tail] = temp;
        head++;
        tail--;
    }
    head = tail = start;
    int space = start;
    while(str[tail]){
        while(str[tail]!=' ' && str[tail]!='\0'){
            tail++;
        }
        space = tail;
        tail--;
        // reverse(head ~ tail-1)
        while(head < tail){
            char temp = str[head];
            str[head] = str[tail];
            str[tail] = temp;
            head++;
            tail--;
        }
        while(str[space] == ' '){
            space++;
        }
        head = tail = space;
        // my_strrev(str, head, tail);
    }
    puts(str);
}

int my_strcmp(char s1[], char s2[]){
    int num1 = 0, num2 = 0, min = 0;
    while(s1[num1]){num1++;}
    while(s2[num2]){num2++;}
    if(num1 > num2){
        min = num2;
    } else {
        min = num1;
    }
    for(int i = 0; i <= min; i++){
        // putchar(s1[i]);
        // putchar(s2[i]);
        if(s1[i] == s2[i]){
            if(!s1[i])
                return 0;
            else
                continue;
        } else {
            return s1[i]-s2[i];
        }
    }
}

void my_strcpy(char dest[], char src[]){
    int num1 = 0, num2 = 0;
    while(dest[num1]){num1++;}
    while(src[num2]){num2++;}
    if(num1 < num2){
        printf("Input length error!\n");
    } else {
        for(int i = 0; i <= num2; i++){
            dest[i] = src[i];
        }
        puts(dest);
    }
}

void my_strcat(char dest[], char src[]){
    int num1 = 0, num2 = 0;
    while(dest[num1]){num1++;}
    while(src[num2]){num2++;}
    for(int i = 0; i <= num2; i++){
            dest[num1+i] = src[i];
    }
    puts(dest);
}

DAY 8_第1张图片

你可能感兴趣的:(c语言,算法)