10.4本章扩充内容

/*
const类型的限定符:
int a;
const int *p=&a;
int const *p=&a;
*p是一个常量,而p不是。
int * const p=&a;
p是一个常量,而*p不是,可以*p=10。
const int *const p=&a;
p和*p都是常量。
*/
/*
字符处理函数:
int isdigit(int c);是数字返回真,否则为假。
int isalpha(int c);是字母返回真,否则为假。
int isalnum(int c);是数字或字母返回真,否则为假。
int islower(int c);是小写字母返回真,否则为假。
int issupper(int c);是大写字母返回真,否则为假。
int tolower(int c);将大写字母变为小写字母。返回字母。
int toupper(int c);将小写字母变为大写字母。
int isspace(int c);是空白字符,返回真。'\n'、' '、'\f'、'\r'、'\t'、'\v'。
int iscntrl(int c);是控制字符,返回真。
int isprint(int c);是打印字符,返回真。
int isgraph(int c);是除了空格之外的打印字符,返回真。
*/
//统计各种类型的字符的个数
#include<stdio.h>
#define N 80
int main()
{
    char str[N];
    int i,letter=0,number=0,space=0,others=0;
    gets(str);
    for(i=0;str[i]!='\0';i++)
    {
        if(str[i]>='a'&&str[i]<='z'||str[i]>='A'&&str[i]<='Z')
            letter++;
        else if(str[i]>='0'&&str[i]<='9')
            number++;
        else if(str[i]==' ')
            space++;
        else
            others++;
    }
    printf("letter=%d,number=%d,space=%d,others=%d\n",letter,number,space,others);
    return 0;
}
#include<stdio.h>
#include<ctype.h>//包含字符处理函数的头文件
#define N 80
int main()
{
    char str[N];
    int i,letter=0,number=0,space=0,others=0;
    gets(str);
    for(i=0;str[i]!='\0';i++)
    {
        if(isalpha(str[i]))
            letter++;
        else if(isdigit(str[i]))
            number++;
        else if(isspace(str[i]))
            space++;
        else
            others++;
    }
    printf("letter=%d,number=%d,space=%d,others=%d\n",letter,number,space,others);
    return 0;
}
//将名和姓的第一个字母转换为大写字母
#include<stdio.h>
#include<ctype.h>
#define N 20
int main()
{
    char str[N];
    int i=1;
    gets(str);
    str[0]=toupper(str[0]);
    while(!isspace(str[i]))
    {
        i++;
    }
    while(isspace(str[i]))
    {
        i++;
    }
    str[i]=toupper(str[i]);
    printf("%s\n",str);
        return 0;
}
//数值字符串向数值转换
/*
double atof(const char *nptr);
将字符串转换成双精度浮点数,返回这个双精度浮点数
int atoi(const char *nptr);、
将字符串转换成整数,返回这个整数
long atol(const char *nptr);
将字符串转换成长整型数,返回这个长整型数
*/

#include<stdio.h>
#include<stlib.h>//包含相应的头文件
int main()
{
    char str[]="   123.5";
    int a;
    double b;
    long c;
    a=atoi(str);
    b=atof(str);
    c=atol(str);
    printf("a=%d,b=%f,c=%ld\n",a,b,c);
    return 0;
}
发布了34 篇原创文章 · 获赞 2 · 访问量 461

猜你喜欢

转载自blog.csdn.net/qq_42148307/article/details/105022543