C语言基础三

C——数组

一维数组的定义和引用
  • 定义:类型说明符 数组名[常量表达式]
    int a[ 10 ];他表示定义了一个整形数组,数组名为a,有10个元素。
    注意:C语言不允许对数组的大小做动态定义。
  • 一维数组元素的引用:数组名[下标]
  • 一维数组初始化

      int[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  • 一维数组例子
    冒泡法对十个数排序(升序)

      #include<stdio.h>
      #include<math.h>
      void main(){
          int i, j, t, a[10];
          printf("input 10 numbers: \n");
          for(i = 0; i< 10; i++)
              scanf("%d", &a[i]);
              printf("\n");//输入10个数字
          for(j = 0; j < 9; j++)//进行9次循环,实现9躺排序
              for(i = 0; i < 9-j; i++)//在每一趟中进行9-j次比较
                  if(a[i] > a[i+1]){
                      t = a[i];
                      a[i] = a[i+1];
                      a[i+1] = t;
                  }
                  printf("the 10 numbers: \n");
                  for(i = 0; i < 9; i++){
                      printf("%d", a[i]);
                      printf("\n");
                  }
      }
二维数组的定义和引用
  • 定义: 类型说明符 数组名[常量表达式] [常量表达式]
    float [3][4]:先横后列
  • 二维数组的引用同一维数组类似
  • 二维数组的初始化

      int a [3] [4] = {{1,2,3}, {4,5,6}, {1,3,4,5,6}};//或
      int a [3] [4] = {1,2,3,4,5,6,7,8,9,0};
    没有占位的默认为0。
  • 二维数组举例:数组的行和列互换

      #include<stdio.h>
      #include<math.h>
      void main(){
          int a[2][3] = {{1,2,3},{4,5,6}};
          int b[3][2], i, j;
          printf("array a: \n");
          for(i = 0; i<= 1;i++)
          {
              for(j = 0; j<= 2; j++)
              {
                 printf("%5d", a[i][j]);
                 b[j][i] = a[i][j];
              }
              printf("\n");
          }
          printf("array b: \n");
          for(i = 0; i<= 2;i++)
          {
              for(j = 0; j<= 1; j++)
                 printf("%5d", b[i][j]);
              printf("\n");
          }
      }
    字符数组
  • 字符数组的定义
    char c[10];
  • 初始化
    char c[5] = {'w', 'i', 'b',' ', 'b'};
  • 字符数组的输入输出
    逐个输入输出:%c;
    整个字符串一次输出输入:%s;
    例子:

      char c[] = {"china"};
      printf("%s",c);
    注意: scanf函数中的输入输出如果是字符数组名,不要再加地址符&,
  • 字符串处理函数
  1. puts(字符数组)
    其作用是将一个字符串输出在终端。
  2. gets函数,作用是从终端输入一个字符串到字符数组,并得到一个函数值。
  3. stract函数
    格式:stract(字符数组1,字符数组2)
    该函数是一个字符串连接函数
  4. strcpy和strncpy函数
    strcpy函数是字符串复制函数,作用是将字符串2里的内容复制到字符串1中,strncpy(str1, str2,2)函数是将字符串2中的最前面的2个字符复制到字符串1中去。
  5. strcmp函数,是字符串比较的函数,
  6. strlen函数,是测试字符串长度的函数,函数的值为字符串中的实际长度,不包括'\0'在内。
  7. strlwr函数,是字符串小写函数,
  • 例子
    输入一行字符,统计共有多少个单词

      #include<stdio.h>
      #include<math.h>
      void main(){
          char string[81];
          int i, num = 0, word = 0;
          char c;
          gets(string);
          for(i = 0; (c = string[i])!= '\0'; i++)
              if(c == ' ') word =0;
              else if(word == 0)
              {
                  word = 1;
                  num ++;
              }
          printf("there are %d words in the line\n", num);
      }

    将两个字符串连接起来 不要用strcat函数

      #include<stdio.h>  
      int main()  
      {  
          char c1[80],c2[80];  
          int i,j;  
          printf("Input string1:");  
          gets(c1);  
          printf("Input string2:");  
          gets(c2);  
          for(i=0;c1[i]!='\0';i++)  
          {  
    
          }    
          for(j=0;c2[j]!='\0';i++,j++)  
          {  
              c1[i]=c2[j];//现在的c[i]已经遍历到了str1的最后了。  
          }     
          puts(c1);  
          return 0;  
      }

    输入一个平行四边形

      #include<stdio.h>
      void main()  
      {
          int i,j,k; 
          for(i=0;i<5;i++)  
          {
              for(j=0;j<5;j++)  
              {
                  printf("*");
              }  
              printf("\n");  
              for(k=1;k<=i+1;k++)  
              printf(" ");
          }
          printf("\n");  
      }

转载于:https://www.cnblogs.com/yehui-mmd/p/6736181.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值