- 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
|
|