c/c++字符串拼接追加复制,字符串和int相互转换,sscanf的转义
2017-09-06 21:41
483 查看
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
相关文章推荐
- IOS开发NSString与int和float的相互转换以及字符串拼接、NSString、NSData、char* 类型之间的转换
- IOS开发NSString与int和float的相互转换以及字符串拼接、NSString、NSData、char* 类型之间的转换
- IOS开发NSString与int和float的相互转换以及字符串拼接、NSString、NSData、char* 类型之间的转换
- IOS开发NSString与int和float的相互转换以及字符串拼接
- C++中的int和string的相互转换
- c++ 将16进制字符串转换成为int数值
- c++中64位int与字符串的转换
- C/C++字符串,字符数组,字符指针及其相互静态拷贝与追加的安全问题解决方案(1)
- C++实现字符串和整数之间的相互转换
- 二进制值和十六进制字符串相互转换的C++代码
- C/C++中int/long/float/double数值类型与字符串互相转换
- c++中string与int之间相互转换
- C++字符串和整数相互转换
- C++数值与字符串相互转换的那些事(一)字符串转数值(转载请注明)
- C/C++中字符串与数字相互转换
- CString、char*、string、int、_bstr_t、CTime、COleDateTime相互转换,判断一个字符串是一个浮点数
- 有关int,float,double型与字符串char*类型的相互转换
- C++ - 字符串(string)和数字(int、float...)互相转换
- C++ 整型和字符串相互转换
- C/C++使用心得:enum与int的相互转换