任务代码:
统计数字字符的个数isdigit || isalpha:
#include <stdio.h>
#include <ctype.h>
int main()
{
char str[50];
printf("请输入字符串:");
gets(str);
int i=0,n=0;
while (str[i]!='\0')
{
if(isdigit(str[i]))
{
n++;
}
i++;
}
printf("数字字符有:%d个!\n",n);
return 0;
}
两个字符串连结 strcat:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main()
{
char str1[50]="i love you -";
char str2[50]=" my dear";
printf("%s\n",strcat(str1, str2));
return 0;
}
字符串复制函数 strcpy:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main()
{
char str1[50];
char str2[50]=" my dear";
strcpy(str1, str2);
printf("%s\n",str1);
return 0;
}
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main()
{
char str1[50]="my dear";
char str2[50]=" my dear";
if(strcmp(str1, str2)!=0)
{
printf("两个字符串不同!");
}
else printf("字符串相同!");
return 0;
}
字符串长度函数strlen:
#include <stdio.h>
#include <string.h>
int main()
{
char str[]="beijing";
printf("有%ld个字符!\n4",strlen(str));//注意这里strlen 返回的是size_t的长度,是一个系统定义的宏,是一个long型的长度
return 0;
}
执行情况:
第一题:
第二题:
第三题:
第四题:
第五题:
知识总结:
字符函数&字符串函数
字符函数:
字符串函数:
两个字符串的连结:
字符串的复制:
字符串比较:
字符串长度函数: