26,字符串函数
来源:互联网 发布:java调用第三方接口 编辑:程序博客网 时间:2024/06/08 02:03
#include <stdio.h>
#include <string.h>
int countStr(char ch[]);
int main(int argc,constchar * argv[]) {
//1,输出字符串,可以使用printf或puts
char name[] ="ljs";
printf("1,name = %s\n",name);
puts(name);
//2,接受字符串,可以使用scanf或gets
//scanf接受字符串,会以空格,tab,回车作为结束符号
printf("请输入一个字符串:\n");
char name1[10];
scanf("%s",name1);
printf("2, buf = %s\n",name1);
printf("请再输入一个字符串:\n");
gets(name1);
printf("2, buf = %s\n",name1);
//3,如何计算字符串的长度
char name2[] = "adbc";
int length = (int)strlen(name2);
//int length = countStr(name2);
printf("3,length = %i\n",length);
//4,字符串拼接
char str1[30] ="ljs";
char str2[20] =" is a good boy";
strcat(str1,str2);
//strncat(<#char *#>, <#const char *#>, <#size_t#>)
printf("4,str1 = %s\n",str1);
//5,字符串拷贝
char str3[30] ="abc";
char str4[] = "def";
strcpy(str3, str4);
//strncmp(<#const char *#>, <#const char *#>, <#size_t#>)
printf("5,str3 = %s\n",str4);
//6,字符串比较
//result = 0;证明两个字符串相等
//result > 0;证明前者大于后者
//result < 0;证明前者小于后者
char str5[] = "abce";
char str6[] = "abcd";
int result = strcmp(str5,str6);
printf("6,result = %i\n",result);
return 0;
}
int countStr(char ch[]){
int count = 0;
while (ch[count] != '\0') {
count++;
}
return count;
}
- 26,字符串函数
- 字符串函数
- 字符串函数
- 字符串函数
- 字符串函数
- 字符串函数
- 字符串函数
- 字符串函数
- 字符串函数
- 字符串函数
- 字符串函数
- 字符串函数
- 字符串函数
- 字符串函数
- 字符串函数
- 字符串函数
- 字符串函数
- 字符串函数
- A*寻路算法入门(六)
- Java正则表达式入门
- Java内存溢出的详细解决方案
- Aspect Based Sentiment Analysis datasets
- 成为Java高手的25个学习要点
- 26,字符串函数
- HDR(High Dynamic Range Imaging)在摄影中指高动态范围成像
- github android优秀的开源项目、作者推荐
- 单向链表 (Singly linked list) 的C++实现
- 用例图
- java API中Util包
- matlab:把cell中的某个元素删去
- 关于矩阵重复元素检测判断条件的粗陋看法
- matlab:cell的元素的调用—— acell(2)相比acell{2}