字符串数组学习

#include <stdio.h>
#include <string.h>/*头文件*/
void main()
{
    char s1[100],s2[50];
    printf("Enter the first string:\n");
    gets(s1);                                  /*运用gets()函数进行输入*/
    printf("Enter the second string:\n");
    gets(s2);
    strcat(s1,s2);
    puts(s1);                                  /*运用puts()函数进行输出*/

    char str1[20] = {"how do you do"}, str2[] = {"How are you!"};
    strcpy(str1,str2);
    puts(str1);
}

输出函数puts()和gets()函数比printf和scanf的好处在于不用过多考虑格式化问题。

#include <stdio.h>
#include <string.h>
void main()
{
    char str1[20] = {"how do you do"}, str2[] = {"How are you!"};
    strcpy(str1,str2);/*字符串拷贝*/
    puts(str1);
}

字符串比较函数strcmp()、求字符串长度函数strlen()

#include <stdio.h>
#include <string.h>
void main()
{
    int a, b, c;
    char str1[]={"hello"}, str2[]={"how are you!"};
    a = strcmp(str1,str2);
    b = strcmp("Chinese","China");
    c = strcmp(str1,"how");
    printf("%d,%d,%d",a,b,c);/*按ASCII码比较,如果str1大于str2,输出1,如果str1等于str2,输出0;如果str小于str2,输出-1.*/
    printf("%d\n",strlen(str1));/*输出字符串str1的长度*/
    printf("%d\n",strlen(str2));/*输出字符串str2的长度*/
}


转载于:https://my.oschina.net/u/241930/blog/403396

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值