A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始



  • 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 个回复

倒序浏览
奈斯
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马