zoukankan      html  css  js  c++  java
  • C语言--一维数组,字符数组

    版权声明:本文为博主原创文章,未经博主同意不得转载。 https://blog.csdn.net/zuoyou1314/article/details/30799519
    #import <Foundation/Foundation.h>
    
    int main(int argc, const char * argv[])
    {
    #pragma mark----------数组
       //数组是容器,同样数据类型
        //构造类型
        
    //    int  a[3]={5,2,0};
        //类型
    //    int[3];
        //变量名
    //    a;
        //初值
    //    {5,2,0};
        
        
    //    int a[4] = {9,8,7,6};
    //    float b[6] = {2.5,3.14};
    //    char c[3] = "abc";
    //    char d[3] = {'a','b','c'};
    //    BOOL e[4] = {YES,NO};
        
        //a[0]+a[1]操作数组下标取值赋值
        
        //前有类型修饰符,代表数组,没有类型修饰符,代表下标
        
        //1.生成一个包括20个元素数组,数组的值取值在30到70之间,并求出数组元素的和
    //    int sum = 0, a[20]={0};
    //    for (int i = 0; i<20; i++) {
    //        a[i]=arc4random()%41+30;
    //        printf("%d ",a[i]);
    //        sum = sum + a[i];
    //    }
    //    printf("
    sum=%d",sum);
        //2.复制
    //    int a[20]={0},b[20]={0};
    //    for (int i = 0; i<20; i++) {
    //        a[i]=arc4random()%41+30;
    //        printf("%d ",a[i]);
    //        b[i]=a[i];
    //    }
    //    printf("
    ");
    //    for (int j = 0; j<20; j++) {
    //        printf("%d ",b[j]);
    //    }
        //3.生成两个数组,然后两个数组相应下标元素相加,放到第三个数组中
    //    int a[10]={0},b[10]={0},c[10]={0};
    //    for (int i = 0; i<10; i++) {
    //        a[i]=arc4random()%(40-20+1)+20;
    //        b[i]=arc4random()%(40-20+1)+20;
    //        c[i]=a[i]+b[i];
    //          printf("%d + %d = %d
    ",a[i],b[i],c[i]);
    //    }
    //    printf("
    ");
    //    for (int j = 0; j<10; j++) {
    //            printf("%d ",b[j]);
    //    }
    //    printf("
    ");
    //    for (int j = 0; j<10; j++) {
    //        printf("%d ",c[j]);
    //    }
        
    #pragma mark--------------1
        //注意,
        //1,系统不会检測数组元素的下标是否越界,编程时,必须保证数组下标不能越界
        //2,数组是一个总体,不能直接參加运算,仅仅能对单个元素进行处理,一般会用到数组的地方,就会用到循环(循环就是为了数组而生)
        
    //    scanf("%d",&a);
    //    getchar() 打印123def 结果是1
    //    printf(""),打印123def 结果是123
        
        /**
         *  从键盘缓冲区读取数据
         */
        
        
    //    int a[10]={0};
    //    for (int i = 0; i<10; i++) {
    //        a[i]=arc4random()%31;
    //        printf("%2d ",a[i]);
    //    }
    //    for (int i = 0; i<10-1; i++) {
    //        for (int j = 0; j<10-1-i; j++) {
    //            if (a[j]>a[j+1]) {
    //                int temp = a[j];
    //                a[j]=a[j+1];
    //                a[j+1]=temp;
    //            }
    //        }
    //    }
    //    printf("
    ");
    //    for (int i =0; i<10; i++) {
    //        printf("%-2d ",a[i]);
    //    }
        
    //    int a[10]={0};
    //    for (int i = 0; i<10; i++) {
    //        a[i]=arc4random()%(40-20+1)+20;
    //        printf("%d ",a[i]);
    //    }
    //    for (int i = 0; i<10-1; i++) {
    //        for (int j = 0; j<10-1-i; j++) {
    //            if (a[j]>a[j+1]) {//max<a[i],min>a[i]
    //                int temp = a[j];
    //                a[j]=a[j+1];
    //                a[j+1]=temp;
    //            }
    //        }
    //    }
    //    printf("
    ");
    //    for (int i =0; i<10; i++) {
    //        printf("%d ",a[i]);
    //    }
        
    //    strlen("hello");
    #pragma mark--------------2
        //字符串长度  比方hello,字符串长度5
    //    printf("%lu",strlen("hello"));
        //字符串所占空间 比方char[10]="hello" 字符串所占空间10;
    //    char c[] = "hello";
    //    printf("%lu",sizeof(c));
    //    for (int i = 0; i<sizeof(c); i++) {
    //        printf(" %c ",c[i]);
    //    }
    //    printf("
    %s ",c);
        //等同于以下
        //%s,从首地址開始打印,直到打到/0结束
    //    char d = 0;
    //    int i = 0;
    //    while ((d = c[i]) != '') {
    //        printf("%c",d);
    //        i++;
    //    }
    
        //strlen,測量字符串长度
        //strcpy
        //strcmp,比較
        //strcat//拼接
    //    char name[20]="zuoyoudong";
    //    printf("%s length = %lu",name,strlen(name));
    //    int i = 0;
    //    while (name[i] !='') {
    //        printf("%c",name[i]);
    //        i++;
    //    }
    //    printf("%d",i);
        
        
    //    char z[]="zuo";
    //    char y[5]="yi";
    //    strcpy(y, z);
    //    printf("%s",strcpy(y, z));
        //1,"hello",2,"abcdefghi",3,拷贝,helloghi
    
    #pragma mark--------------3
    //    char str1[50] = "hangsan";
    //    char str2[10] = "lisi";
    //    strcpy(str2, str1);//前面目的串,后面来源串,const仅仅可读,不可赋值
    //    printf("%s",str2);
        
        
        //字符串拼接strcat 注意:是否有足够的空间,放长度
    //    strcat(str1, str2);//带const修饰是不变的
    //    strcat(str1, str2);
    //    printf("%s",str1);
        
        
        //字符串比較strcmp(按ASC码值求大小)
    //    int result = strcmp(str1, str2);
    //    printf("%d",strcmp(str1, str2));
    //    printf("%d",result);
    //    if (result>1) {
    //        printf("
    %s 大于 %s",str1,str2);
    //    }else if (result==0){
    //        printf("%s 等于 %s",str1,str2);
    //    }else{
    //        printf("
    %s 小于 %s",str1,str2);
    //    }
        
        
        //strlen
        //strcmp
        //strcat
        //strcpy
        
        
        
        //查找字符串中的空格数
    //    char str[50] = "I love iOS,i want an iPhone5s";
    //    int i = 0,count = 0;
    //    while (str[i]!='') {
    //        char c = str[i];
    //        if (c ==' ') {
    //            count++;
    //        }
    //        i++;
    //    }
    //    printf("%d",count);
    
    #pragma mark--------------4
        //把字符倒转过来
        //解题思路!
        //凡是交换要定义第三方temp
        char str1[]="hello";
        char str2[5]=" ";
        long length = strlen(str1);
    //    printf("%lu",length);
        printf("%lu",sizeof(str1));
    //    long length =strlen(str1);
    //    printf("%lu",length);
    //    int  i = 0;
    //    while (str1[i] !='') {
    //        char temp = str1[i];
    //        str1[i]=str2[i];
    //        str2[i]=temp;
    //        
    //    }
    //    printf("%s",str2);
    //    for (int i = 0; i<sizeof(str1); <#increment#>) {
    //        <#statements#>
    //    }
    //#pragma mark--------------5绝对值
    //#pragma mark--------------6for循环
    //#pragma mark--------------7作业
    

查看全文
  • 相关阅读:
    mysql从视图view简化到带子查询的sql
    my.ini或my.cnf
    Windows文件在linux下每行后出现^M字样
    Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'
    javascript 判断中文字符长度
    该如何正确的使用position属性 它的作用是什么?
    css中em与px的介绍及换算方法
    如何卸载Linux下的Apache?
    HDU 3954 Level up
    HDU 4027 Can you answer these queries?
  • 原文地址:https://www.cnblogs.com/ldxsuanfa/p/10946525.html
  • Copyright © 2011-2022 走看看