zoukankan      html  css  js  c++  java
  • step1 . day6 C语言基础练习之数组和字符串

    今天复习之数组即字符串相关知识,贴几个小练习,体会一下数组细节知识点:

    1.冒泡排序

    #include <stdio.h>

    #define N 10

    int main(int argc, const char *argv[])
    {
    int i,j;
    int arr[N],tep;
    printf("please input 10 numbers: ");
    for(i=0 ; i<N ; i++){
    printf("arr[%d]:",i);
    scanf("%d",&arr[i]);
    }
    printf("arr is :");
    for(i=0 ; i<N; i++){
    printf("%d ",arr[i]);
    }
    printf(" ");

    for(i=0;i<N-1;i++){
    for(j=0;j<N-i-1;j++){
    if(arr[j]>arr[j+1])
    {
    tep = arr[j];
    arr[j] = arr[j+1];
    arr[j+1] = tep;
    }
    }
    }
    printf("sort arr is:");
    for(i=0 ; i<N; i++){
    printf("%d ",arr[i]);
    }
    printf(" ");
    return 0;
    }

    2.选择排序(不使用中间变量交换)

    #include <stdio.h>

    #define N 10

    int main(int argc, const char *argv[])
    {
    int i,j;
    int index;
    int arr[N];

    printf("please input 10 numbers: ");
    for(i=0 ; i<N ; i++){
    printf("arr[%d]:",i);
    scanf("%d",&arr[i]);
    }

    printf("arr is :");
    for(i=0 ; i<N; i++)
    printf("%d ",arr[i]);
    printf(" ");


    for(i=0;i<N;i++){
    index = i;
    for(j=i+1;j<N;j++){
    if(arr[index]>arr[j])
    index = j;
    }
    if(index != i){
    arr[i] += arr[index];
    arr[index] = arr[i] - arr[index];
    arr[i] = arr[i]- arr[index];
    }
    }

    printf("sort arr is:");

    for(i=0 ; i<N; i++)
    printf("%d ",arr[i]);
    printf(" ");

    return 0;
    }

    3.复写strcpy strcmp strcat strlen(提前预习一下函数和使用指针的函数传参)

    #include <stdio.h>
    #include <string.h>

    #define N 50
    int lenth(char *src)
    {
    int i=0;
    while(src[i] != '')
    i++;
    return i;
    }

    void cpy(char *dest,char *src){
    int i = 0;
    while(src[i] != ''){
    dest[i] = src[i];
    i++;
    }
    dest[i] = '';

    printf("copy completely! dest is :%s ",dest);
    }

    void cat(char *dest,char *src){
    int i = 0,j = 0;
    int k;
    while(src[i] != '')
    i++;
    while(dest[j] != '')
    j++;
    for(k=0;k<i;k++)
    dest[j+k] = src[k];
    dest[j+k] = '';

    printf("cat completely! dest is :%s ",dest);
    }

    int compare(char *dest,char *src){
    int i =lenth(src);
    for(i = 0;i<lenth(src);i++)
    if(src[i] > dest[i])
    return -1;
    else if(src[i] < dest[i])
    return 1;
    else
    continue;
    return 0;
    }


    int main(int argc, const char *argv[])
    {
    char src[20];
    char dest[N];

    printf("please input a src str:");
    // scanf("%s",src);//无法如数空格符
    gets(src);
    printf("please input a dest str:");
    //scanf("%s",dest);
    gets(dest);

    // printf("cmp result:%d ",compare(dest,src));
    // printf("compare函数验证:%d ",strcmp(dest,src));

    // cpy(dest,src);
    // printf("验证cp函数n:%s ",strcpy(dest,src));
    //
    // printf("src的字符长度是:%d ",lenth(src));
    // printf("验证lenth函数:%d ",strlen(src));

    // cat(dest,src);
    printf("cat函数验证:%s ",strcat(dest,src));
    return 0;
    }

  • 相关阅读:
    56. Merge Intervals
    Reorder List
    Merge Two Sorted Lists
    彻底删除kafka topic数据
    什么时候类加载
    checkpoint的作用
    case when
    SQL:将查询结果插入到另一个表的三种情况
    IFNULL函数
    kafka主要配置
  • 原文地址:https://www.cnblogs.com/huiji12321/p/11123381.html
Copyright © 2011-2022 走看看