1使用字符串函数必须包含头文件
2.gets 和puts的用法,就相当于scanf和printf;
#include<stdio.h> #include<string> int main() { char x[100] = "ABC"; char y[100] = "123456"; gets(x); put(x); return 0; }
3,strlend的用法,求数组的长度
4.计算一个数组中有几个字母
#include<string.h> int main() { //先定义一个字符串数组 char s[100] = "ashdsnhcucgwicgyg2345450abc"; int n, i; n = 0; for (i = 0; s[i] != '\0'; i++) //for循环,直到遇到\0才停 if (s[i] >= 'a' && s[i] <= 'z' || s[i] >= 'A' && s[i] <= "Z") //if语句做出判断,n充当计算器,循环每进行一轮n++,&&与,||或 n++; printf("%d", n); return 0; }
5从字符串中打印整数
#include<string.h> int main() { char s[100] = "362"; //数组内'3', '6', '2', '\0' int n, i; //n的变量初始化 n = 0; for (i = 0;s[i] != '\0'; i++) n = n * 10 + s[i] - '0'; printf("%d", n); return 0; }
6点单词
int main() { char s[100] = "ancbnd shdchajxbx bygxhx sahsxba i love you xiang bao "; int n, i, k; n = 0; k = 1; for (i=0;s[i] !='\0';i++) if (s[i] >= 'a' && s[i] <= 'z' || s[i] >= 'A' && s[i] <= 'Z') { if (k == 1) { n++; k = 0; } } printf("%d", n); return 0; }