字符数组的输入与输出

  • 字符输入输出有两种办法。一是用”%c’’输入输出一个字符,二是,用“%s”将字符串一次输入或输出。
  • scanf函数中的输入项如果是字符数组名,就不加地址符“&”。

    字符串处理函数

putsgetsstrcatstrcpy strncpystrcmpstrlenstrlwrstrupr
一般形式puts(字符数组)gets(字符数组)strcat(字符数组1,字符数组2)strcpy(字符数组1,字符数组2) strncpy(字符数组1,字符数组2)strcmp(字符串1,字符串2)strlen(字符数组)strlwr(字符串)strupr(字符串)
作用输出一个字符串,作用相当于printf输入一个字符串,作用相当于scanf将两个字符串连接起来,字符串2接到字符串1后。strcpy将字符串2中字符复制到字符串1中,strncpy可以把字符串2中的前n个字符复制到字符串1中。对两个字符串从左到右一个一个比较(ASCLL码值大小的比较)直到出现不同字符或遇到“\0”结束,遇到不同的字符时,按第一个不同的字符的比较结果为准。计算出字符串中有几个字符将字符串中的大写字母转换成小写字母将字符串中的大写字母转换成小写字母。

puts and gets函数

#include<stdio.h>

int main()
{
    char str[10];
    gets(str);
    puts(str);

}

strcat

#include<stdio.h>

int main()
{
   char str1[30]={"People's Republic of "};
   char str2[]={"China"};
   printf("%s", strcat(str1,str2));
}

strcpy strncpy

#include<stdio.h>

int main()
{
    char str1[10] = "asdas";
    char str2[] = {"China"};
    strcpy(str1,str2);
    printf("%s",str1);

}

strcmp

int main()
{
    char str1[] = {"compare"};
    char str2[] = {"computer"};
    if(strcmp(str1,str2)>0)
        printf("yes");
    else
        printf("no");

}

strlen

#include<stdio.h>

int main()
{
    char a[10] = {"China"};
    printf("%d",strlen(a));
}

strlwr

#include<stdio.h>

int main()
{
    char str1[] = {"CHINA"};
    printf("%s",strlwr(str1));
}

strupr

#include<stdio.h>

int main()
{
    char str1[] = {"china"};
    printf("%s", strupr(str1));
}

将一个数组中的值按逆序重新存放

#include<stdio.h>

int main()
{
    int a[5];
    int i, j, t;
    for(i = 0;i < 5;i++)
       scanf("%d", &a[i]);
    for(j = 0;j < 2;j++)
    {
        t = a[j];
        a[j] = a[4 - j];
        a[4 - j] = t;
    }
    for(i = 0;i < 5;i++)
        printf("%d ", a[i]);

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值