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("\n");
        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 = n - 1; // n是输入字符数目。不含\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] != '\0')
        n++;
    printf("length of %s = %d\n", 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\n");
    else if(strcmp(a, b) > 0)
        printf("a > b\n");
    else
        printf("a = b\n");

    printf("length of a(%s) = %d\n", a, strlen(a));
    printf("length of b(%s) = %d\n", b, strlen(b));
    puts(a);
    puts(b);

}

 

转载于:https://www.cnblogs.com/lqcdsns/p/6576471.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值