• C语言字符数组与字符串


    研究几个案例:

    输出图案:

    #include <stdio.h>
    
    void main()
    {
        char a[5][5] = {
            {' ', ' ', '1', ' ', ' '},
            {' ', '1', '2', '1', ' '},
            {'1', '2', '3', '2', '1'},
            {' ', '1', '2', '1', ' '},
            {' ', ' ', '1', ' ', ' '}
        };
        int i, j;
        for(i = 0; i < 5; i++)
        {
            printf("
    ");
            for(j = 0; j < 5; j++)
            {
                printf("%c", a[i][j]);
            }
        }
    }

    从键盘上输入一串字符(不多于40个,以回车换行符作为输入结束)存入数组,将其中的大写字母
    改为小写字母,其他字符不变,然后逆向输出。

    #include <stdio.h>
    
    void main()
    {
        char a[40];
        int i, n = 0;
    
        do {
            scanf("%c", &a[n]);
            n++;
        } while (a[n - 1] != '
    ');
    
        n = n - 1; // n是输入字符数目。不含
    
    
        for(i = 0; i < n; i++)
            if(('A' <= a[i]) && (a[i] <= 'Z'))
                a[i] += 32; // 是大写字母改为对应的小写字母
    
        for(i = n - 1; i >= 0; i--)
            printf("%c", a[i]);
    }

    从键盘上输入一个字符串,统计该字符串的长度。

    提示:字符串的长度是指字符串中有效字符的个数。而有效字符不含字符串结束标记符。

    #include <stdio.h>
    
    void main()
    {
        char a[80];
        int n = 0;
        scanf("%s", a);
        while(a[n] != '')
            n++;
        printf("length of %s = %d
    ", a, n);
    }

     

    #include <stdio.h>
    #include <string.h>
    
    void main()
    {
        char a[40], b[80];
        gets(a);
        strcpy(b, a);
        strcat(b, a);
    
        if(strcmp(a, b) < 0)
            printf("a < b
    ");
        else if(strcmp(a, b) > 0)
            printf("a > b
    ");
        else
            printf("a = b
    ");
    
        printf("length of a(%s) = %d
    ", a, strlen(a));
        printf("length of b(%s) = %d
    ", b, strlen(b));
        puts(a);
        puts(b);
    
    }
  • 相关阅读:
    C++实现二叉树的相应操作
    C++对文件的操作
    C++与C的区别二
    C++多线程编程二
    C++多线程编程一
    C语言多线程编程二
    C语言多线程编程一
    HTML练习(三)
    磁盘/内存模式查询数据
    RestTemplate使用不当引发的问题分析
  • 原文地址:https://www.cnblogs.com/lqcdsns/p/6576471.html
Copyright © 2020-2023  润新知