c/c++字符串拼接追加复制,字符串和int相互转换,sscanf的转义

    char s[100]="hello world";
    //1.计算字符串的长度
    int lenth=  strlen(s);
    printf("length=%d\n",lenth);
    //2.字符串的拼接或追加
    char s2[100]="abcefg";
    strcat(s,s2);//将s2拼接到s后面

这里写图片描述


字符串的指定长度拼接

  char s[100]="hello world";
     char s2[100]="abcefg";
     //3.添加指定长度的字符串
     strncat(s,s2,3);

这里写图片描述

  //字符串的复制 
  char s[100]="hello world";
  char s2[100]="abcefg";
  strcpy(s,s2);

这里写图片描述


 char s[100]="hello";
     char s2[100]="hello";
     //比较两个字符串内容是否一样
     if(strcmp(s,s2)==0)
     {
         printf("the same");
     }else
     {
         printf("the different");
     }
     //===============================
     char s[100]="hello";
     char s2[100]="hellabac";
     //比较两个字符串内容是否一样
     if(strncmp(s,s2,4)==0)
         //比较前面四个是不是一样的
     {
         printf("the same");
     }else
     {
         printf("the different");
     }

这里写图片描述


字符串转int

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
     char s[100]="100";
    //string转int
    int value=atoi(s);
    printf("%d\n",value);
    return 0;
}

//int 转字符串
int main(void)
{
    int a=1000;
    char s[10]="0";
    sprintf(s,"%d\n",a);
    printf("s=%s\n",s);
    return 0;
}

这里写图片描述


sscanf的格式转换/义

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    char s[20]="34+76=";
    //求出这个表达式的值
    int a=0;
    int b=0;
    sscanf(s,"%d+%d",&a,&b);
    printf("result=%d\n",a+b);
    return 0;
}
//得出最终结果为110

这里写图片描述

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值